Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Numerical differentiation

Derivatives are everywhere in numerical computation: Newton’s method for equations needs them, optimisation algorithms follow gradients, stiff ODE solvers need Jacobians, and sensitivity analysis asks how outputs change with inputs. When a formula for the derivative is not available, or the function is a procedure rather than a formula, the derivative must be computed numerically.

Differentiation is in one sense the opposite of integration, and its numerical behaviour is opposite too. Integration averages values and is forgiving: errors in individual function values are smoothed out. Differentiation subtracts nearby values and divides by a small number: it amplifies errors. Every numerical differentiation method is a compromise between the error of approximating the derivative (which wants small steps) and the amplification of rounding errors (which wants large ones).

Choosing a method

Your situationRecommendedEntry
A function you can only evaluate, moderate accuracyCentral differences with a balanced stepFinite difference derivatives
Higher accuracy from function values aloneFive-point stencils, or Richardson extrapolationFinite difference derivatives
Gradients, Hessians and JacobiansCoordinate-wise central differencesFinite difference derivatives
Near machine precision with an error estimate, no step to chooseRidders’ extrapolation: adaptive::derivative, jacobian, hessianAdaptive derivatives
An analytic function you can evaluate at complex pointsCauchy integral formulaComplex contour integration
A smooth periodic function or one on an interval, sampled on a gridSpectral differentiationSpectral methods

Testing against SciPy

SciPy 1.17.1’s scipy.differentiate estimates derivatives adaptively: it applies central differences with a sequence of shrinking steps and extrapolates (Richardson), stopping when the estimates agree. The library uses one fixed step chosen to balance truncation and rounding. The references come from book/generator/data/make_derivatives_refs.py; the tables give relative errors against the exact derivatives:

Table 1.1. Scalar derivatives against SciPy's adaptive `derivative`.

functionf’: central differencef’: five-point stencilf’: SciPy derivativef’’: five-point stencilf’’: SciPy, nested
exp(x) at 18e-123e-141e-14 (2e-13 with a first step of 0.01)7e-115e-10
sin(x) at 14e-124e-141e-14 (2e-13 with a first step of 0.01)2e-112e-10
ln(x) at 0.11e-92e-9NaN (5e-15 with a first step of 0.01)5e-74e-13
1/(1 + 25 x^2) at 0.32e-103e-118e-11 (9e-15 with a first step of 0.01)7e-91e-12
x^(5/2) at 25e-127e-144e-15 (1e-13 with a first step of 0.01)2e-125e-10

Table 1.2. Gradients, Jacobians and Hessians against SciPy.

derivative (SciPy reference)library: relative errorSciPy: relative error
gradient of Rosenbrock’s function at (-1.2, 1) (differentiate.jacobian)1e-104e-16
its Hessian (differentiate.hessian)3e-92e-15
Jacobian of polar coordinates at (2, 0.5) (differentiate.jacobian)8e-121e-14

Adaptive extrapolation reaches 13 to 15 digits where the fixed-step formulas give 9 to 14, at the cost of many more evaluations, but it needs a sensible first step. SciPy’s default first step of 0.5 jumps past zero for at and returns NaN; the library’s step is relative to , so it stays near the point, but for the same reason it is too large for a function that varies on a scale much smaller than 1, which costs it accuracy at . The second derivatives of the five-point stencil lose more digits, as the step must be larger to keep rounding error, now divided by , in check.

In the library

The differentiation crate is lib_jsl_derivatives (API reference).