Finite differences for PDEs

Replace every derivative by a difference on a grid, and a PDE (Partial Differential Equation) becomes arithmetic a computer can do. The art is doing it stably.

⏱ 25 minNeeds: Chapters 2, 4, 6, 73 simulations
In plain words

Lay a grid over space and time. Store the temperature only at the grid points. Approximate each derivative using neighbouring values — a stencil. Now the PDE says how to compute the next row of the grid from the current one. Choose the time step carelessly, though, and tiny errors grow until the answer is garbage.

You will learn
Level 1 · basicGrids and stencils Level 2 · standardThe explicit heat scheme and its time-step limit Level 3 · advancedvon Neumann analysis, CFL, Lax equivalence
Simplest example first · differences on three points

Take $u(x) = x^2$ at the three grid points $x = 0.9, 1, 1.1$ (spacing $h = 0.1$): the values are $0.81, 1, 1.21$. Then $$\underbrace{\frac{1.21 - 1}{0.1} = 2.1}_{\text{forward}},\qquad \underbrace{\frac{1.21 - 0.81}{0.2} = 2}_{\text{central}},\qquad \underbrace{\frac{0.81 - 2(1) + 1.21}{0.01} = 2}_{\text{second difference}}.$$ The exact values are $u'(1) = 2$ and $u''(1) = 2$. The forward difference is off by $h = 0.1$; the central differences are exact for any parabola. A finite difference method is nothing more than this, done at every grid point at once.

1. Grid and stencils

Use grid points $x_i = i\,\Delta x$ and times $t^n = n\,\Delta t$, and write $u_i^n \approx u(x_i, t^n)$. Taylor's theorem (Chapter 2) gives

DerivativeFinite differenceError
$u_t$$\dfrac{u_i^{n+1} - u_i^n}{\Delta t}$ (forward)$O(\Delta t)$
$u_x$$\dfrac{u_{i+1} - u_{i-1}}{2\Delta x}$ (central)$O(\Delta x^2)$
$u_x$$\dfrac{u_{i} - u_{i-1}}{\Delta x}$ (backward / upwind)$O(\Delta x)$
$u_{xx}$$\dfrac{u_{i-1} - 2u_i + u_{i+1}}{\Delta x^2}$ (central)$O(\Delta x^2)$
$\Delta u = u_{xx} + u_{yy}$$\dfrac{u_{i-1,j} + u_{i+1,j} + u_{i,j-1} + u_{i,j+1} - 4u_{i,j}}{h^2}$ (5-point)$O(h^2)$
Result · Error of the central second difference

If $u$ has four continuous derivatives, $$\frac{u(x - h) - 2u(x) + u(x + h)}{h^2} = u''(x) + \frac{h^2}{12}u^{(4)}(\xi), \quad \xi \in (x - h, x + h).$$

LeVeque, Finite Difference Methods for Ordinary and Partial Differential Equations, SIAM (2007), chapter 1 "Finite Difference Approximations".

2. The explicit scheme for the heat equation

Animation: two runs of the explicit heat scheme; with r = 0.45 the profile decays smoothly, with r = 0.55 a zig-zag grows and explodes.
Watch: Identical scheme and grid. With $r = 0.45$ the solution decays smoothly; with $r = 0.55$ a tiny zig-zag grows every step until it explodes.

For $u_t = \alpha u_{xx}$, use forward difference in time and central in space (FTCS (Forward-Time, Central-Space scheme)): $$\frac{u_i^{n+1} - u_i^n}{\Delta t} = \alpha\,\frac{u_{i-1}^n - 2u_i^n + u_{i+1}^n}{\Delta x^2} \quad\Longrightarrow\quad u_i^{n+1} = u_i^n + r\,\big(u_{i-1}^n - 2u_i^n + u_{i+1}^n\big),\qquad r = \frac{\alpha\,\Delta t}{\Delta x^2}.$$ Each new value is computed from three old ones — the stencil. Everything depends on the single number $r$.

Stable or unstable? The number r decidesinitial profile: a triangle; ends held at 0
exact (Fourier series)FTCS grid values
Set $r = 0.52$ and increase the number of steps: a saw-tooth appears and explodes, though the true solution simply decays. Below $r = 0.5$ the scheme is stable for every number of steps. Halving $\Delta x$ forces $\Delta t$ to drop by a factor of 4 — the price of an explicit method for a parabolic PDE.

3. Why ½? von Neumann stability analysis

Errors on the grid can be written as a sum of Fourier modes $e^{\mathrm i k x_i}$. Insert a single mode $u_i^n = G^n e^{\mathrm i k i\Delta x}$ into the scheme. Using $e^{\mathrm i\theta} + e^{-\mathrm i\theta} - 2 = -4\sin^2(\theta/2)$: $$G = 1 - 4r\sin^2\!\Big(\frac{k\Delta x}{2}\Big).$$ Each step multiplies the mode by the amplification factor $G$. Stability needs $|G| \le 1$ for every $k$. The worst mode is the saw-tooth ($\sin^2 = 1$): $|1 - 4r| \le 1 \iff r \le \tfrac12$. That is exactly the saw-tooth you saw blow up.

Definition · von Neumann stability condition

A linear constant-coefficient scheme on a periodic grid is stable (in the $\ell^2$ norm) if its amplification factor satisfies $|G(k)| \le 1 + C\Delta t$ for all wave numbers $k$.

Charney, Fjørtoft & von Neumann, "Numerical integration of the barotropic vorticity equation", Tellus 2 (1950) 237–254; Strikwerda, Finite Difference Schemes and Partial Differential Equations, 2nd ed., SIAM (2004), chapter 2; LeVeque (2007), chapter 9.
Implicit cure: Crank–Nicolson

