The Heat Equation: Implicit Methods
Explicit time stepping for diffusion is held hostage by its stability limit . Implicit methods are unconditionally stable for the heat equation: the step size can be chosen for accuracy alone. Each step requires solving a linear system, but for the heat equation it is tridiagonal in 1D (sparse in 2D and 3D), so the cost per step stays low.
Backward Euler
Evaluating the spatial difference at the new time level:
The amplification factor for each mode is , between 0 and 1 for every : unconditionally stable. It is first order in time.
Crank-Nicolson
Averaging the two time levels (the trapezoid rule in time) gives second order in both time and space:
import numpy as np
import scipy.sparse as sp
from scipy.sparse.linalg import splu
kappa, m = 1.0, 100
h = 1 / m
x = np.linspace(0, 1, m + 1)
D = sp.diags([1, -2, 1], [-1, 0, 1], shape=(m - 1, m - 1)) / h**2
I = sp.identity(m - 1)
u0 = np.sin(np.pi * x)[1:-1]
T = 0.1
exact = np.exp(-np.pi**2 * T) * np.sin(np.pi * x[1:-1])
def run(method, tau):
steps = int(round(T / tau))
if method == "BE":
lhs, rhs = I - tau * kappa * D, I
else:
lhs, rhs = I - tau * kappa / 2 * D, I + tau * kappa / 2 * D
solver = splu(lhs.tocsc()) # factor once, reuse every step
u = u0.copy()
for _ in range(steps):
u = solver.solve(rhs @ u)
return np.max(np.abs(u - exact))
print(" tau backward Euler Crank-Nicolson (r = tau/h^2)")
for tau in (0.02, 0.01, 0.005, 0.0025):
print(f"{tau:.4f} {run('BE', tau):.3e} {run('CN', tau):.3e} {tau / h**2:.0f}") tau backward Euler Crank-Nicolson (r = tau/h^2)
0.0200 3.359e-02 1.169e-03 200
0.0100 1.746e-02 2.686e-04 100
0.0050 8.922e-03 4.440e-05 50
0.0025 4.522e-03 1.159e-05 25
Both are stable with up to 200, 400 times beyond the explicit limit . Backward Euler's error halves when halves (first order); Crank-Nicolson's falls by roughly 4 (second order).
Crank-Nicolson's weakness
Crank-Nicolson's amplification factor tends to for very stiff modes (). With large steps, sharp initial data (a discontinuity) produce slowly decaying oscillations, while backward Euler smooths them immediately. A common remedy is a few backward Euler steps at the start ("Rannacher smoothing").
import numpy as np
import scipy.sparse as sp
from scipy.sparse.linalg import splu
m = 100; h = 1 / m; tau = 0.01
x = np.linspace(0, 1, m + 1)[1:-1]
D = sp.diags([1, -2, 1], [-1, 0, 1], shape=(m - 1, m - 1)) / h**2
I = sp.identity(m - 1)
u_cn = np.where(np.abs(x - 0.5) < 0.1, 1.0, 0.0) # discontinuous initial data
u_be = u_cn.copy()
cn = splu((I - tau / 2 * D).tocsc()); be = splu((I - tau * D).tocsc())
B = I + tau / 2 * D
for k in range(3):
u_cn = cn.solve(B @ u_cn); u_be = be.solve(u_be)
wiggle = lambda u: int(np.sum(np.diff(np.sign(np.diff(u))) != 0))
print(f"step {k + 1}: CN min {u_cn.min():+.3f} (turning points {wiggle(u_cn)}), BE min {u_be.min():+.4f} (turning points {wiggle(u_be)})")step 1: CN min +0.001 (turning points 5), BE min +0.0017 (turning points 1)
step 2: CN min -0.075 (turning points 7), BE min +0.0039 (turning points 1)
step 3: CN min -0.102 (turning points 5), BE min +0.0056 (turning points 1)
The true solution is positive and has a single bump; Crank-Nicolson shows negative values and spurious wiggles, backward Euler does not.
Cost per step
In 1D each implicit step is a tridiagonal solve, , about the same as an explicit step. In 2D and 3D the systems are larger and sparse; a sparse LU factorization (computed once) or an iterative solver handles them (Sparse Matrices, Conjugate Gradients).
In software
scipy.sparse.linalg.splu(factor once, solve many times) orfactorized. For time-dependent coefficients, the matrix changes and must be refactored.solve_ivp(method="BDF", jac_sparsity=...)applied to the method-of-lines system automates implicit stepping with adaptive steps.
Exercises
Exercise 1: Amplification factors
For a mode with , compute the amplification factor of FTCS, backward Euler and Crank-Nicolson.
Answer
, , .
Solution
FTCS: . BE: . CN: .
Exercise 2: Why factor once?
Why is splu called outside the time loop?
Answer
The matrix is the same every step, so one factorization serves all steps.
Solution
Factorization dominates the cost; each step then needs only forward and backward substitution.
Exercise 3: Source term (code)
Solve on with at both ends from with backward Euler, , until . Compare with the steady state at .
Answer
, the steady value.
Solution
import numpy as np
import scipy.sparse as sp
from scipy.sparse.linalg import splu
m = 50; h = 1 / m; tau = 0.05
D = sp.diags([1, -2, 1], [-1, 0, 1], shape=(m - 1, m - 1)) / h**2
solver = splu((sp.identity(m - 1) - tau * D).tocsc())
u = np.zeros(m - 1)
for _ in range(40):
u = solver.solve(u + tau * 1.0)
print(round(float(u[m // 2 - 1]), 6))0.125
Test yourself
Backward Euler for the heat equation is…
- stable only for
- unconditionally stable
- unstable
- second order in time
Crank-Nicolson is accurate to…
Crank-Nicolson with large steps on discontinuous data…
- is exact
- can produce slowly decaying oscillations
- blows up
- is identical to backward Euler