TExcelTAD
Home › Numerical Methods › The Wave Equation and the CFL Condition

The Wave Equation

The wave equation utt=c2uxxu_{tt} = c^2u_{xx} governs vibrating strings, sound, and light in simple media. Unlike diffusion, waves carry information at a finite speed cc and do not decay. A good numerical scheme must respect both: it must not move information faster than the grid allows (the CFL condition), and it should not damp or distort waves (low dissipation and dispersion). The theory and d'Alembert's solution are in The Wave Equation.

The leapfrog scheme

Centered differences in both time and space give the classic explicit scheme:

uik+1−2uik+uik−1τ2=c2ui+1k−2uik+ui−1kh2⟹uik+1=2uik−uik−1+σ2(ui+1k−2uik+ui−1k),\frac{u_i^{k+1} - 2u_i^k + u_i^{k-1}}{\tau^2} = c^2\frac{u_{i+1}^k - 2u_i^k + u_{i-1}^k}{h^2} \quad\Longrightarrow\quad u_i^{k+1} = 2u_i^k - u_i^{k-1} + \sigma^2\left(u_{i+1}^k - 2u_i^k + u_{i-1}^k\right),

with the Courant number σ=cτh\sigma = \frac{c\tau}{h}. It is second order in time and space. The first step uses the initial velocity gg: ui1=ui0+τgi+σ22(ui+10−2ui0+ui−10)u_i^1 = u_i^0 + \tau g_i + \frac{\sigma^2}{2}(u_{i+1}^0 - 2u_i^0 + u_{i-1}^0).

python · try it yourself
import numpy as np

c, L, m = 1.0, 1.0, 200
h = L / m
x = np.linspace(0, L, m + 1)
f = lambda x: np.exp(-((x - 0.5) / 0.05) ** 2)      # a narrow pulse, initially at rest

def leapfrog_wave(sigma, T):
    tau = sigma * h / c
    steps = int(round(T / tau))
    u_old = f(x); u_old[[0, -1]] = 0
    u = u_old.copy()
    u[1:-1] = u_old[1:-1] + sigma**2 / 2 * (u_old[2:] - 2 * u_old[1:-1] + u_old[:-2])
    for _ in range(steps - 1):
        u_new = np.zeros_like(u)
        u_new[1:-1] = 2 * u[1:-1] - u_old[1:-1] + sigma**2 * (u[2:] - 2 * u[1:-1] + u[:-2])
        u_old, u = u, u_new
    return u

T = 0.3                                              # pulses travel 0.3 left and right
exact = 0.5 * (f(x - c * T) + f(x + c * T))           # d'Alembert (no reflection yet)
for sigma in (0.5, 0.9, 1.0, 1.1):
    u = leapfrog_wave(sigma, T)
    print(f"sigma = {sigma}: max error {np.max(np.abs(u - exact)):.3e}, max |u| = {np.max(np.abs(u)):.3e}")
sigma = 0.5: max error 3.674e-03, max |u| = 4.999e-01
sigma = 0.9: max error 1.206e-02, max |u| = 4.996e-01
sigma = 1.0: max error 5.627e-08, max |u| = 5.000e-01
sigma = 1.1: max error 1.214e+04, max |u| = 1.214e+04

The CFL condition

The numerical solution at (xi,tk+1)(x_i, t_{k+1}) depends only on grid values within distance hh per step, a numerical domain of dependence of speed hτ\frac{h}{\tau}. The true solution depends on data up to distance cτc\tau per step. If cτ>hc\tau > h, the scheme cannot possibly see all the data that determine the solution, so it cannot converge. Courant, Friedrichs and Lewy's condition:

σ=cτh≤1.\sigma = \frac{c\tau}{h} \le 1.

For leapfrog this is also exactly the stability limit. Notice in the output that σ=1\sigma = 1 is exact in 1D: the scheme then reproduces d'Alembert's solution on the grid.

Dispersion

For σ<1\sigma < 1, different wavelengths travel at slightly different numerical speeds (short waves lag), so a sharp pulse develops a trailing wiggle. Refining the grid reduces it. The relation τ∝h\tau \propto h (not h2h^2 as for diffusion) is why explicit schemes are standard for waves: the stability limit matches the accuracy needs.

python · try it yourself
import numpy as np

