The Heat Equation: Explicit Methods
The heat equation describes diffusion: heat spreading through a rod, a dye spreading in water, a price option smoothing out. The method of lines discretizes space with finite differences, turning the PDE into a large system of ODEs, and then steps in time. With explicit Euler in time, the method is simple but only stable for very small time steps. The theory and exact solutions are in The Heat Equation.
Method of lines
On a grid , , with boundary values fixed, replace by the centered second difference:
or with the tridiagonal matrix .
FTCS: forward time, centered space
Euler's method with time step gives
import numpy as np
kappa, L, m = 1.0, 1.0, 50
h = L / m
x = np.linspace(0, L, m + 1)
u0 = np.sin(np.pi * x) + 0.5 * np.sin(3 * np.pi * x) # exact solution known
def ftcs(r, T):
tau = r * h**2 / kappa
steps = int(round(T / tau))
u = u0.copy()
for _ in range(steps):
u[1:-1] = u[1:-1] + r * (u[:-2] - 2 * u[1:-1] + u[2:])
return u, steps * tau, steps
for r in (0.25, 0.5, 0.52):
u, T, steps = ftcs(r, 0.1)
exact = np.exp(-np.pi**2 * T) * np.sin(np.pi * x) + 0.5 * np.exp(-9 * np.pi**2 * T) * np.sin(3 * np.pi * x)
print(f"r = {r}: {steps:4d} steps, max error at t = {T:.3f}: {np.max(np.abs(u - exact)):.2e}")r = 0.25: 1000 steps, max error at t = 0.100: 5.96e-05
r = 0.5: 500 steps, max error at t = 0.100: 2.39e-04
r = 0.52: 481 steps, max error at t = 0.100: 1.46e-01
Just above the error jumps by three orders of magnitude, and it keeps growing exponentially with every further step.
The stability condition
The eigenvalues of are , as negative as about . Euler is stable when for all of them (Euler's Method):
This is a severe restriction: halving for accuracy forces to shrink by 4, so the work to reach a fixed time grows like in 1D (and worse in 2D and 3D). The semi-discrete system is stiff (Stiff Equations).
import numpy as np
for m in (10, 20, 40, 80):
h = 1 / m
D = (np.diag(-2 * np.ones(m - 1)) + np.diag(np.ones(m - 2), 1) + np.diag(np.ones(m - 2), -1)) / h**2
lam = np.linalg.eigvalsh(D)
print(f"m = {m:2d}: most negative eigenvalue {lam.min():10.1f} (-4/h^2 = {-4 / h**2:8.0f}), max stable tau {2 / abs(lam.min()):.2e}")m = 10: most negative eigenvalue -390.2 (-4/h^2 = -400), max stable tau 5.13e-03
m = 20: most negative eigenvalue -1590.2 (-4/h^2 = -1600), max stable tau 1.26e-03
m = 40: most negative eigenvalue -6390.1 (-4/h^2 = -6400), max stable tau 3.13e-04
m = 80: most negative eigenvalue -25590.1 (-4/h^2 = -25600), max stable tau 7.82e-05
Accuracy
FTCS is first order in time and second order in space, . With forced by stability, both errors are , so the method is balanced but slow. For long simulations or fine grids, implicit methods win (The Heat Equation: Implicit Methods).
In software
- Explicit stencils are trivially parallel (each new value depends on three old ones), which makes them attractive on GPUs, where the small step can be affordable.
- Image blurring by repeated averaging of neighbouring pixels is exactly FTCS for 2D diffusion with or so.
Exercises
Exercise 1: Maximum time step
For (m/s) and mm, what is the largest stable FTCS time step?
Answer
s.
Solution
s.
Exercise 2: Cost scaling
If halving is needed for accuracy, by what factor does the FTCS work grow in 1D?
Answer
Solution
Twice as many grid points, and four times as many time steps.
Exercise 3: Insulated ends (code)
With insulated ends (), the total heat is conserved. Implement FTCS with ghost points (, ) and check that the trapezoid sum of stays constant.
Answer
The total heat stays constant to rounding error while the profile flattens to its mean.
Solution
import numpy as np
m = 40; h = 1 / m; r = 0.4
x = np.linspace(0, 1, m + 1)
u = np.where(x < 0.3, 1.0, 0.0)
total = lambda u: h * (u.sum() - (u[0] + u[-1]) / 2)
T0 = total(u)
for _ in range(4000):
g = np.r_[u[1], u, u[-2]] # ghost points
u = u + r * (g[:-2] - 2 * u + g[2:])
print(f"total heat {T0:.12f} -> {total(u):.12f}; spread of u now {u.max() - u.min():.1e}")total heat 0.287500000000 -> 0.287500000000; spread of u now 5.1e-05
Test yourself
FTCS for is stable when…
- always
The method of lines turns a PDE into…
- one algebraic equation
- a system of ODEs in time
- an eigenvalue problem
- an integral equation
Why is the semi-discrete heat equation stiff?
- It is nonlinear
- Its eigenvalues range from about to
- It has complex eigenvalues
- It is not stiff