# 数学代考|MATH50009-Principles of Programming Exam

• 保持代码简洁

• 不要重复代码

• 对拓展开放，对修改关闭

• 组合由于继承

• 单一职责

• 注释代码

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)

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)

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)

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)

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)

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)

Implement a new subclass of Exception named ConvergenceError . The new exception does not need to have any new methods or attributes.

(ii)

Implement a subclass of CentralDifferenceFunction called AdaptiveCentralDifferenceFunction . The constructor of this class should have parameters fn , 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)

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:

1. 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.

2. 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)

Ensure that your code passes Flake8.

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

## contact ## Recent Case

### PHP代写：100%原创代码，首单享受折扣优惠

PHP（超文本预处理器）被称为通用脚本语言，可用于

## Service Scope

C|C++|Java|Python|Matlab|Android|Jsp|Prolo  