TExcelTAD
Home › Numerical Methods › The Heat Equation: Method of Lines

The Heat Equation: Explicit Methods

The heat equation ut=κuxxu_t = \kappa u_{xx} 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 xi=ihx_i = ih, i=0,…,mi = 0, \ldots, m, with boundary values fixed, replace uxxu_{xx} by the centered second difference:

duidt=κh2(ui−1−2ui+ui+1),i=1,…,m−1,\frac{du_i}{dt} = \frac{\kappa}{h^2}\left(u_{i-1} - 2u_i + u_{i+1}\right), \qquad i = 1, \ldots, m - 1,

or u′=κDu\mathbf{u}' = \kappa D\mathbf{u} with the tridiagonal matrix D=1h2tridiag⁡(1,−2,1)D = \frac{1}{h^2}\operatorname{tridiag}(1, -2, 1).

FTCS: forward time, centered space

Euler's method with time step τ\tau gives

uik+1=uik+r(ui−1k−2uik+ui+1k),r=κτh2.u_i^{k+1} = u_i^k + r\left(u_{i-1}^k - 2u_i^k + u_{i+1}^k\right), \qquad r = \frac{\kappa\tau}{h^2}.
python · try it yourself
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 r=12r = \frac{1}{2} the error jumps by three orders of magnitude, and it keeps growing exponentially with every further step.

The stability condition

The eigenvalues of DD are λj=−4h2sin⁡2jπh2L\lambda_j = -\frac{4}{h^2}\sin^2\frac{j\pi h}{2L}, as negative as about −4h2-\frac{4}{h^2}. Euler is stable when τκ∣λ∣≤2\tau\kappa\lvert\lambda\rvert \le 2 for all of them (Euler's Method):

r=κτh2≤12.r = \frac{\kappa\tau}{h^2} \le \frac{1}{2}.

This is a severe restriction: halving hh for accuracy forces τ\tau to shrink by 4, so the work to reach a fixed time grows like h−3h^{-3} in 1D (and worse in 2D and 3D). The semi-discrete system is stiff (Stiff Equations).

python · try it yourself
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, O(τ+h2)O(\tau + h^2). With τ∝h2\tau \propto h^2 forced by stability, both errors are O(h2)O(h^2), 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 r=14r = \frac{1}{4} or so.

Exercises

Exercise 1: Maximum time step

For κ=10−4\kappa = 10^{-4} (m2^2/s) and h=1h = 1 mm, what is the largest stable FTCS time step?

Answer

5×10−35 \times 10^{-3} s.

Solution

τ≤h22κ=10−62×10−4=0.005\tau \le \frac{h^2}{2\kappa} = \frac{10^{-6}}{2 \times 10^{-4}} = 0.005 s.

Exercise 2: Cost scaling

If halving hh 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 (ux=0u_x = 0), the total heat ∫u dx\int u\,dx is conserved. Implement FTCS with ghost points (u−1=u1u_{-1} = u_1, um+1=um−1u_{m+1} = u_{m-1}) and check that the trapezoid sum of uu stays constant.

Answer

The total heat stays constant to rounding error while the profile flattens to its mean.

Solution
python · try it yourself
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 ut=κuxxu_t = \kappa u_{xx} is stable when…

  1. κτ/h2≤1/2\kappa\tau/h^2 \le 1/2
  2. τ≤h\tau \le h
  3. always
  4. κτ/h≤1\kappa\tau/h \le 1

The method of lines turns a PDE into…

  1. one algebraic equation
  2. a system of ODEs in time
  3. an eigenvalue problem
  4. an integral equation

Why is the semi-discrete heat equation stiff?

  1. It is nonlinear
  2. Its eigenvalues range from about −π2-\pi^2 to −4/h2-4/h^2
  3. It has complex eigenvalues
  4. It is not stiff