c, m = 1.0, 200
h = 1 / m
x = np.linspace(0, 1, m + 1)
f = lambda x: np.where(np.abs(x - 0.5) < 0.05, 1.0, 0.0)     # a square pulse (discontinuous)
sigma = 0.5; tau = sigma * h
u_old = f(x).astype(float); u = u_old.copy()
u[1:-1] = u_old[1:-1] + sigma**2 / 2 * (u_old[2:] - 2 * u_old[1:-1] + u_old[:-2])
for _ in range(int(round(0.3 / tau)) - 1):
    u_new = np.zeros_like(u)
    u_new[1:-1] = 2 * u[1:-1] - u_old[1:-1] + sigma**2 * (u[2:] - 2 * u[1:-1] + u[:-2])
    u_old, u = u, u_new
print(f"square pulse after t = 0.3: max {u.max():.3f} (exact 0.5), min {u.min():.3f} (exact 0)")
square pulse after t = 0.3: max 0.635 (exact 0.5), min -0.156 (exact 0)

Energy

The discrete energy ∑i[(uik+1−uikτ)2+c2(ui+1k+1−uik+1)(ui+1k−uik)h2]\sum_i\left[\left(\frac{u_i^{k+1} - u_i^k}{\tau}\right)^2 + c^2\frac{(u_{i+1}^{k+1} - u_i^{k+1})(u_{i+1}^k - u_i^k)}{h^2}\right] is exactly conserved by leapfrog, which, like the symplectic ODE methods, prevents long-term drift (Long-Time Integration and Symplectic Methods).

In software

  • Seismic imaging, acoustics and electromagnetics (the FDTD/Yee scheme) use explicit leapfrog-type stencils on huge grids, usually on GPUs.
  • Game water surfaces and cloth often use the same 2D wave stencil with a little damping.

Exercises

Exercise 1: Time step

A string of length 1 m with c=300c = 300 m/s is discretized with h=1h = 1 mm. What is the largest stable time step?

Answer

τ≤3.33×10−6\tau \le 3.33 \times 10^{-6} s.

Solution

τ≤hc=10−3300\tau \le \frac{h}{c} = \frac{10^{-3}}{300}.

Exercise 2: 2D CFL

In 2D with equal spacing hh in both directions, the CFL limit for the 5-point leapfrog scheme is cτh≤12\frac{c\tau}{h} \le \frac{1}{\sqrt{2}}. By what factor must τ\tau shrink compared with 1D?

Answer

12≈0.707\frac{1}{\sqrt{2}} \approx 0.707.

Solution

In dd dimensions the limit is 1d\frac{1}{\sqrt{d}}.

Exercise 3: Standing wave (code)

With u(x,0)=sin⁡(πx)u(x, 0) = \sin(\pi x), zero initial velocity and fixed ends, the exact solution is cos⁡(πct)sin⁡(πx)\cos(\pi ct)\sin(\pi x). Run leapfrog with σ=0.8\sigma = 0.8, m=100m = 100 to t=2t = 2 (one period) and report the max error.

Answer

Tiny, about 4×10−94 \times 10^{-9}: after exactly one period the small phase error of the scheme nearly cancels (at t=0.5t = 0.5, for example, it is about 2×10−52 \times 10^{-5}).

Solution
python · try it yourself
import numpy as np
m = 100; h = 1 / m; sigma = 0.8; tau = sigma * h
x = np.linspace(0, 1, m + 1)
u_old = np.sin(np.pi * x); u = u_old.copy()
u[1:-1] = u_old[1:-1] + sigma**2 / 2 * (u_old[2:] - 2 * u_old[1:-1] + u_old[:-2])
steps = int(round(2 / tau))
for _ in range(steps - 1):
    u_new = np.zeros_like(u)
    u_new[1:-1] = 2 * u[1:-1] - u_old[1:-1] + sigma**2 * (u[2:] - 2 * u[1:-1] + u[:-2])
    u_old, u = u, u_new
print(f"t = {steps * tau:.4f}, max error {np.max(np.abs(u - np.cos(np.pi * steps * tau) * np.sin(np.pi * x))):.2e}")
t = 2.0000, max error 4.33e-09

Test yourself

The CFL condition for the 1D wave equation with leapfrog is…

  1. cτ/h≤1c\tau/h \le 1
  2. cτ/h2≤1/2c\tau/h^2 \le 1/2
  3. τ≤h2\tau \le h^2
  4. none

Why are explicit methods usually fine for waves but not for diffusion?

  1. Waves are linear
  2. The wave stability limit τ∝h\tau \propto h matches accuracy needs, unlike diffusion's τ∝h2\tau \propto h^2
  3. Waves have no boundary conditions
  4. They are not

Numerical dispersion means…

  1. the solution decays
  2. different wavelengths travel at different numerical speeds
  3. the scheme is unstable
  4. energy is conserved