1. Homepage
  2. Exam
  3. [2021] Imperial College London - Principles of Programming - Q1 - Numerical Derivative

[2021] Imperial College London - Principles of Programming - Q1 - Numerical Derivative

This question has been solved
Engage in a Conversation

Question 1 Numerical derivative


The object of this exercise is to create functions which can compute their own numerical derivative. Given a real-valued function f of one real variable, and a small ?>0, the derivative of f at a point x can be approximated by the central difference approximation:


1. (a) (2 marks)
Create a package derivative containing a module derivative.py. Make the packages in this repository installable. You will aid your own completion of the exam by installing this package in editable mode.

1. (b)
(i) (2 marks)
Create a class derivative.derivative.Function whose constructor takes a function fn as an input and stores it. This is the function we will evaluate and whose derivative we will take.

(ii) (2 marks)
Implement the __call__ special method on Function to take a single argument (the value of x), evaluate fn on that input, returning the result.

1 (c)
(i) (2 marks)
Implement a subclass of Function called CentralDifferenceFunction. The constructor of this class should take fn and a scalar parameter delta. This constructor should call the superclass constructor to store fn, and it should store delta on the object instance.

(ii) (2 marks)
Implement a method CentralDifferenceFunction.derivative which takes two parameters, x and delta. The delta parameter should be made optional by giving it a default value of None. The derivative method should return the central difference approximation to the gradient of fn at x. If delta is passed to the method, then this value should be used in the calculation. If the argument is omitted then the value of delta passed in to the constructor should be used.

1 (d)
(i) (1 mark)
Implement a new subclass of Exception named ConvergenceError. The new exception does not need to have any new methods or attributes.

(ii) (1 mark)
Implement a subclass of CentralDifferenceFunction called AdaptiveCentralDifferenceFunction. The constructor of this class should have parametersfn, delta, eps, and max_its. The constructor should call the superclass constructor to store fn and delta, and should store eps and max_its directly on the object.

(iii) (4 marks)
Implement a method AdaptiveCentralDifferenceFunction.derivative which takes x as an argument. This function should call the superclass derivative method for a sequence of values of delta. The first value of delta should be the one passed to the class constructor. The next value of delta should be half the first value. This counts as the first iteration. The method should keep calculating more estimates of the derivative, each time with delta half that of the previous time. This sequence stops when the first of the following conditions is satisfied:

The absolute value of the difference between successive derivative approximations falls below eps. In this case the iteration has succeeded and the last calculated approximation should be returned.
The number of iterations exceeds max_its. In this case, the iteration has failed and the method should raise ConvergenceError with the message "Failed to converge within the allowed number of iterations."
1 (e)
(i) (2 marks)
Ensure that your code passes Flake8.

(ii) (2 marks)
Ensure that your code otherwise conforms to good programming style as we have learned in this course.

There is no need to write any docstrings in this exam.

Get the Solution to This Question

WeChat (微信) WeChat (微信)
Whatsapp WhatsApp
Imperial College London代写,Principles of Programming代写,Python代写,Imperial College London代编,Principles of Programming代编,Python代编,Imperial College London代考,Principles of Programming代考,Python代考,Imperial College Londonhelp,Principles of Programminghelp,Pythonhelp,Imperial College London作业代写,Principles of Programming作业代写,Python作业代写,Imperial College London编程代写,Principles of Programming编程代写,Python编程代写,Imperial College Londonprogramming help,Principles of Programmingprogramming help,Pythonprogramming help,Imperial College Londonassignment help,Principles of Programmingassignment help,Pythonassignment help,Imperial College Londonsolution,Principles of Programmingsolution,Pythonsolution,