Numerical Differentiation
calculator.
Estimate a derivative from nearby function values, and see the true error of each.
- True error, central
- 5.7 × 10⁻⁶
- Best h, central
- …
A smaller h cuts the formula's own error until rounding error takes over. The error study below finds the best step.
Error against step size
Drag the point to change hMeasuring the error at every step size.
The slopes behind the estimates
f(x) = ln(x) around x = 1.8, with the slopes the estimates stand for. The forward slope is 0.5540180376, the central slope is 0.5555612713 and the exact tangent has slope 0.5555555556.
All methods at h = 0.01
| Method | Formula | Order | Estimate | Error |
|---|---|---|---|---|
| Forward difference[f(x + h) − f(x)] / hError O(h) | f(x + h) − f(x)h | O(h) | 0.55401803760.55401804 | 1.5 × 10⁻³ |
| Backward difference[f(x) − f(x − h)] / hError O(h) | f(x) − f(x − h)h | O(h) | 0.55710450490.5571045 | 1.5 × 10⁻³ |
| Central difference[f(x + h) − f(x − h)] / 2hError O(h²) | f(x + h) − f(x − h)2h | O(h²) | 0.55556127130.55556127 | 5.7 × 10⁻⁶ |
| Five-point central[f(x − 2h) − 8f(x − h) + 8f(x + h) − f(x + 2h)] / 12hError O(h⁴) | f(x − 2h) − 8f(x − h) + 8f(x + h) − f(x + 2h)12h | O(h⁴) | 0.55555555510.55555556 | 4.2 × 10⁻¹⁰ |
| Richardson extrapolation[4D(h/2) − D(h)] / 3, D = central differenceError O(h⁴) | 4D(h/2) − D(h)3 | O(h⁴) | 0.55555555550.55555556 | 2.6 × 10⁻¹¹ |
Working
- 1Function values either side of the pointf(1.79) = 0.5822156199, f(1.8) = 0.5877866649, f(1.81) = 0.5933268453
- 2Forward difference: the slope to one point ahead0.5933268453 − 0.58778666490.01= 0.5540180376
- 3Central difference: the slope across both sides0.5933268453 − 0.58221561990.02= 0.5555612713
- 4Richardson extrapolation: combine h and half of h to cancel the next error term4 × 0.5555569845 − 0.55556127133= 0.5555555555
- 5Second derivative from the same three values0.5933268453 − 2 × 0.5877866649 + 0.58221561990.012= −0.3086467384
- 6Check against the exact derivativef′(x) =1x, so f′(1.8) = 0.5555555556, a gap of 5.7 × 10⁻⁶
Measuring the error as h halves.
Measuring the error at every step size.
Slopes from a table of values
Have measurements and no formula? Paste them here. The first and last rows use a one-sided difference, the others a three-point formula that also handles uneven spacing. The example is a falling object, x in seconds and y in meters.
| # | x | y |
|---|---|---|
| 1 | ||
| 2 | ||
| 3 | ||
| 4 | ||
| 5 |
Slope at each row
| x | y | Slope | Rule |
|---|---|---|---|
| 0 | 0 | 4.9 | forward |
| 1 | 4.9 | 9.8 | central |
| 2 | 19.6 | 19.6 | central |
| 3 | 44.1 | 29.4 | central |
| 4 | 78.4 | 34.3 | backward |
How it's worked out
A derivative is a limit of slopes, so a slope across a small step h makes a good estimate. You use this when f comes from a simulation or a lookup and you have no formula to differentiate. Taylor's theorem tells you how large each estimate's error is.
The forward difference is off by about . That is first order: halve h, and the error halves. In the central difference, the even terms cancel, so the error shrinks with . That is second order, and it costs just one more evaluation of f.
N is the top of the five-point formula. Richardson extrapolation combines central differences at h and half of h so their main error terms cancel. It and the five-point formula are both fourth order.
You enter f as a formula, so the tool also finds the exact derivative with its own rules, and it subtracts to give you the true error of each estimate.
Worked examples
Ines compares formulas on ln x at 1.8
The exact derivative is , or 0.5555556. Ines tries three step sizes and reads the errors.
| h | Forward | Forward error | Central error |
|---|---|---|---|
| 0.1 | 0.5406722 | 1.5 × 10⁻² | 5.7 × 10⁻⁴ |
| 0.01 | 0.554018 | 1.5 × 10⁻³ | 5.7 × 10⁻⁶ |
| 0.001 | 0.5554013 | 1.5 × 10⁻⁴ | 5.7 × 10⁻⁸ |
Each cut of h by 10 cuts the forward error by about 10 and the central error by about 100, just as their orders predict.
Tom watches the order show up on sin x
Tom has written his own solver and wants to be sure his formula has the order he claims. He tests sin x at 1, halving h from 0.1.
| h | Forward | Central | Five-point |
|---|---|---|---|
| 0.1 | 4.3 × 10⁻² | 9 × 10⁻⁴ | 1.8 × 10⁻⁶ |
| 0.05 | 2.1 × 10⁻² | 2.3 × 10⁻⁴ | 1.1 × 10⁻⁷ |
| 0.025 | 1.1 × 10⁻² | 5.6 × 10⁻⁵ | 7 × 10⁻⁹ |
| 0.0125 | 5.3 × 10⁻³ | 1.4 × 10⁻⁵ | 4.4 × 10⁻¹⁰ |
From the first row to the second, the forward error shrinks 2.02 times, the central 4 times and the five-point 15.99 times. Those sit close to the 2, 4 and 16 that first, second and fourth order predict.
Kai looks for the best step size for eˣ at 1
Kai expects smaller h to always win. The chart shows otherwise. The forward difference is best near h = 1 × 10⁻⁸, the central near 1 × 10⁻⁵ and the five-point near 1 × 10⁻³, each among the powers of 10 tested. Below those, the errors climb again.
A better formula also wants a larger h, since its own error drops so quickly that rounding catches up sooner.
Reading your result
Why the error curve has a V shape. Two errors compete. The formula's own cutoff, called truncation error, falls as h shrinks. Rounding error rises, because f(x + h) and f(x) become nearly equal and subtracting them loses digits, and then you divide by a tiny h. Too large a step and truncation wins. Too small and rounding wins.
Where the bottom of the V sits. With machine precision ε, about 2.2 × 10⁻¹⁶, the best h is of order for the forward difference and for the central. Alexanderian's note works out why. That is about 10⁻⁸ and 10⁻⁵ to 10⁻⁶ when x and f have a size near 1. Scale h with x when x is much larger or smaller than that.
The second derivative is more delicate. It divides by h², so rounding hits it harder. For eˣ at 1, h = 0.01 gives an error of 2.3 × 10⁻⁵, and h = 10⁻⁶ gives 4.6 × 10⁻⁴. Stay with a larger h for f″ than you would use for f′.
Common mistakes
- Making h as small as you can. Below the best step, each cut makes the estimate worse, and at h = 10⁻¹² a forward difference may keep only a few correct digits.
- Trusting a difference at a corner. At x = 0, |x| has no derivative. The central difference still prints 0, since the two sides cancel, while forward says 1 and backward says −1. Disagreement like that is your warning sign.
- Using one-sided differences when you could use both sides. Forward and backward are for the edge of a domain or data set, where one side is missing. Everywhere else, the central difference is far more accurate for almost the same work.
- Using a tiny h on measured data. Noise plays the part of rounding error here, and it is far larger, so differences across close points mostly measure the noise.