Chapter 6
Numerical Differentiation
The Chapter 6 companion programs illustrate differentiation from tabulated data and the improvement of centered finite differences through Richardson extrapolation. The resources below connect these examples with widely used numerical tools and provide benchmark functions and practical guidance for choosing a method and step size.
Software and library resources
The table below lists representative tools for numerical differentiation. Their formulas, endpoint treatments, and error estimates are not necessarily identical, so check the documentation before comparing results or using a routine in a larger calculation.
On a narrow screen, scroll horizontally to view all language columns.
| Numerical task | MATLAB | Python | C++ | Fortran |
|---|---|---|---|---|
| Derivatives of tabulated data First derivatives on uniform or nonuniform grids. |
gradientCentered differences in the interior and first-order one-sided differences at the endpoints. On a nonuniform grid, do not assume second-order accuracy; use the companion code or nonuniform finite-difference weights when needed |
numpy.gradientUse edge_order=2 when second-order endpoint
formulas are desired
|
Companion implementation or a custom local interpolation formula | Companion implementation or a custom local interpolation formula |
| Derivative of a supplied function Finite differences with method-specific step selection and error estimates. |
Evaluate the function on a grid and use
gradient,
or use the companion implementation
|
scipy.differentiate.derivativeAdaptive step size and an error estimate; requires SciPy 1.15 or later |
Boost finite_difference_derivativeAutomatically selected step and optional error estimate Boost complex_step_derivative
for analytic functions;GSL gsl_deriv_*
(C library, callable from C++)
|
Companion Richardson implementation, an implementation of Ridders' extrapolation method, or GSL through the FGSL Fortran bindings |
| Multidimensional gradient Partial derivatives of data sampled on a regular or specified grid. |
gradient
|
numpy.gradient
|
Apply one-dimensional finite-difference stencils along each coordinate direction | Apply one-dimensional finite-difference stencils along each coordinate direction |
| Discrete Laplacian Approximation of the sum of second partial derivatives. |
del2Multiply by 4 in two dimensions to obtain ∇2u; in N ≥ 2
dimensions multiply by 2N. At boundaries,
del2 linearly extrapolates second differences
from the interior
|
scipy.ndimage.laplaceUses unit index spacing and defaults to mode='reflect'; for uniform physical spacing
divide the result by h2
|
Custom finite-difference stencil with explicitly chosen boundary conditions | Custom finite-difference stencil with explicitly chosen boundary conditions |
| Derivatives of noisy data Smoothing or local polynomial fitting before differentiation. |
sgolay
for differentiation filters;sgolayfilt
for smoothing; equally spaced samples requiredcsaps
for smoothing splinesToolboxes required |
scipy.signal.savgol_filterSet deriv and the constant sample spacing
delta;make_smoothing_spline
|
Boost discrete_lanczos_derivative
for equally spaced data
|
Smoothing or local least-squares fitting followed by differentiation |
For functions implemented as compatible computer code, automatic
differentiation avoids finite-difference truncation error and does
not require a step size. Examples include
jax.grad,
MATLAB
dlgradient,
and
Boost.Math autodiff.
The differentiated operations must be supported by the selected tool.
Benchmark functions and reference derivatives
These examples can be used to test a new implementation, compare finite-difference formulas, or study how the error changes with step size. Compare each numerical result with the exact value shown in the table.
| Function and point | Exact derivatives | Why it is useful |
|---|---|---|
f(x) = exp(x) at x = 1 |
f'(1) = e ≈ 2.718281828459045f''(1) = e ≈ 2.718281828459045 |
All derivatives equal e, so the exact value and leading error constants are especially simple. |
f(x) = sin(x) at x = π/4 |
f'(π/4) = √2/2 ≈ 0.7071067811865476f''(π/4) = -√2/2 ≈ -0.7071067811865476 |
Checks signs and trigonometric derivatives. |
f(x) = ln(x) at x = 2 |
f'(2) = 1/2f''(2) = -1/4 |
Matches the Richardson-extrapolation companion program. |
f(x) = 1/(1+x2) at x = 1 |
f'(1) = -1/2f''(1) = 1/2 |
Tests a rational function with opposite derivative signs. |
f(x) = x4-2x2+1 at x = 1/2 |
f'(1/2) = -3/2f''(1/2) = -1 |
The centered first- and second-derivative errors are both exactly 2h2. A five-point stencil or one Richardson step is exact apart from roundoff. |
f(x) = tanh(20x) at x = 0.05 |
f'(0.05) = 20 sech2(1) ≈ 8.399486832280521f''(0.05) = -800 sech2(1)tanh(1) ≈ -255.8800033796898 |
A steep-gradient test in which both derivatives are nonzero. At x = 0, the centered second derivative would vanish identically by symmetry and would not test numerical accuracy. |
When an exact derivative is zero, report absolute error; relative error is undefined or misleading.
Choosing a differentiation method
| Character of the problem | Method to consider | Practical note |
|---|---|---|
| Smooth function available at arbitrary points | Centered finite differences | Usually more accurate than a forward or backward formula with the same step size and number of nearby evaluations. |
| Higher accuracy is needed | Higher-order stencil or Richardson extrapolation | Verify the expected convergence by repeating the calculation with smaller step sizes. |
| Point is at or near a boundary | One-sided finite difference or a shifted interpolation stencil | Do not evaluate the function outside its physical or tabulated domain. |
| Data are tabulated on a nonuniform grid | Differentiate a local interpolation polynomial | Use the actual coordinate values rather than formulas derived for constant spacing. |
| Data contain measurement noise | Local polynomial fit, smoothing spline, or Savitzky–Golay method | Differentiation amplifies high-frequency noise; decreasing the spacing usually makes the derivative estimate noisier. |
| Choosing the step size | Plot error against h on a log–log scale |
Truncation error decreases with h, but subtractive cancellation and roundoff eventually increase the total error. |
| Function of several variables | One-dimensional stencils applied along each coordinate | Handle grid spacing and boundary conditions separately in each coordinate direction. |
| Mixed partial derivative | Tensor-product stencil or successive one-dimensional derivatives | Use consistent stencils, coordinate spacing, and boundary treatments when applying successive one-dimensional derivatives. |
| Analytic function that accepts complex arguments | Complex-step first derivative | Avoids subtractive cancellation, but every operation must preserve the complex perturbation. Operations such as abs, max, or real-valued conditional branches can invalidate the result. |
| Function implemented with operations supported by an automatic-differentiation tool | Automatic differentiation | No finite-difference step is required. The result differentiates the executed computer program, so numerical conditioning and unsupported operations still matter. |
Starting estimates for the step size
For double-precision calculations, let ε denote
machine epsilon and let L be a characteristic scale for
x, often max(1,|x|). Useful initial estimates
are:
- forward difference:
h ≈ ε1/2L, typically about10-8L; - centered first derivative:
h ≈ ε1/3L, typically about10-5L; - centered second derivative:
h ≈ ε1/4L, typically about10-4L; - fourth-order first derivative:
h ≈ ε1/5L, typically about10-3L; - fourth-order second derivative:
h ≈ ε1/6L, typically a few times10-3L.
These are starting values rather than universal optima; the best step
also depends on the function and its derivative scales. In floating-point
arithmetic, use the representable increment
hactual = (x + hrequested) - x.
A complex-step first derivative permits a much smaller step because it avoids subtractive cancellation. The practical lower limit depends on underflow and on whether every operation preserves the imaginary perturbation, so an extremely small universal value should not be prescribed.
Selected references and documentation
- B. Fornberg, “Generation of Finite Difference Formulas on Arbitrarily Spaced Grids,” Mathematics of Computation 51 (1988), 699–706. doi:10.1090/S0025-5718-1988-0935077-0 The standard algorithm for generating finite-difference weights on arbitrary grids.
- C. J. F. Ridders, “Accurate Computation of F′(x) and F″(x),” Advances in Engineering Software 4(2) (1982), 75–76. doi:10.1016/S0141-1195(82)80057-0 Develops extrapolated numerical differentiation with an error estimate.
- W. Squire and G. Trapp, “Using Complex Variables to Estimate Derivatives of Real Functions,” SIAM Review 40(1) (1998), 110–112. doi:10.1137/S003614459631241X A concise presentation of cancellation-free complex-step differentiation.
- J. R. R. A. Martins, P. Sturdza, and J. J. Alonso, “The Complex-Step Derivative Approximation,” ACM Transactions on Mathematical Software 29(3) (2003), 245–262. doi:10.1145/838250.838251 A detailed treatment of the complex-step method and its implementation.
- A. Savitzky and M. J. E. Golay, “Smoothing and Differentiation of Data by Simplified Least Squares Procedures,” Analytical Chemistry 36 (1964), 1627–1639. doi:10.1021/ac60214a047 Introduces local polynomial smoothing and differentiation filters for equally spaced data.
- J. N. Lyness and C. B. Moler, “Numerical Differentiation of Analytic Functions,” SIAM Journal on Numerical Analysis 4 (1967), 202–210. doi:10.1137/0704019 An early foundation for numerical differentiation using complex analytic function evaluations.
- A. Griewank and A. Walther, Evaluating Derivatives: Principles and Techniques of Algorithmic Differentiation, second edition, SIAM, 2008. doi:10.1137/1.9780898717761 A standard reference on automatic differentiation.
- MATLAB numerical integration and differentiation documentation
-
NumPy
gradientdocumentation - SciPy finite-difference differentiation documentation
- Boost.Math numerical differentiation documentation
- GNU Scientific Library: Numerical Differentiation