The Wave Equation
The wave equation governs vibrating strings, sound, and light in simple media. Unlike diffusion, waves carry information at a finite speed 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:
with the Courant number . It is second order in time and space. The first step uses the initial velocity : .
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 depends only on grid values within distance per step, a numerical domain of dependence of speed . The true solution depends on data up to distance per step. If , the scheme cannot possibly see all the data that determine the solution, so it cannot converge. Courant, Friedrichs and Lewy's condition:
For leapfrog this is also exactly the stability limit. Notice in the output that is exact in 1D: the scheme then reproduces d'Alembert's solution on the grid.
Dispersion
For , 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 (not as for diffusion) is why explicit schemes are standard for waves: the stability limit matches the accuracy needs.
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 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 m/s is discretized with mm. What is the largest stable time step?
Answer
s.
Solution
.
Exercise 2: 2D CFL
In 2D with equal spacing in both directions, the CFL limit for the 5-point leapfrog scheme is . By what factor must shrink compared with 1D?
Answer
.
Solution
In dimensions the limit is .
Exercise 3: Standing wave (code)
With , zero initial velocity and fixed ends, the exact solution is . Run leapfrog with , to (one period) and report the max error.
Answer
Tiny, about : after exactly one period the small phase error of the scheme nearly cancels (at , for example, it is about ).
Solution
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…
- none
Why are explicit methods usually fine for waves but not for diffusion?
- Waves are linear
- The wave stability limit matches accuracy needs, unlike diffusion's
- Waves have no boundary conditions
- They are not
Numerical dispersion means…
- the solution decays
- different wavelengths travel at different numerical speeds
- the scheme is unstable
- energy is conserved