Finite differences: the first derivative

Forward, backward and central formulas for the first derivative, their three- and five-point versions, the order of the error and a surprising numerical comparison.

The three basic formulas

With equally spaced nodes of step h, the first derivative is approximated looking forward (forward), backward (backward) or both sides (central). The central one is more accurate because it cancels the even term of the Taylor expansion.

f(xi)fi+1fih+O(h)f'(x_i)\approx\frac{f_{i+1}-f_i}{h}+\mathcal{O}(h)
Forward, order 1.
f(xi)fifi1h+O(h)f'(x_i)\approx\frac{f_i-f_{i-1}}{h}+\mathcal{O}(h)
Backward, order 1.
f(xi)fi+1fi12h+O(h2)f'(x_i)\approx\frac{f_{i+1}-f_{i-1}}{2h}+\mathcal{O}(h^2)
Central, order 2.

Higher-order formulas

Using more nodes raises the order. The three-point ones reach O(h2)\mathcal{O}(h^2) with information on only one side; the five-point central one reaches O(h4)\mathcal{O}(h^4).

f(xi)fi+2+4fi+13fi2h+O(h2)f'(x_i)\approx\frac{-f_{i+2}+4f_{i+1}-3f_i}{2h}+\mathcal{O}(h^2)
Three-point forward.
f(xi)3fi4fi1+fi22h+O(h2)f'(x_i)\approx\frac{3f_i-4f_{i-1}+f_{i-2}}{2h}+\mathcal{O}(h^2)
Three-point backward.
f(xi)fi+2+8fi+18fi1+fi212h+O(h4)f'(x_i)\approx\frac{-f_{i+2}+8f_{i+1}-8f_{i-1}+f_{i-2}}{12h}+\mathcal{O}(h^4)
Five-point central.

Derivation of the three-point forward formula

DerivationDerivation: three-point forward O(h²)View as its own page →
  1. Expand Taylor at xi+1x_{i+1} and xi+2x_{i+2} around xix_i:

    f(xi+1)=f(xi)+hf(xi)+h22f(xi)+R2f(xi+2)=f(xi)+2hf(xi)+2h2f(xi)+R2\begin{aligned} f(x_{i+1})&=f(x_i)+hf'(x_i)+\tfrac{h^2}{2}f''(x_i)+R_2\\ f(x_{i+2})&=f(x_i)+2hf'(x_i)+2h^2 f''(x_i)+R_2 \end{aligned}
  2. Compute (second)2(first)(\text{second})-2\cdot(\text{first}) to cancel ff'; solving for ff'' gives an O(h)\mathcal{O}(h) formula for the second derivative:

    f(xi)=fi+22fi+1+fih2+O(h)f''(x_i)=\frac{f_{i+2}-2f_{i+1}+f_i}{h^2}+\mathcal{O}(h)
  3. Substituting this ff'' into the first expansion and solving for ff' cancels the order-hh term and leaves:

    f(xi)=fi+2+4fi+13fi2h+O(h2)f'(x_i)=\frac{-f_{i+2}+4f_{i+1}-3f_i}{2h}+\mathcal{O}(h^2)