Averaging the spatial difference between times $n$ and $n+1$ gives the Crank–Nicolson scheme, second order in time and stable for every $r$: $$\frac{u_i^{n+1} - u_i^n}{\Delta t} = \frac{\alpha}{2}\left[\delta^2 u_i^{n} + \delta^2 u_i^{n+1}\right], \qquad \delta^2 u_i = \frac{u_{i-1} - 2u_i + u_{i+1}}{\Delta x^2}.$$ Each step now requires solving a tridiagonal linear system (Chapter 4) — cheap, $O(N)$ operations.

Crank & Nicolson, "A practical method for numerical evaluation of solutions of partial differential equations of the heat-conduction type", Proc. Camb. Phil. Soc. 43 (1947) 50–67.

4. Waves and the CFL condition

For the advection equation $u_t + a\,u_x = 0$ ($a > 0$), a bump travels right at speed $a$. The upwind scheme uses the backward difference (information comes from the left): $$u_i^{n+1} = u_i^n - \nu\,\big(u_i^n - u_{i-1}^n\big), \qquad \nu = \frac{a\,\Delta t}{\Delta x}\ \ (\text{Courant number}).$$

Theorem · Courant–Friedrichs–Lewy (CFL) condition

A necessary condition for convergence of an explicit scheme is that its numerical domain of dependence contains the true domain of dependence of the PDE. For upwind advection this means $$\nu = \frac{|a|\,\Delta t}{\Delta x} \le 1:$$ in one time step, the wave must not travel further than one grid cell.

Courant, Friedrichs & Lewy, "Über die partiellen Differenzengleichungen der mathematischen Physik", Mathematische Annalen 100 (1928) 32–74; LeVeque (2007), chapter 10.
Upwind advection and the Courant numberperiodic domain; the bump should travel without changing shape
exact (shifted bump)upwind scheme, 80 cells
For $\nu < 1$ the bump survives but spreads — numerical diffusion from the first-order error. At $\nu = 1$ the scheme is exact (each value just moves one cell). Above 1 it violates CFL and explodes.

5. Convergence: the Lax equivalence theorem

Theorem · Lax equivalence theorem

For a consistent finite difference approximation of a well-posed linear initial value problem, stability is necessary and sufficient for convergence.

Lax & Richtmyer, "Survey of the stability of linear finite difference equations", Comm. Pure Appl. Math. 9 (1956) 267–293.

This is the PDE version of Dahlquist's theorem for ODEs (Chapter 6). Consistency (small local error) is usually easy to check with Taylor series; stability is the hard part, and von Neumann analysis checks it.

For steady problems the recipe is the same: discretise $-u'' = f$ with the central difference, solve the tridiagonal system, and the error is $O(h^2)$. Measure it:

Order of accuracy of a Poisson solver−u″ = π² sin(πx), u(0) = u(1) = 0, exact u = sin(πx)
max error, central differencesreference slope 2
Each point solves the linear system with the Thomas algorithm (Gaussian elimination for tridiagonal matrices) — in your browser, now. Halving $h$ divides the error by 4: second order.
Beyond finite differences

For complex geometries, engineers use the finite element method (FEM: approximate $u$ by piecewise polynomials and impose the PDE in an averaged, "weak" sense) and the finite volume method (FVM: conserve fluxes across cell faces — the standard in reservoir and CFD codes). The ideas of consistency, stability and convergence carry over unchanged.

Brenner & Scott, The Mathematical Theory of Finite Element Methods, 3rd ed., Springer (2008); LeVeque, Finite Volume Methods for Hyperbolic Problems, CUP (2002).
Finite differences meet machine learning

A stencil applied across a grid is a convolution: the 5-point Laplacian is a $3\times3$ convolution kernel. Convolutional networks can therefore represent — and learn — finite difference operators (Long, Lu, Ma & Dong, "PDE-Net", ICML 2018). Classical solvers also generate the training data for neural operators, and remain the reference against which every SciML method must be checked.

6. Try it in Python

import numpy as np
import matplotlib.pyplot as plt

def ftcs(N=20, r=0.45, steps=100):
    x = np.linspace(0, 1, N + 1); dx = 1 / N
    u = np.minimum(x, 1 - x) * 2            # triangle, u = 0 at both ends
    for _ in range(steps):
        u[1:-1] = u[1:-1] + r * (u[:-2] - 2 * u[1:-1] + u[2:])
    return x, u, steps * r * dx**2          # final time t = n * dt

for r in [0.45, 0.52]:
    x, u, t = ftcs(r=r, steps=120)
    plt.plot(x, u, "o-", label=f"r = {r}, t = {t:.3f}")
plt.ylim(-1, 1.2); plt.legend(); plt.grid(alpha=.3); plt.title("FTCS: stable vs unstable")
plt.show()

With FTCS for the heat equation you halve $\Delta x$. To stay stable, $\Delta t$ must be…

Right: $r = \alpha\Delta t/\Delta x^2 \le \tfrac12$, so $\Delta t \propto \Delta x^2$.

Upwind scheme, wave speed $a = 2$, $\Delta x = 0.01$. The largest stable $\Delta t$ is…

Right: CFL needs $2\,\Delta t/0.01 \le 1$, so $\Delta t \le 0.005$.

Limitations, and where this leads

Grids grow like $n^d$ with dimension, stability restricts time steps, complex geometries need meshes, and measured data cannot easily be added to a scheme. Chapter 9 looks at these limits honestly, and at how SciML methods address them by building on what you learned here. Go to Chapter 9 →

Key takeaways

Further reading