---
title: Finite differences and stability
heading: Replacing derivatives with differences, and the step size that blows up
eyebrow: A12 · Lesson 48 · Finite differences and stability
xp: 40
---

::: hook
Most PDEs engineers meet have no closed-form solution: odd shapes, varying materials, nonlinear terms. They're solved on a grid, by replacing each derivative with a difference between neighboring values. The method is simple enough to write in an afternoon, and it has one trap that catches everyone the first time: take a time step a little too large, and a tiny wiggle at the grid's finest scale doubles every step until the answer is garbage. The **von Neumann stability analysis** predicts exactly when that happens, and the same reasoning sets the time step of every FDTD field solver (C10).
:::

::: play
- The explicit scheme for the heat equation on 21 grid points, after N steps. The start is a smooth sine plus a barely visible wiggle at the finest scale the grid can hold. The curve is the exact solution.
- With $r = \alpha\Delta t/\Delta x^2$ below 0.5, the wiggle dies at once and the computed points follow the exact curve.
- Push r past 0.5 and step N forward: the wiggle's amplification factor (the readout) passes −1, and it grows by that factor every step, alternating in sign, until it swamps everything.
:::
:::

```sim plot
view: graph
x: n = 0..20
y: u = -1.5..1.5
slider: r = 0.1..0.7 step 0.01 = 0.4
slider: N = 0..60 step 1 = 10
plot: sin(pi*n/20)*(1 - 4*r*sin(pi/40)^2)^N + 0.02*sin(19*pi*n/20)*(1 - 4*r*sin(19*pi/40)^2)^N | stems | label: computed
plot: sin(pi*n/20)*exp(-pi^2*r*N/400) | label: exact
readout: amplification of the finest wiggle per step = 1 - 4*r*sin(19*pi/40)^2
readout: amplification of the smooth sine per step = 1 - 4*r*sin(pi/40)^2
```

::: predict
## The explicit heat scheme is stable for $r \le 1/2$. If you halve the grid spacing Δx, what happens to the largest stable time step?

- [ ] It halves
- [x] It drops to a quarter
- [ ] It doubles
- [ ] It stays the same

::: reveal
A quarter: $\Delta t \le \Delta x^2/(2\alpha)$. A twice-finer grid needs four times as many steps, each on twice as many points: eight times the work in 1D. That cost is why implicit schemes, which have no such limit, are used for diffusion.
:::
:::

::: nameit
## Differences

With grid spacing h:

| Derivative | Difference | Error |
|---|---|---|
| $u'$, forward | $\frac{u_{i+1} - u_i}{h}$ | $O(h)$ |
| $u'$, central | $\frac{u_{i+1} - u_{i-1}}{2h}$ | $O(h^2)$ |
| $u''$ | $\frac{u_{i+1} - 2u_i + u_{i-1}}{h^2}$ | $O(h^2)$: $\frac{h^2}{12}u''''$ |

## The explicit (FTCS) heat scheme

$$u_i^{n+1} = u_i^n + r\left(u_{i+1}^n - 2u_i^n + u_{i-1}^n\right), \qquad r = \frac{\alpha\Delta t}{\Delta x^2}$$

## Von Neumann analysis

Put $u_i^n = G^ne^{jki\Delta x}$ into the scheme: each Fourier mode is multiplied every step by

$$G(k) = 1 - 4r\sin^2\frac{k\Delta x}{2}$$

Stability needs $\lvert G\rvert \le 1$ for every mode. The worst is the finest, $k\Delta x = \pi$, with $G = 1 - 4r$, so the condition is $r \le \frac12$.

## Other schemes

- **Implicit (backward Euler):** use the new values in the difference; $G = \frac{1}{1 + 4r\sin^2(k\Delta x/2)}$, stable for every r, at the cost of solving a tridiagonal system each step.
- **Crank–Nicolson:** average the two; $G = \frac{1 - 2r\sin^2(k\Delta x/2)}{1 + 2r\sin^2(k\Delta x/2)}$, stable for every r and second-order accurate in time.
- **The wave equation** (leapfrog, FDTD): stable when the Courant number $C = c\Delta t/\Delta x \le 1$; information mustn't skip a cell in one step.
- **Laplace's equation:** no time step at all; iterate (Jacobi, Gauss–Seidel, SOR) until the grid settles.
:::

::: example
## A copper bar (α = 1.1 × 10⁻⁴ m²/s) is modeled with Δx = 1 mm. What's the largest stable explicit time step, and how many steps does 10 s take?

::: solution
1. **The stability limit.**
   $$\begin{aligned} \Delta t &\le \frac{(10^{-3})^2}{2 \times 1.1 \times 10^{-4}}\ \text{s} \\ &= 4.5\ \text{ms} \end{aligned}$$
2. **The number of steps.** So 10 s takes at least 2200 steps.
3. **A finer grid.** At Δx = 0.1 mm it would be 220 000.

**Answer:** the largest stable step is 4.5 ms, so 10 s takes at least 2200 steps.
:::
:::

::: example
## Check the second difference on $u = x^4$ at x = 1 with h = 0.5.

::: solution
1. **The second difference.**
   $$\begin{aligned} D^2u &= \frac{1.5^4 - 2 + 0.5^4}{0.25} \\ &= \frac{5.0625 - 2 + 0.0625}{0.25} \\ &= 12.5 \end{aligned}$$
   against the exact $u'' = 12$.
2. **The error.** It is 0.5, which is
   $$\frac{h^2}{12}u'''' = \frac{0.25}{12} \times 24$$
   as the table predicts.

