Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

 Open In Colab

Forward Difference (Second Order)

We can similarly derive higher order derivatives in forward, backward, and central approximations. Once obtained, we can also use the second order derivative to improve our estimate of the first order derivative.

Consider the Taylor expansions:

f(x+2h)=f(x)+2f′(x)h+f′′(x)4h22+O(h3)f(x+h)=f(x)+f′(x)h+f′′(x)h22+O(h3)\begin{aligned} f(x+2h) &= f(x)+ 2 f'(x) h + f''(x)\frac{4 h^2}{2} + O(h^3) \\ f(x+h) &= f(x)+ f'(x) h + f''(x)\frac{h^2}{2} + O(h^3) \end{aligned}

Multiplying the second equation by 2 and subtracting from the first eliminates the first derivative term:

f′′(x)=f(x+2h)−2f(x+h)+f(x)h2+O(h)f''(x) = \frac{f(x+2h) - 2 f(x+h) + f(x)}{h^2} + O(h)

Alternative Derivation

Note we could also arrive at this considering the 1st derivative of the 1st derivatives:

f′(x)=f(x+h)−f(x)hf′(x+h)=f(x+2h)−f(x+h)hf′′(x)=f′(x+h)−f′(x)h=f(x+2h)−2f(x+h)+f(x)h2\begin{aligned} f'(x) &= \frac{f(x+h) - f(x)}{h} \\ f'(x+h) &= \frac{f(x+2h) - f(x+h)}{h} \\ f''(x) &= \frac{f'(x+h) - f'(x)}{h} = \frac{f(x+2h) - 2f(x+h) + f(x)}{h^2} \end{aligned}

which lends itself directly to a recursive implementation.

A More Accurate First Derivative

We can now revisit the approximation of f′(x)f'(x) and use our approximation of f′′(x)f''(x) to improve it!

Recall the Taylor series:

f(x+h)=f(x)+f′(x)h+f′′(x)h22+O(h3)f(x+h) = f(x) + f'(x)h + f''(x)\frac{h^2}{2} + O(h^3)

Substituting our f′′(x)f''(x) forward approximation yields:

f′(x)=−f(x+2h)+4f(x+h)−3f(x)2h+O(h2)f'(x) = \frac{-f(x+2h) + 4f(x+h) - 3f(x)}{2h} + O(h^2)

This is a nice result, but notice that we now need three function calls to achieve the same accuracy as the central difference (which only needs two function calls).

The general formula for nn-th order forward derivatives is:

f(n)(x)=∑i=0n(−1)n−i(ni)f(x+ih)f^{(n)}(x) = \sum_{i=0}^n (-1)^{n-i} \binom{n}{i} f(x+ih)

which one could use to successively build higher and higher order algorithms.

Example: Error Comparison for Improved Forward Difference

Compare the error for the improved forward difference method against standard forward difference and central difference:

Backward Difference (Second Order)

Following a similar derivation, the backward second derivative is:

f′′(x)=f(x)−2f(x−h)+f(x−2h)h2+O(h)f''(x) = \frac{f(x) - 2 f(x-h) + f(x-2h)}{h^2} + O(h)

and the second-order accurate backward first derivative is:

f′(x)=3f(x)−4f(x−h)+f(x−2h)2h+O(h2)f'(x) = \frac{3 f(x) - 4 f(x-h) + f(x-2h)}{2h} + O(h^2)

Central Difference (Second Order)

Finally, simply adding the forward and backward Taylor expansions gives the central difference for the second derivative:

f(x+h)=f(x)+f′(x)h+f′′(x)h22+f′′′(x)h36+f(4)(x)h424+O(h5)f(x−h)=f(x)−f′(x)h+f′′(x)h22−f′′′(x)h36+f(4)(x)h424+O(h5)\begin{aligned} f(x+h) &= f(x) + f'(x) h + f''(x) \frac{h^2}{2} + f'''(x)\frac{h^3}{6} + f^{(4)}(x)\frac{h^4}{24} + O(h^5) \\ f(x-h) &= f(x) - f'(x) h + f''(x) \frac{h^2}{2} - f'''(x)\frac{h^3}{6} + f^{(4)}(x)\frac{h^4}{24} + O(h^5) \end{aligned}

Adding the two expressions cancels all odd powers of hh:

f(x+h)+f(x−h)=2f(x)+f′′(x)h2+O(h4)f(x+h) + f(x-h) = 2 f(x) + f''(x) h^2 + O(h^4)

Rearranging for f′′(x)f''(x) yields:

f′′(x)=f(x+h)−2f(x)+f(x−h)h2+O(h2)f''(x) = \frac{f(x+h) - 2 f(x) + f(x-h)}{h^2} + O(h^2)

which is second-order accurate!

Summary of Finite Difference Algorithms

Summary Table of Finite Difference Stencils

The accuracy of the approximation can be improved in two ways:

  1. Decrease step size (h→0h \to 0).

  2. Use approximated higher order derivatives to correct lower order ones.

Central difference yields the same order of accuracy as the improved forward/backward difference with one fewer function evaluation, but requires points on both sides of the evaluation point.