Previous Up Next

6.19.6  Numerical differentiation: numdiff

The numdiff command finds numerical approximations to derivatives.

numdiff uses Fornberg’s algorithm described in “Generation of Finite Difference Formulas on Arbitrarily Spaced Grids”, Mathematics of Computation, 51(184):699–706, 1988. The complexity of this algorithm is O(n2m) in both time and space. To avoid numerical instabilities, numdiff operates in exact arithmetic.

Note that α01,…,αn do not have to be equally spaced, but they must be mutually different and input in ascending order. There are no restrictions on the choice of x0.


Examples.


numdiff can be used for generating custom finite-difference stencils for approximation of derivatives.


Example.
Let X=[−1,0,2,4], Y=[a,b,c,d] and x0=1. To obtain an approximation formula for the second derivative:
Input:

numdiff([-1,0,2,4],[a,b,c,d],1,2)

Output:

2
5
 a
b
2
+
d
10

The approximation is always a linear combination of elements in Y, regardless of X, x0 and m.


Given the lists X=[α01,…,αn] and Y=[β01,…,βn], the Lagrange polynomial passing through points (αkk) where k=0,1,…,n can be obtained by setting m=0 and entering a symbol for x0.


Example.
Let X=[−2,0,1] and Y=[2,4,1]:
Input:

expand(numdiff([-2,0,1],[2,4,1],x,0))

Output:

4
3
 x2
5
3
 x+4

The same result is obtained by entering lagrange([-2,0,1],[2,4,1],x).


Previous Up Next