A13 · Lesson 14 · Numerical differentiation
A smaller step isn't always a better derivative
The derivative is a limit as the step shrinks to zero, so it seems the smaller the step, the better. On a computer that's only half true. A difference quotient makes two errors: truncation, from using a finite step, which shrinks with h, and rounding, from subtracting two nearly equal function values, which grows as . Their sum has a minimum, and with plain double precision the best forward difference gets only about 8 digits right. Knowing where that minimum is, and the tricks that move it (central differences, Richardson extrapolation, the complex step), matters every time a solver needs a Jacobian or firmware differentiates a sensor signal.
- Each curve is the actual error of a derivative estimate of at x = 1, computed in double precision in your browser, against the step h.
- The forward difference falls as h until about , then rises again: to the left, rounding wins. The central difference falls faster (as ) and bottoms out near , with about 11 good digits.
- The complex step, , never subtracts, so it keeps improving down to the smallest h: it's exact to rounding from about on.
Predict
A forward difference of at x = 1 uses . About how accurate is it?
Wrong in the fourth digit: the error is . The two values and agree in their first 12 digits, so their difference keeps only about 4, and dividing by h doesn't restore them. At the error is , close to the best possible.
Name it
Difference formulas and their errors
With the rounding of each f value bounded by (u = 1.1 × 10⁻¹⁶ in double):
| Formula | Truncation | Rounding | Best h | Best error |
|---|---|---|---|---|
| forward | ||||
| central | ||||
| second |
(for f and its derivatives of similar size; scale h with the size of x in general).
Richardson extrapolation
The central difference's error is . Combining two steps cancels the term:
which is fourth-order accurate. Repeating the idea gives Romberg integration (lesson 19).
The complex step
For a real-analytic f, , so
with error and no subtraction: take and get f′ to full precision. It needs code that accepts complex inputs.
Noisy data
Differencing samples with noise σ adds noise of about to the derivative: the finer the sampling, the worse. Filter before differentiating, or fit a smooth curve and differentiate that.
Worked example
Estimate at 1 with central differences at h = 0.1 and 0.05, then extrapolate.
Show the solution
- Central differences. with errors and : a quarter, as predicts.
- Richardson extrapolation.
- Compare with e. Against e = 2.7182818 the error is .
Answer extrapolation gives 2.7182813, within of e.
Worked example
A 12-bit ADC sampled at 1 MHz has 0.5 mV of noise. How noisy is a forward-difference estimate of dV/dt?
Show the solution
- The noise in one difference.
- Compare with the signal. If the signal's slopes are a few V/ms (thousands of V/s), the estimate is mostly noise.
- Average. Averaging over 10 samples (h = 10 µs) cuts it to 71 V/s.
Answer about 707 V/s of noise, or 71 V/s when averaged over 10 samples.
Worked example
A position sensor is read every 1 ms with 1 µm of noise. How noisy is acceleration estimated by the second difference?
Show the solution
- The second difference.
- Noise in the numerator. With independent noise σ in each reading, it is
- Divide by .
- What that means. That's a quarter of g, from a sensor good to a micrometer. Second derivatives amplify noise as , so in practice one smooths first, fits a model, or measures acceleration directly.
Answer about 2.4 m/s² of noise, a quarter of g.
Check
What does the forward difference give for the derivative of at x = 2 with h = 0.1?
Check
And the central difference for at x = 2 with h = 0.1?
Check
In a format with 53 significant bits, about what step gives the most accurate forward difference (take )?
Check
Take central differences of at x = 1 with h = 0.1 and h = 0.05. What's Richardson's correction, , which is added to D(0.05)? (Carry at least 8 digits in D.)
Check
Samples with 0.5 mV of noise, 1 µs apart, are differenced. About how much noise does the derivative estimate carry?
Write units the way you normally would: 1.5 V/s, 1500 mV/s and 0.0015 kV/s are the same answer.
Check
Why can the complex-step derivative use when a forward difference can't?
Check
A position is read every 1 ms with 1 µm of independent noise.
(a)
How noisy is the acceleration from the second difference?
Write units the way you normally would: 1.5 m/s^2, 1500 mm/s^2 and 0.0015 km/s^2 are the same answer.
(b)
How noisy is the velocity from the central difference ?
Check
Apply the second difference to .
Step 1
What does give exactly?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
Expanding , what is the leading error?
Explain why
Why does making the step smaller eventually make a difference quotient worse?
A difference quotient has two errors. The truncation error, from replacing the limit by a finite step, shrinks as h shrinks: as h for a forward difference, as for a central one. The rounding error comes from subtracting f(x + h) and f(x), which agree in more and more leading digits as h shrinks; each carries a rounding error of about , which the subtraction leaves untouched and the division by h magnifies, so it grows as . The total is smallest where the two balance, near for a forward difference, and below that the rounding term takes over and the answer gets worse.
Practice
Keep practicing
9 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.