**Answer:** the second difference gives 12.5 against the exact 12, and the error of 0.5 is what $\frac{h^2}{12}u''''$ predicts.
:::
:::

::: example
## With $r = \alpha\Delta t/\Delta x^2 = 2$, how do the explicit, backward Euler and Crank–Nicolson schemes treat the grid's finest wiggle ($k\Delta x = \pi$)?

::: solution
1. **The truth.** The true heat equation would shrink that wiggle by
   $$\begin{aligned} e^{-r(k\Delta x)^2} &= e^{-2\pi^2} \\ &\approx 3 \times 10^{-9} \end{aligned}$$
   per step.
2. **Explicit.** With $s = \sin^2(k\Delta x/2) = 1$, the amplification factor per step is
   $$\begin{aligned} G &= 1 - 4rs \\ &= -7 \end{aligned}$$
   so the explicit scheme blows the wiggle up sevenfold per step.
3. **Backward Euler.**
   $$\begin{aligned} G &= \frac{1}{1 + 4rs} \\ &= \frac19 \end{aligned}$$
   It damps the wiggle strongly, though less than the truth.
4. **Crank–Nicolson.**
   $$\begin{aligned} G &= \frac{1 - 2rs}{1 + 2rs} \\ &= -\frac35 \end{aligned}$$
   It is stable, but it only shrinks the wiggle by 0.6 and flips its sign each step, so sharp starting data leaves a slowly fading checkerboard.
5. **In practice.** That's why Crank–Nicolson runs often take a few backward Euler steps first.

**Answer:** per step, the explicit scheme multiplies the wiggle by −7 (it blows up), backward Euler by $\frac19$, and Crank–Nicolson by $-\frac35$ (stable, but a slowly fading checkerboard).
:::
:::

::: check
given: dx = 0.5 | 1 | 2; a = 0.00002 | 0.0001
answer: (dx/1000)^2/(2*a)*1000
unit: ms
tolerance: 0.5%

## An explicit heat solver uses Δx = {dx} mm on a material with α = {a} m²/s. What's the largest stable time step?

::: right
Right. $\Delta x^2/(2\alpha)$ = {answer} ms.
:::

::: misses
- [(dx/1000)^2/a*1000] The limit is $r \le \frac12$, so divide by 2α.
- [any] Not yet. $r = \alpha\Delta t/\Delta x^2 \le \frac12$.
:::
:::

::: check
given: r = 0.3 | 0.6 | 0.75 | 1
answer: 1 - 4*r
tolerance: 0.5%

## With r = {r}, what's the amplification factor of the finest grid wiggle ($k\Delta x = \pi$) per step?

::: right
Right. $1 - 4r$ = {answer}.
:::

::: misses
- [1 - 2*r] At $k\Delta x = \pi$, $\sin^2(\pi/2) = 1$, so it's $1 - 4r$.
- [any] Not yet. $G = 1 - 4r\sin^2(k\Delta x/2)$.
:::
:::

::: check
given: r = 0.6 | 0.75 | 1; N = 10 | 20
answer: abs(1 - 4*r)^N
tolerance: 1%

## An explicit heat run with r = {r} starts with a tiny wiggle at the grid's finest scale. By what factor has its size grown after {N} steps?

::: right
Right. $\lvert 1 - 4r\rvert^N$ = {answer}: the run is ruined.
:::

::: misses
- [any] Not yet. Each step multiplies it by $1 - 4r$.
:::
:::

::: check
given: dx = 1 | 5 | 10
answer: dx*1e-3/3e8*1e12
unit: ps
tolerance: 0.5%

## A 1D FDTD simulation of free space uses cells of {dx} mm. What's the largest stable time step?

::: right
Right. $\Delta x/c$ = {answer} ps.
:::

::: misses
- [dx*1e-3/3e8*1e12/2] In 1D the Courant limit is exactly $c\Delta t/\Delta x = 1$.
- [any] Not yet. $c\Delta t \le \Delta x$.
:::
:::

::: check
given: x = 1 | 2; h = 0.5 | 1
answer: 12*x^2 + 2*h^2
tolerance: 0.1%

## What does the second difference $\frac{u(x + h) - 2u(x) + u(x - h)}{h^2}$ give for $u = x^4$ at x = {x} with h = {h}?

::: right
Right. $12x^2 + 2h^2$ = {answer}, against the true $12x^2$.
:::

::: misses
- [12*x^2] That's the exact second derivative; the difference adds $\frac{h^2}{12}u'''' = 2h^2$.
- [any] Not yet. Expand $(x \pm h)^4$ and add.
:::
:::

