Numerical differentiation
In numerical analysis, numerical differentiation algorithms estimate the derivative of a mathematical function or subroutine using values of the function. Unlike analytical differentiation, which provides exact expressions for derivatives, numerical differentiation relies on the function's values at a set of discrete points to estimate the derivative's value at those points or at intermediate points. This approach is particularly useful when dealing with data obtained from experiments, simulations, or situations where the function is defined only at specific intervals.
Finite differences
The simplest method is to use finite difference approximations.
A simple two-point estimation is to compute the slope of a nearby secant line through the points Template:Math and Template:Math.[1] Choosing a small number Template:Mvar, Template:Mvar represents a small change in Template:Mvar, and it can be either positive or negative. The slope of this line is This expression is Newton's difference quotient (also known as a first-order divided difference).
To obtain an error estimate for this approximation, one can use Taylor expansion of about the base point to give for some between and . Rearranging gives The slope of this secant line differs from the slope of the tangent line by an amount that is approximately proportional to Template:Mvar. As Template:Mvar approaches zero, the slope of the secant line approaches the slope of the tangent line and the error term vanishes. Therefore, the true derivative of Template:Math at Template:Mvar is the limit of the value of the difference quotient as the secant lines get closer and closer to being a tangent line:
Since immediately substituting 0 for Template:Mvar results in indeterminate form, calculating the derivative directly can be unintuitive.
Equivalently, the slope could be estimated by employing positions Template:Math and Template:Mvar.
Another two-point formula is to compute the slope of a nearby secant line through the points Template:Math and Template:Math. The slope of this line is
This formula is known as the symmetric difference quotient. In this case the first-order errors cancel, so the slope of these secant lines differ from the slope of the tangent line by an amount that is approximately proportional to . Hence for small values of Template:Mvar this is a more accurate approximation to the tangent line than the one-sided estimation. However, although the slope is being computed at Template:Mvar, the value of the function at Template:Mvar is not involved.
The estimation error is given by where is some point between and . This error does not include the rounding error due to numbers being represented and calculations being performed in limited precision.
The symmetric difference quotient is employed as the method of approximating the derivative in a number of calculators, including TI-82, TI-83, TI-84, TI-85, all of which use this method with Template:Math.[2][3]
Step size

