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 (x, f(x)) and (x + h, f(x + h)). Choosing a small number h, h represents a small change in x, and it can be either positive or negative. The slope of this line is f(x+h)−f(x)h. 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 f(x) about the base point x to give f(x+h)=f(x)+hf′(x)+h22f″(c) for some c between x and x+h. Rearranging gives f′(x)=f(x+h)−f(x)h⏟Slope of secant line−h2f″(c)⏟Error term. The slope of this secant line differs from the slope of the tangent line by an amount that is approximately proportional to h. As h 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 f at x is the limit of the value of the difference quotient as the secant lines get closer and closer to being a tangent line: f′(x)=limh→0f(x+h)−f(x)h. Since immediately substituting 0 for h results in 00 indeterminate form, calculating the derivative directly can be unintuitive. Equivalently, the slope could be estimated by employing positions x − h and x. Another two-point formula is to compute the slope of a nearby secant line through the points (x − h, f(x − h)) and (x + h, f(x + h)). The slope of this line is f(x+h)−f(x−h)2h. 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 h2. Hence for small values of h this is a more accurate approximation to the tangent line than the one-sided estimation. However, although the slope is being computed at x, the value of the function at x is not involved. The estimation error is given by R=−f(3)(c)6h2, where c is some point between x−h and x+h. 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 h = 0.001.
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, h. To illustrate, consider the two-point approximation formula with error term: f′(x)=f(x+h)−f(x−h)2h−f(3)(c)6h2 where c is some point between x−h and x+h. Let e(x) denote the roundoff error encountered when evaluating the function f(x) and f̂(x) denote the computed value of f(x). Therefore, f(x)=f̂(x)+e(x). The total error in the approximation is f′(x)−f̂(x+h)−f̂(x−h)2h=e(x+h)−e(x−h)2h⏟Roundoff error−f(3)(c)6h2⏟Truncation error. Assuming that the roundoff errors are bounded by some number ε>0 and the third derivative of f(x) is bounded by some number M>0, we get |f′(x)−f̂(x+h)−f̂(x−h)2h|≤εh+h26M. To reduce the truncation error h26M, we must reduce h. But as h is reduced, the roundoff error εh increases. Due to the need to divide by the small value h, all the finite-difference formulae for numerical differentiation are similarly ill-conditioned. If instead the derivative is approximated using f′(x)=f(x+h)−f(x)h and we assume the relative approximation error due to rounding is bounded by machine epsilon and the values |f″(x)| and |f(x)| are bounded on [x0,x0+h] by M1and M2 respectively it can be shown that:|f′(x0)−f̂(x0+h)−f̂(x0)h|≤h2M1+2εhM2By 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 M1 and M2. Instead, if an approximate upper bound is used - one where M1and M2 are replaced by |f″(x0)| and |f(x0)| the error can be estimated as:e(h)=h2|f″(x0)|+2εh|f(x0)|By minimizing e(h) an estimate for the optimal step size h can be found: h=2ε|f(x)f″(x)|(though not when f″(x)=0). If no information about the function f is known, sometimes the approximation |f(x)f″(x)|≈x is used giving a choice for h that is small without producing a large rounding error: εx, where the machine epsilon ε is typically of the order of 2.2×10−16 for double precision. 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.
Example To demonstrate this difficulty, consider approximating the derivative of the function f(x)=2x1+x at the point x0=9. In this case, we can calculate f′(x)=2+x(1+x)2 which gives f′(9)=2+9(1+9)2=2+3(1+3)2=516=0.3125. 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 10−4, after which the absolute error steadily increases as the roundoff errors dominate calcuations.
For computer calculations the problems are exacerbated because, although x necessarily holds a representable floating-point number in some precision (32 or 64-bit, etc.), x + h almost certainly will not be exactly representable in that precision. This means that x + h will be changed (by rounding or truncation) to a nearby machine-representable number, with the consequence that (x + h) − x will not equal h; the two function evaluations will not be exactly h 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 h = 0.1 will not be a round number in binary; it is 0.000110011001100...2 A possible approach is as follows:
… excerpt ends here. Continue reading the full article.


