A finite difference with step h goes wrong in two separate ways. The truncation error comes from stopping at a finite h instead of taking the limit; it shrinks as h shrinks. The rounding error comes from the computer keeping only about 16 digits; it grows as h shrinks.
Both are in the units of the derivative: output units per input unit. This lesson measures each one, then picks the h that keeps their sum smallest.
Truncation error, from Taylor polynomials
Lesson 2.7.1 wrote the Taylor polynomial about a point a in step form: f(a+h)≈f(a)+f′(a)h+2f′′(a)h2+⋯. Take the point a to be x itself, and go one term further:
f(x+h)=f(x)+f′(x)h+21f′′(x)h2+61f′′′(x)h3+⋯
A step to the left replaces h by −h, which flips the sign of every odd power:
f(x−h)=f(x)−f′(x)h+21f′′(x)h2−61f′′′(x)h3+⋯
Each finite difference of lesson 2.2.2 combines these two lines.
Forward. Subtract f(x) from the first line and divide by h. That leaves f′(x)+21f′′(x)h+61f′′′(x)h2+⋯.
Backward. The same with the second line leaves f′(x)−21f′′(x)h+61f′′′(x)h2−⋯.
Central. Subtract the second line from the first. The f(x) and h2 terms cancel, and dividing by 2h leaves f′(x)+61f′′′(x)h2+⋯.
The estimate minus the true f′(x), to leading order: the first term of the series that the formula leaves over. Forward and backward errors shrink like h, the central error like h2.
For x3 at 1, f′′(1)=6 and f′′′(1)=6. The forward error is 21⋅6h=3h and the central error 61⋅6h2=h2: lesson 2.2.2's 3h and h2. A cubic's series stops at h3, so the forward difference is exactly 3+3h+h2; the extra h2 is the 61f′′′(x)h2 term.
A second example: ex at 1
Every derivative of ex is ex, so at 1 they all equal e≈2.718282. The rule predicts a forward error of 2eh≈1.359h and a central error of 6eh2≈0.453h2.
step h
0.1
0.01
0.001
forward error
0.14056
0.013637
0.00136
predicted, 2eh
0.135914
0.013591
0.001359
central error
4.533×10−3
4.530×10−5
4.530×10−7
predicted, 6eh2
4.530×10−3
4.530×10−5
4.530×10−7
The predictions get better as h shrinks, because the terms they leave out shrink faster.
The sign of the error
The error has the sign of the derivative in its term. Where f′′>0, the curve bends up (lesson 2.4.2): the forward difference is too large and the backward one too small.
lnx bends down, with f′′(2)=−41. So at 2 its forward differences fall short, by about 81h: −0.012098, −0.001246 and −0.000125 for h = 0.1, 0.01 and 0.001. Its third derivative x32 is 41 at 2, so the central difference is too large, by 4.17×10−4 at h=0.1, as 241h2 predicts.
A zero term makes a difference better than its rule says. At 0, sinx has f′′=0, so its forward difference hsinh is off by only about −61h2: 0.998334 at h=0.1, as in lesson 2.3.5. A quadratic has f′′′=0 everywhere, so its central difference is exact.
Where it breaks: a corner inside the step
The series needs f to be smooth all the way from x−h to x+h. Take ∣x∣ at x=0.001, where the slope is 1, with h=0.01. The points −0.009 and 0.011 straddle the corner at 0, and the central difference gives 0.1.
The rule predicts no error at all, since f′′′=0 on each side of the corner. The estimate has measured the corner, not the slope. With h=0.0001 both points stay right of the corner, and it gives 1.
Case study 2.8.4 meets the same trap at an option's strike.
Try it
The first loop prints the forward and central errors of ex at 1 beside the predictions. The second prints the errors of lnx at 2, signs included.
The last line straddles the corner of ∣x∣, then keeps to its smooth side.
⌘/Ctrl + Enter runs the cell
Rounding error
A float keeps 53 binary digits, about 16 decimal digits (lesson 2.1.1). The gap between 1 and the next float up is the machine epsilon, ε≈2.2×10−16. Storing a number moves it by at most about 21ε of its size.
Even the step is rounded. 1+10−15 is stored as 1+5ε, which is 1+1.11×10−15. That is why lesson 2.1.1's table gave 2.22044605 instead of 2 at h=10−15: the step really taken was 11% too long.
Subtracting two nearly equal numbers keeps their rounding errors but cancels their leading digits. That was the trap of lesson 2.1.5: for huge n, 1+r/n kept only a few digits of r/n, and math.log1p avoided the subtraction. A finite difference cannot avoid it, since its top, f(x+h)−f(x), is exactly such a subtraction.
Each value of f is off by up to about 21ε∣f(x)∣, so the top is off by up to about ε∣f(x)∣. Dividing by h gives the rule.
Rule: Rounding error
forward: about hε∣f(x)∣central: about 2hε∣f(x)∣
A ceiling, not an exact size: the actual rounding error jumps around below it. It grows as h shrinks: a step 10 times smaller allows a rounding error 10 times larger. A large ∣f(x)∣ raises it too.
For ex at 1 the forward ceiling hεe is 6.0×10−6 at h=10−10 and 6.0×10−4 at h=10−12. The measured errors are 1.5×10−6 and 4.3×10−4: under the ceiling, and far above the truncation errors 1.4×10−10 and 1.4×10−12.
Try it
sys.float_info.epsilon is ε. The second line shows the stored step: (1+10−15)−1 is exactly 5ε.
The loop prints the forward error of ex at 1 for tiny steps, beside the ceiling hεe and the truncation error 2eh.
⌘/Ctrl + Enter runs the cell
The total error and the best step
The total error is at most about the truncation error plus the rounding ceiling. For the forward difference that is 21∣f′′(x)∣h+hε∣f(x)∣. The first part falls as h shrinks and the second rises, so the total is smallest close to where they are equal.
Set the truncation error equal to the rounding ceiling and solve for h. When f and its derivatives have about the same size, these are about ε≈1.5×10−8 and ε1/3≈6×10−6.
For ex at 1 both ratios are 1, so the best steps are 2ε≈2.1×10−8 and (3ε)1/3≈8.7×10−6. These match lesson 2.2.2's 10−8 and 10−5 for x3 at 1. At those steps the errors are 5.0×10−8 (forward) and 3.7×10−11 (central).
So the central difference wins twice: its error falls faster, and at its best it is about a thousand times more accurate.
step h
forward error
central error
10−2
1.4×10−2
4.5×10−5
10−4
1.4×10−4
4.5×10−9
10−6
1.4×10−6
1.6×10−10
10−8
6.6×10−9
6.6×10−9
10−10
1.5×10−6
6.7×10−7
Try it
The loop builds the table. The last two lines compute the best steps for ex at 1 and the errors there.
⌘/Ctrl + Enter runs the cell
Scale the step to the function
The best-step rule needs f′′ or f′′′, the very things you are avoiding. What matters is a ratio such as ∣f′′′(x)∣∣f(x)∣, which has the units of the input cubed. Its cube root is s, the scale of f: roughly the distance over which f changes by its own size.
Rule: A default step
forward: h=εscentral: h=ε1/3s
For powers and logarithms of x, the scale is s=∣x∣ (use 1 at x=0). For ex and sinx it is s=1, wherever x is.
Two examples show both cases. Below, the relative error is the error as a fraction of the true slope.
x at 106. The slope is 0.0005 and s=106, so the central step is ε1/3×106≈6.06. Its relative error is 8.5×10−14. The unscaled h=10−5 gives 1.1×10−5.
ex at 30. Here f changes by its own size over a distance of about 1, whatever x is. The unscaled h=10−5 has relative error 1.7×10−11. Scaling by x gives h=1.8×10−4, and 5.5×10−9.
The step must also keep x−h where f is defined. For x at 10−6, h=10−5 puts x−h below 0, and Python raises ValueError. The scaled step, 6×10−12, gives a relative error of 2.4×10−11.
At the other extreme, a step below ε∣x∣ vanishes. 108+10−9 is stored as 108, so the forward difference of x2 at 108 with h=10−9 is 0.
Try it
relative is the relative error. Each line compares the unscaled step 10−5 with a scaled one, for x at 106 and ex at 30.
Then x near the edge of its domain, and a step too small for x=108.
⌘/Ctrl + Enter runs the cell
The second derivative
Lesson 2.4.2 estimated f′′ by the second central difference. Add the two Taylor lines, each taken one term further, to 241f(4)(x)h4, where f(4) is the fourth derivative. The odd powers cancel:
f(x+h)−2f(x)+f(x−h)=f′′(x)h2+121f(4)(x)h4+⋯
So after dividing by h2, the truncation error is 121f(4)(x)h2. The top combines three values of f, one of them doubled, so its rounding can reach about 2ε∣f(x)∣, and that is divided by h2.
Rule: Second difference: errors and best step
truncation: 121f(4)(x)h2rounding: about h22ε∣f(x)∣best: h≈(∣f(4)(x)∣24ε∣f(x)∣)1/4
When f and f(4) have about the same size, the best step is about ε1/4≈10−4, times the scale s as before.
The best step is larger because the rounding ceiling grows 100 times for each step 10 times smaller, so it overtakes truncation sooner. For ex at 1 the best step is (24ε)1/4≈2.7×10−4, thirty times the central difference's.
step h
error
truncation 12eh2
ceiling h22εe
10−2
2.3×10−5
2.3×10−5
1.2×10−11
10−3
2.3×10−7
2.3×10−7
1.2×10−9
10−4
3.8×10−8
2.3×10−9
1.2×10−7
10−5
6.0×10−6
2.3×10−11
1.2×10−5
10−6
1.2×10−5
2.3×10−13
1.2×10−3
At its best step the error is 2.4×10−8, against 3.7×10−11 for the first derivative: a second derivative from three values is always rougher. Lesson 2.4.2 saw the right-hand end of this table. Case study 2.4.5 measured a bond's convexity with a bump of 10−4, close to the best step.
Case study 2.8.4 uses these steps to measure an option's sensitivities by repricing it.
The measured error of each estimate with its two parts: the truncation error from the Taylor series and the rounding ceiling. The best step sits where they cross. For the second difference the ceiling is steeper, so the crossing moves to a larger step and a larger error.
Try it
second is lesson 2.4.2's estimate. Each line prints the step, the error, the truncation error 12eh2 and the rounding ceiling h22εe.
The last line uses the best step, (24ε)1/4.
⌘/Ctrl + Enter runs the cell
Common mistakes
Smaller is not always better. Below the best step, rounding grows like h1, or like h21 for the second difference.
One step does not fit every function. Scale it: about 6 for x at 106, but about 10−5 for ex at 30.
Use a larger step for f′′. About 10−4 times the scale, not 10−8.
Keep all the points on smooth ground, inside the domain. A corner between x−h and x+h ruins the estimate, and x has no value below 0.
The error has a sign. A forward difference is too large where f′′>0 and too small where f′′<0; a backward one does the opposite.
A step below ε∣x∣ vanishes. Then x+h is stored as x, and the difference is 0.
Finite differences and their errors
0 of 12 correct
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
How to type answers
Powers with ^: x^7, 2^(1/3), a^-2.
Multiply with * or by writing side by side: 3x^2, 2(x+1).
ln(x) is the natural log, log(x, 2) is log base 2, e is Euler's number, sqrt(x) is a square root.
sin(x), cos(x) and tan(x) take radians; write pi for π and sin(x)^2 for sin²x.
Your first answer to each question is saved: it counts towards your score and brings the question back for review.
Exact answers like ln(20)/ln(3) are best. Decimals need about 4 significant figures.
Summary card
Truncation errors, from the Taylor series: forward 21f′′(x)h, backward −21f′′(x)h, central 61f′′′(x)h2. They need f smooth between x−h and x+h.
Rounding error: about hε∣f(x)∣ at most, with ε≈2.2×10−16. It grows as h shrinks.
The best step makes the two about equal: εs forward and ε1/3s central, where the scale s is ∣x∣ for powers and logs and 1 for ex and sinx.
Second difference: truncation 121f(4)(x)h2, rounding about h22ε∣f(x)∣, best step about ε1/4s≈10−4s.