::: check
given: k = 2 | 3 | 10
answer: k^2
tolerance: 0.5%

## A second-order scheme's error goes as $h^2$. By what factor does the error shrink when the grid is made {k} times finer?

::: right
Right. {answer} times.
:::

::: misses
- [k] That's a first-order scheme.
- [any] Not yet. $(1/k)^2$.
:::
:::

::: check
kind: choice

## Which of these schemes for the heat equation is stable for every time step?

- [ ] Forward Euler in time, central in space (explicit)
- [x] Backward Euler in time (implicit)
- [ ] Leapfrog in time
- [ ] Any explicit scheme with a small enough grid

::: misses
- [1] That one needs $r \le \frac12$.
- [3] Leapfrog is unstable for diffusion at every step size.
- [4] A finer grid makes the explicit limit stricter.
:::
:::

::: check
kind: parts
given: r = 0.25 | 1 | 3

## A heat solver runs with $r = \alpha\Delta t/\Delta x^2$ = {r}. Consider the grid's finest wiggle, $k\Delta x = \pi$.

::: part
answer: (1 - 2*r)/(1 + 2*r)
tolerance: 0.5%

### What is Crank–Nicolson's amplification factor for it?

::: misses
- [1 - 4*r] That's the explicit scheme.
- [any] $\frac{1 - 2r\sin^2(k\Delta x/2)}{1 + 2r\sin^2(k\Delta x/2)}$.
:::
:::

::: part
answer: 1/(1 + 4*r)
tolerance: 0.5%

### What is backward Euler's?

::: misses
- [any] $\frac{1}{1 + 4r\sin^2(k\Delta x/2)}$.
:::
:::
:::

::: check
kind: derivation
vars: r = 0.1..2, t = 0..3

## Substitute $u_j^n = G^ne^{ij\theta}$ into a heat scheme, with $\theta = k\Delta x$ (write θ as t).

::: step
answer: 1 - 4*r*sin(t/2)^2

### What is G for the explicit scheme?
:::

::: step
answer: 1/(1 + 4*r*sin(t/2)^2)

### And for backward Euler?
:::

::: right
Right. The explicit G drops below −1 once $r > \frac12$; backward Euler's stays between 0 and 1 for every r.
:::
:::

::: explain
## Why does an explicit scheme blow up when its time step is too large, even though each step is a reasonable approximation?

::: model
Every grid function is a sum of Fourier modes, and a linear scheme multiplies each mode by its own factor every step. For the explicit heat scheme that factor is $1 - 4r\sin^2(k\Delta x/2)$. The true equation damps the finest modes hardest, but if $r > \frac12$ the scheme overshoots for those modes: it subtracts too much and flips them with a size greater than 1. Rounding error always puts a little of every mode into the grid, so that finest wiggle grows geometrically, alternating in sign, until it swamps the solution. The accuracy of a single step doesn't matter; what matters is whether errors grow from step to step, which is the stability condition.
:::
:::

::: practice

::: check
given: dx = 0.2 | 3; a = 0.00001 | 0.00005
answer: (dx/1000)^2/(2*a)*1000
unit: ms
tolerance: 0.5%

## Largest explicit Δt, Δx = {dx} mm, α = {a}?

::: misses
- [any] $\Delta x^2/(2\alpha)$.
:::
:::

::: check
given: r = 0.1 | 0.2 | 0.3 | 0.4 | 0.6 | 0.9
answer: 1 - 4*r
tolerance: 0.5%

## Finest-mode amplification at r = {r}?

::: misses
- [any] $1 - 4r$.
:::
:::

::: check
given: dx = 1 | 2 | 5 | 10 | 20 | 50
answer: dx*1e-3/3e8*1e12
unit: ps
tolerance: 0.5%

## 1D FDTD Courant limit, {dx} mm cells?

::: misses
- [any] $\Delta x/c$.
:::
:::
:::
