Numerical differentiation and competing errors
Derive difference formulas, balance truncation against uncertainty, and recognize when smaller steps become worse.
On this page
The question for today
What you’ll understand
- Derive first- and second-order derivative approximations from Taylor expansions.
- Quantify how function-value uncertainty is amplified by division by a step.
- Use refinement experiments without mistaking an asymptotic estimate for a guarantee.
A derivative is a limit, but a program must choose a finite step. Making that step smaller reduces one error while increasing another: nearby function values become harder to distinguish, and their uncertainty is divided by the shrinking interval. Numerical differentiation therefore teaches a general principle of scientific computation: refinement is useful only within a model of all important errors. We will derive the central difference, check a polynomial example exactly, and distinguish finite differences from automatic and complex-step differentiation. Each method has assumptions that determine when its output means the derivative we intended.
Taylor expansion explains the stencil
The forward difference subtracts f(x) from f(x+h) and divides by h. A Taylor expansion shows a leading error proportional to h when the second derivative is controlled. Sampling symmetrically on both sides cancels the even terms in the numerator, producing a central difference whose leading error is proportional to h squared. This cancellation is mathematical and useful, but evaluating the numerator still involves subtraction of nearby computed values. The truncation argument alone says nothing about their rounding errors.
The error order assumes enough smoothness near the evaluation point and a fixed function as h decreases. At a kink, a central stencil may converge to an average-like number even though the classical derivative does not exist. Near a domain boundary, a symmetric sample may be unavailable; one-sided higher-order formulas are possible but have different coefficients and uncertainty amplification. A stencil is not a universal replacement for differentiability. Verify the domain and the solution concept before interpreting the returned number.
Smaller steps amplify function-value error
Suppose each function evaluation has an absolute uncertainty bounded by ε. In a central difference, the worst numerator perturbation is at most 2ε, giving a derivative uncertainty at most ε divided by |h|. If a truncation bound behaves like C times h squared, the combined upper model has a bowl-shaped dependence on positive h. The best scale depends on the third derivative and on evaluation accuracy. Unit roundoff alone does not specify ε when the function evaluation itself has cancellation, iteration error, or noisy observations.
Differentiation of measured data is especially sensitive because refining the sample interval does not necessarily reduce measurement noise. Smoothing or fitting a local model can be sensible, but then bandwidth and model bias become part of the analysis. Report those choices. A graph that looks smooth after aggressive filtering does not establish that its derivative is correct. The desired observable may instead be an integrated quantity, which is often much less sensitive to high-frequency noise.
Refinement and alternatives need interpretation
In a clean second-order regime, halving h should reduce the leading truncation error by roughly four. Comparing D at h and h/2 estimates the smaller-step error from their difference divided by three, provided the leading expansion dominates. A plateau or reversal suggests rounding, noise, nonsmoothness, or an implementation issue. Two agreeing approximations can still share a bias, so an independently solvable test function and a range of step sizes are more informative than one apparently stable decimal string.
Automatic differentiation propagates derivatives through the executed operations using the chain rule, avoiding a finite-difference step. It differentiates that program along its executed branches; it does not automatically validate a physical model or repair nondifferentiable logic. Complex-step differentiation can avoid subtractive cancellation for functions admitting a suitable analytic complex extension, but operations such as absolute value or branch-dependent clipping can invalidate the argument. Choose the technique according to the mathematical function and the implementation, rather than treating every derivative API as interchangeable.
Work it through
Central differences on a cubic
Approximate f′(1) for f(x)=x³ using h=0.1 and h=0.05, then interpret an absolute evaluation uncertainty of 10⁻⁸.
The exact derivative is 3x², hence f′(1)=3. With h=0.1, the sampled values are 1.331 and 0.729; their difference divided by 0.2 is 3.01.
With h=0.05, the values are 1.157625 and 0.857375; dividing their difference 0.30025 by 0.1 gives 3.0025. The errors 0.01 and 0.0025 have the predicted factor of four.
The cubic has an exact central-difference error h². The observed difference 3.01−3.0025=0.0075 divided by three estimates the smaller error 0.0025 exactly in exact arithmetic.
If each value has absolute uncertainty at most 10⁻⁸, the derivative uncertainty bounds are 10⁻⁷ and 2×10⁻⁷ for these steps. At h=10⁻⁸ that bound becomes one, despite the tiny truncation error. Shrinking h indefinitely is therefore unjustified.
Refinement improves the truncation error initially but eventually magnifies evaluation uncertainty. The correct step depends on both effects.
The living notebook
Explore the model
Interactive lab · function
Use an exact tangent as a comparison target
For the lab's y=a x², set a=1 and x=1. Predict the central difference for any symmetric step: it equals the displayed tangent slope two in exact arithmetic. Change a and x to repeat the check. The lab does not draw finite-difference samples or simulate roundoff; it supplies only the exact quadratic value, slope, and area.
- y at chosen x
- 0.608
- Tangent slope · 2ax
- 1.35
- Signed area · ax³/3
- 0.182
This lab uses only y = ax². Playback traces the curve within the fixed integration interval from 0 to x; it does not estimate numerical error or change the chosen point. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.
Look forA derivative method should reproduce a tractable target before it is trusted on a function with no known derivative.
Pause & reason
Check your understanding
Your turn
Try it yourself
For f(x)=x³ at x=2, calculate the central difference with h=0.2 and its error relative to the exact derivative.
Take a moment to make a prediction or work it out on paper. Open a hint when you need one.
Give me a hint
Expand (x+h)³−(x−h)³ before inserting numbers.
Compare with the solution
The quotient is 3x²+h², so it equals 12+0.04=12.04. Directly, 2.2³=10.648 and 1.8³=5.832; their difference 4.816 divided by 0.4 is 12.04. The exact derivative is 12 and the truncation error is 0.04, assuming exact function values.
Keep the idea
Take it with you
- A stencil's error order depends on smoothness and its sampling geometry.
- Dividing by a small step amplifies uncertainty in function values.
- Refinement, automatic differentiation, and complex-step methods answer different implementation questions.
Read further
Original sources and deeper explanations to follow at your own pace.
Optional progress, saved in this browser when available. Revisit any lesson at any time.
Lesson not marked complete.