Error and Step Size in Finite Differences
A finite-difference formula has two competing errors. Truncation error shrinks as the step shrinks; rounding error grows, because nearly equal function values are subtracted and the difference is divided by . There is an optimal , and it limits how many digits a finite difference can ever deliver.
The total error
For the forward difference, if each function value carries a relative rounding error about :
Minimizing over gives (for and of moderate size) and a best error about : only half the digits.
For a method of order , the same balance gives and best error :
| Formula | Order | Best | Best error |
|---|---|---|---|
| forward | 1 | ||
| centered | 2 | ||
| 5-point centered | 4 | ||
| second derivative, 3-point | 2 |
Seeing the V shape
import numpy as np
x, exact = 0.7, np.cos(0.7)
hs = 10.0 ** -np.arange(1, 16)
print(" h forward centered 5-point")
for h in hs:
fw = abs((np.sin(x + h) - np.sin(x)) / h - exact)
ce = abs((np.sin(x + h) - np.sin(x - h)) / (2 * h) - exact)
f5 = abs((np.sin(x - 2*h) - 8*np.sin(x - h) + 8*np.sin(x + h) - np.sin(x + 2*h)) / (12 * h) - exact)
print(f"{h:7.0e} {fw:.1e} {ce:.1e} {f5:.1e}") h forward centered 5-point
1e-01 3.3e-02 1.3e-03 2.5e-06
1e-02 3.2e-03 1.3e-05 2.5e-10
1e-03 3.2e-04 1.3e-07 3.5e-14
1e-04 3.2e-05 1.3e-09 1.8e-13
1e-05 3.2e-06 1.7e-11 5.9e-12
1e-06 3.2e-07 2.2e-11 6.8e-11
1e-07 3.2e-08 7.7e-11 7.7e-11
1e-08 2.3e-09 2.3e-09 4.1e-09
1e-09 2.3e-09 2.3e-09 3.9e-08
1e-10 4.5e-07 4.5e-07 7.2e-07
1e-11 1.6e-06 1.6e-06 2.9e-07
1e-12 9.5e-06 9.5e-06 9.0e-06
1e-13 1.0e-04 1.0e-04 2.9e-04
1e-14 1.2e-03 1.2e-03 3.1e-03
1e-15 1.2e-02 1.2e-02 6.2e-03
On a log-log plot each column traces a "V": slope on the right (truncation), slope on the left (rounding).
The complex-step trick
For a real-analytic implemented with complex arithmetic, , so
with no subtraction: there is no cancellation, is fine, and the result is accurate to machine precision.
import numpy as np
f = lambda z: np.exp(z) * np.sin(z) / (1 + z**2)
x = 0.9
h = 1e-20
cs = np.imag(f(x + 1j * h)) / h
best_cd = (f(x + 1e-5) - f(x - 1e-5)) / 2e-5
exact = (np.exp(x) * (np.sin(x) + np.cos(x)) * (1 + x**2) - np.exp(x) * np.sin(x) * 2 * x) / (1 + x**2) ** 2
print(f"complex step error: {abs(cs - exact):.1e}")
print(f"centered (best h) error: {abs(best_cd - exact):.1e}")complex step error: 5.6e-16
centered (best h) error: 3.6e-12
Richardson extrapolation
If , then combining two step sizes cancels the term:
which is fourth order. Repeating the idea gives a whole table (Romberg's method for integrals uses the same trick).
In software
scipy.optimize.approx_fprimeuses forward differences with ;scipy.differentiate.derivative(SciPy 1.15+) uses adaptive centered differences with Richardson-style error estimates.- Gradient checks in ML compare backprop gradients with centered differences in double precision, expecting agreement to about relative.
Exercises
Exercise 1: Best step
Using the balance , estimate the optimal for the centered difference with .
Answer
About .
Solution
, .
Exercise 2: Single precision
In float32 (), what accuracy can the forward difference reach?
Answer
About (3 to 4 digits).
Solution
Best error at .
Exercise 3: Richardson (code)
Apply one Richardson step to the centered difference of at with and compare errors.
Answer
The error drops from about to about .
Solution
import numpy as np
x, exact = 0.7, np.cos(0.7)
D = lambda h: (np.sin(x + h) - np.sin(x - h)) / (2 * h)
h = 0.1
R = (4 * D(h / 2) - D(h)) / 3
print(f"D(h) error {abs(D(h) - exact):.1e}, D(h/2) error {abs(D(h / 2) - exact):.1e}, Richardson error {abs(R - exact):.1e}")D(h) error 1.3e-03, D(h/2) error 3.2e-04, Richardson error 1.6e-07
Test yourself
The best achievable error of the forward difference in double precision is about…
Why does the error grow when becomes very small?
- Truncation error grows
- Rounding errors in the subtraction are divided by
- The Taylor series diverges
- Overflow
The complex-step derivative avoids cancellation because…
- it uses higher precision
- it takes an imaginary part instead of subtracting
- it uses a large
- it is exact for polynomials only