Path: blob/master/01_00_numerical_differentiation.ipynb
374 views
A primer on numerical differentiation
In order to numerically evaluate a derivative at point , we approximate is by using finite differences: Therefore we find:
Then we re-write the derivative in terms of discrete differences as:
Example
Let's look at the accuracy of this approximation in terms of the interval . In our first example we will evaluate the derivative of at .
Why is it that the sequence does not converge? This is due to the round-off errors in the representation of the floating point numbers. To see this, we can simply type:
Let's try using powers of 1/2
In addition, one could consider the midpoint difference, defined as:
For a more complex function we need to import it from the math module. For instance, let's calculate the derivative of at , including both the forward and midpoint differences.
A more in-depth discussion about round-off errors in numerical differentiation can be found here
Special functions in numpy
numpy provides a simple method diff() to calculate the numerical derivatives of a dataset stored in an array by forward differences. The function gradient() will calculate the derivatives by midpoint (or central) difference, that provides a more accurate result.
Notice above that gradient() uses forward and backward differences at the two ends.
More discussion about numerical differenciation, including higher order methods with error extrapolation can be found here.
The module scipy also includes methods to accurately calculate derivatives:
One way to improve the roundoff errors is by simply using the decimal package