An important consideration in practice when the function is calculated using floating-point arithmetic of finite precision is the choice of step size, Template:Mvar. To illustrate, consider the two-point approximation formula with error term:
where is some point between and . Let denote the roundoff error encountered when evaluating the function and denote the computed value of . Therefore, The total error in the approximation is Assuming that the roundoff errors are bounded by some number and the third derivative of is bounded by some number , we get
To reduce the truncation error , we must reduce Template:Mvar. But as Template:Mvar is reduced, the roundoff error increases. Due to the need to divide by the small value Template:Mvar, all the finite-difference formulae for numerical differentiation are similarly ill-conditioned.[4]
If instead the derivative is approximated using and we assume the relative approximation error due to rounding is bounded by machine epsilon and the values and are bounded on by and respectively it can be shown that:[5]By minimizing this upper bound an estimate for the optimal step size can be obtained. However, employing this method would require the knowledge of the bounds and . Instead, if an approximate upper bound is used - one where and are replaced by and the error can be estimated as:By minimizing an estimate for the optimal step size Template:Mvar can be found:[5] (though not when ). If no information about the function is known, sometimes the approximation is used giving a choice for Template:Mvar that is small without producing a large rounding error: , where the machine epsilon Template:Mvar is typically of the order of Template:Val for double precision.[6] In practice, one cannot compute the value that minimizes the above error bounds and therefore cannot compute an approximation of the optimal step size without information about higher-order derivatives.[7]
Example
To demonstrate this difficulty, consider approximating the derivative of the function at the point . In this case, we can calculate which gives Using 64-bit floating point numbers, the following approximations are generated with the two-point approximation formula and increasingly smaller step sizes. The smallest absolute error is produced for a step size of , after which the absolute error steadily increases as the roundoff errors dominate calcuations.
| Step Size (h) | Approximation | Absolute Error |
|---|---|---|
For computer calculations the problems are exacerbated because, although Template:Mvar necessarily holds a representable floating-point number in some precision (32 or 64-bit, etc.), Template:Math almost certainly will not be exactly representable in that precision. This means that Template:Math will be changed (by rounding or truncation) to a nearby machine-representable number, with the consequence that Template:Math will not equal Template:Mvar; the two function evaluations will not be exactly Template:Mvar apart. In this regard, since most decimal fractions are recurring sequences in binary (just as 1/3 is in decimal) a seemingly round step such as Template:Math will not be a round number in binary; it is 0.000110011001100...2 A possible approach is as follows:
h := sqrt(eps) * x; xph := x + h; dx := xph - x; slope := (F(xph) - F(x)) / dx;
However, with computers, compiler optimization facilities may fail to attend to the details of actual computer arithmetic and instead apply the axioms of mathematics to deduce that Template:Math and Template:Mvar are the same. With C and similar languages, a directive that Template:Math is a volatile variable will prevent this.
Three Point methods
Template:Further To obtain more general derivative approximation formulas for some function , let be a positive number close to zero. The Taylor expansion of about the base point is Template:NumBlk
Replacing by gives Template:NumBlk
Multiplying identity (Template:EquationNote) by 4 gives Template:NumBlk
Subtracting identity (Template:EquationNote) from (Template:EquationNote) eliminates the term:
which can be written as
Rearranging terms gives
which is called the three-point forward difference formula for the derivative. Using a similar approach, one can show
which is called the three-point central difference formula, and which is called the three-point backward difference formula.
By a similar approach, the five point midpoint approximation formula can be derived as:[8]
Numerical Example
Consider approximating the derivative of at the point . Since , the exact value is
| Formula | Step Size (h) | Approximation | Absolute Error |
|---|---|---|---|
| Three-point forward difference formula | |||
| Three-point backward difference formula | |||
| Three-point central difference formula | |||
Algorithms
The following pseudocode describes the various three point differentiation formulas.
Three-Point Forward Difference
input: Real valued function, f The point at which to approximate the derivative, a The step size, h output: An approximation of return ((-3/2)*f(a) + 2*f(a+h) - (1/2)*f(a+2*h)) / h
Three-Point Central Difference
input: Real valued function, f The point at which to approximate the derivative, a The step size, h output: An approximation of return ((-1/2)*f(a-h) + (1/2)*f(a+h)) / h
Three-Point Backward Difference
input: Real valued function, f The point at which to approximate the derivative, a The step size, h output: An approximation of return ((1/2)*f(a-2*h) - 2*f(a-h) + (3/2)*f(a)) / h
Higher derivatives
Using Taylor Series, it is possible to derive formulas to approximate the second (and higher order) derivatives of a general function. For a function and some number , expanding it about and gives
and where . Adding these two equations gives
If is continuous on , then is between and . The Intermediate Value Theorem guarantees a number, say , between and . We can therefore write
where .
Numerical Example
Consider approximating the second derivative of the function at the point .
Since , the exact value is .
| Step Size (h) | Approximation | Absolute Error |
|---|---|---|
Arbitrary Derivatives
Using Newton's difference quotient, the following can be shown[9] (for Template:Math):
Complex-variable methods
The classical finite-difference approximations for numerical differentiation are ill-conditioned. However, if is a holomorphic function, real-valued on the real line, which can be evaluated at points in the complex plane near , then there are stable methods. For example,[10] the first derivative can be calculated by the complex-step derivative formula:[11][12][13]
The recommended step size to obtain accurate derivatives for a range of conditions is .[14] This formula can be obtained by Taylor series expansion:
The complex-step derivative formula is only valid for calculating first-order derivatives. A generalization of the above for calculating derivatives of any order employs multicomplex numbers, resulting in multicomplex derivatives.[15][16][17] where the denote the multicomplex imaginary units; . The operator extracts the th component of a multicomplex number of level , e.g., extracts the real component and extracts the last, "most imaginary" component. The method can be applied to mixed derivatives, e.g. for a second-order derivative
A C++ implementation of multicomplex arithmetics is available.[18]
In general, derivatives of any order can be calculated using Cauchy's integral formula:[19] where the integration is done numerically.
Using complex variables for numerical differentiation was started by Lyness and Moler in 1967.[20] Their algorithm is applicable to higher-order derivatives.
A method based on numerical inversion of a complex Laplace transform was developed by Abate and Dubner.[21] An algorithm that can be used without requiring knowledge about the method or the character of the function was developed by Fornberg.[4]
Differential quadrature
Differential quadrature is the approximation of derivatives by using weighted sums of function values.[22][23] Differential quadrature is of practical interest because its allows one to compute derivatives from noisy data. The name is in analogy with quadrature, meaning numerical integration, where weighted sums are used in methods such as Simpson's rule or the trapezoidal rule. There are various methods for determining the weight coefficients, for example, the Savitzky–Golay filter. Differential quadrature is used to solve partial differential equations. There are further methods for computing derivatives from noisy data.[24]
See also
- Template:Annotated link
- Template:Annotated link
- List of numerical-analysis software
- Template:Annotated link
- Template:Annotated link
- Template:Annotated link
References
External links
- Numerical Differentiation from wolfram.com
- NAG Library numerical differentiation routines
- Boost. Math numerical differentiation, including finite differencing and the complex step derivative
- Differentiation With(out) a Difference by Nicholas Higham, SIAM News.
- ↑ Richard L. Burden, J. Douglas Faires (2000), Numerical Analysis, (7th Ed), Brooks/Cole. Template:Isbn.
- ↑ Template:Cite book
- ↑ Template:Cite book
- ↑ 4.0 4.1 Numerical Differentiation of Analytic Functions, B Fornberg – ACM Transactions on Mathematical Software (TOMS), 1981.
- ↑ 5.0 5.1 Template:Cite book
- ↑ Following Numerical Recipes in C, Chapter 5.7.
- ↑ Richard L. Burden, J. Douglas Faires (2000), Numerical Analysis, (7th Ed), Brooks/Cole. Template:Isbn.
- ↑ Abramowitz & Stegun, Table 25.2.
- ↑ Template:Cite book
- ↑ Using Complex Variables to Estimate Derivatives of Real Functions, W. Squire, G. Trapp – SIAM REVIEW, 1998.
- ↑ Template:Cite journal
- ↑ Differentiation With(out) a Difference by Nicholas Higham
- ↑ article from MathWorks blog, posted by Cleve Moler
- ↑ Template:Cite book
- ↑ Template:Cite web
- ↑ Template:Cite journal
- ↑ Template:Cite web
- ↑ Template:Cite web
- ↑ Ablowitz, M. J., Fokas, A. S.,(2003). Complex variables: introduction and applications. Cambridge University Press. Check theorem 2.6.2
- ↑ Template:Cite journal
- ↑ Template:Cite journal
- ↑ Differential Quadrature and Its Application in Engineering: Engineering Applications, Chang Shu, Springer, 2000, Template:Isbn.
- ↑ Advanced Differential Quadrature Methods, Yingyan Zhang, CRC Press, 2009, Template:Isbn.
- ↑ Template:Cite journal