TExcelTAD
Home › Numerical Methods › Error and Step Size in Finite Differences

Error and Step Size in Finite Differences

A finite-difference formula has two competing errors. Truncation error shrinks as the step hh shrinks; rounding error grows, because nearly equal function values are subtracted and the difference is divided by hh. There is an optimal hh, 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 ϵmach\epsilon_{\text{mach}}:

∣f~(x+h)−f~(x)h−f′(x)∣≲h2∣f′′∣⏟truncation+2ϵmach∣f∣h⏟rounding.\left\lvert\frac{\tilde{f}(x + h) - \tilde{f}(x)}{h} - f'(x)\right\rvert \lesssim \underbrace{\frac{h}{2}\lvert f''\rvert}_{\text{truncation}} + \underbrace{\frac{2\epsilon_{\text{mach}}\lvert f\rvert}{h}}_{\text{rounding}}.

Minimizing over hh gives h∗≈ϵmach≈10−8h^* \approx \sqrt{\epsilon_{\text{mach}}} \approx 10^{-8} (for ff and f′′f'' of moderate size) and a best error about ϵmach≈10−8\sqrt{\epsilon_{\text{mach}}} \approx 10^{-8}: only half the digits.

For a method of order pp, the same balance gives h∗≈ϵmach1/(p+1)h^* \approx \epsilon_{\text{mach}}^{1/(p+1)} and best error ≈ϵmachp/(p+1)\approx \epsilon_{\text{mach}}^{p/(p+1)}:

Formula Order pp Best hh Best error
forward 1 10−810^{-8} 10−810^{-8}
centered 2 10−510^{-5} 10−1110^{-11}
5-point centered 4 10−310^{-3} 10−1310^{-13}
second derivative, 3-point 2 10−410^{-4} 10−810^{-8}

Seeing the V shape

python · try it yourself
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 pp on the right (truncation), slope −1-1 on the left (rounding).

The complex-step trick

For a real-analytic ff implemented with complex arithmetic, f(x+ih)=f(x)+ihf′(x)−h22f′′(x)+⋯f(x + ih) = f(x) + ihf'(x) - \frac{h^2}{2}f''(x) + \cdots, so

f′(x)≈Im⁡f(x+ih)hf'(x) \approx \frac{\operatorname{Im}f(x + ih)}{h}

with no subtraction: there is no cancellation, h=10−20h = 10^{-20} is fine, and the result is accurate to machine precision.

python · try it yourself
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 D(h)=f′(x)+ch2+O(h4)D(h) = f'(x) + ch^2 + O(h^4), then combining two step sizes cancels the h2h^2 term:

f′(x)≈4D(h/2)−D(h)3,f'(x) \approx \frac{4D(h/2) - D(h)}{3},

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_fprime uses forward differences with h≈ϵh \approx \sqrt{\epsilon}; 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 10−710^{-7} relative.

Exercises

Exercise 1: Best step

Using the balance h26=ϵh\frac{h^2}{6} = \frac{\epsilon}{h}, estimate the optimal hh for the centered difference with ϵ=10−16\epsilon = 10^{-16}.

Answer

About 8×10−68 \times 10^{-6}.

Solution

h3=6ϵh^3 = 6\epsilon, h=(6×10−16)1/3≈8.4×10−6h = (6 \times 10^{-16})^{1/3} \approx 8.4 \times 10^{-6}.

Exercise 2: Single precision

In float32 (ϵ≈1.2×10−7\epsilon \approx 1.2 \times 10^{-7}), what accuracy can the forward difference reach?

Answer

About 3×10−43 \times 10^{-4} (3 to 4 digits).

Solution

Best error ≈ϵ≈3.5×10−4\approx \sqrt{\epsilon} \approx 3.5 \times 10^{-4} at h≈3.5×10−4h \approx 3.5 \times 10^{-4}.

Exercise 3: Richardson (code)

Apply one Richardson step to the centered difference of sin⁡\sin at x=0.7x = 0.7 with h=0.1h = 0.1 and compare errors.

Answer

The error drops from about 1.3×10−31.3 \times 10^{-3} to about 1.6×10−71.6 \times 10^{-7}.

Solution
python · try it yourself
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…

  1. 10−1610^{-16}
  2. 10−810^{-8}
  3. 10−410^{-4}
  4. 10−1210^{-12}

Why does the error grow when hh becomes very small?

  1. Truncation error grows
  2. Rounding errors in the subtraction are divided by hh
  3. The Taylor series diverges
  4. Overflow

The complex-step derivative avoids cancellation because…

  1. it uses higher precision
  2. it takes an imaginary part instead of subtracting
  3. it uses a large hh
  4. it is exact for polynomials only