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

The forward difference is so named because it starts at xx and looks forward to obtain derivative information.

Consider the Taylor expansion of f(x)f(x) from a point xix_i, with the step size hh:

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

Truncating the series after the first two terms:

f(x+h)=f(x)+f′(x)h+O(h2)f(x+h) = f(x) + f'(x) h + O(h^2)

and solving for the first derivative yields the forward difference stencil:

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

which is to say that:

f′(x)≈f(x+h)−f(x)hf'(x) \approx \frac{f(x+h) - f(x)}{h}

with a linear order of convergence since it depends on O(h)O(h).

Example: Rocket Velocity

A rocket has velocity:

v(t)=2000ln⁡(14×10414×104−2100t)−9.8tv(t) = 2000 \ln \left(\frac{14 \times 10^4}{14 \times 10^4 - 2100t}\right) - 9.8 t

What is the acceleration as a function of tt? Analytically we can find,

a(t)=v′(t)=42×10514×104−2100t−9.8a(t) = v'(t) = \frac{42 \times 10^5}{14 \times 10^4 - 2100 t} - 9.8

Numerical Acceleration with Forward Difference

We implement the forward difference stencil and approximate the rocket acceleration across step sizes h∈{2,1,0.1}h \in \{2, 1, 0.1\}:

Backward Difference

By contrast, the backward difference steps backwards. Consider replacing hh with −h-h:

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

and solving for the derivative:

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

Central Difference

Note that the approximations are approaching the analytic solution from opposite directions. Can we combine forward and backward differences to get a better result?

Let’s examine the forward and backward expansions:

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

Subtracting the two equations cancels the quadratic term:

f(x+h)−f(x−h)=2f′(x)h+2f′′′(x)h36+O(h5)f(x+h) - f(x-h) = 2 f'(x) h + 2 f'''(x)\frac{h^3}{6} + O(h^5)

Rearranging leads to the central difference formula:

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

which is second order accurate in hh since the next correction is O(h2)O(h^2)!

Error Comparison (h=2h=2)

Let’'s compare the error in forward, backward, and central difference for h=2h=2 across the first few time steps:

Error Scaling: First-Order Schemes

Let’'s see how error scales when halving the step size from h=2h=2 to h′′=1h''=1. For forward and backward difference, the error roughly cuts in half: Error∼hh′′Error \sim \frac{h}{h''}, while the central difference cuts by Error∼[hh′′]2Error \sim \left[\frac{h}{h''}\right]^2

Summary of First-Order Derivatives

The central difference algorithm:

  • Finds a more accurate solution in the same number of function calls.

  • Becomes more accurate with step size quadratically (O(h2)O(h^2)).

  • Requires information both before and after the evaluation point, meaning it cannot be used at boundary endpoints without one-sided stencils.

  • Requires equal step size!