TExcelTAD
Home › Numerical Methods › Implicit Schemes and Crank-Nicolson

The Heat Equation: Implicit Methods

Explicit time stepping for diffusion is held hostage by its stability limit τ≤h22κ\tau \le \frac{h^2}{2\kappa}. 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:

uik+1−uikτ=κh2(ui−1k+1−2uik+1+ui+1k+1)⟹(I−τκD) uk+1=uk.\frac{u_i^{k+1} - u_i^k}{\tau} = \frac{\kappa}{h^2}\left(u_{i-1}^{k+1} - 2u_i^{k+1} + u_{i+1}^{k+1}\right) \quad\Longrightarrow\quad (I - \tau\kappa D)\,\mathbf{u}^{k+1} = \mathbf{u}^k.

The amplification factor for each mode is 11−τκλj\frac{1}{1 - \tau\kappa\lambda_j}, between 0 and 1 for every τ>0\tau > 0: 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:

(I−τκ2D)uk+1=(I+τκ2D)uk.\left(I - \tfrac{\tau\kappa}{2}D\right)\mathbf{u}^{k+1} = \left(I + \tfrac{\tau\kappa}{2}D\right)\mathbf{u}^k.
python · try it yourself
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 rr up to 200, 400 times beyond the explicit limit r≤12r \le \frac{1}{2}. Backward Euler's error halves when τ\tau halves (first order); Crank-Nicolson's falls by roughly 4 (second order).

Crank-Nicolson's weakness

Crank-Nicolson's amplification factor 1+z/21−z/2\frac{1 + z/2}{1 - z/2} tends to −1-1 for very stiff modes (z=τκλ→−∞z = \tau\kappa\lambda \to -\infty). 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").

python · try it yourself
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, O(m)O(m), 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) or factorized. 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 τκλ=−100\tau\kappa\lambda = -100, compute the amplification factor of FTCS, backward Euler and Crank-Nicolson.

Answer

−99-99, 1101≈0.0099\frac{1}{101} \approx 0.0099, −4951≈−0.96\frac{-49}{51} \approx -0.96.

Solution

FTCS: 1+z1 + z. BE: 11−z\frac{1}{1 - z}. CN: 1+z/21−z/2=−4951\frac{1 + z/2}{1 - z/2} = \frac{-49}{51}.

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 ut=uxx+1u_t = u_{xx} + 1 on [0,1][0, 1] with u=0u = 0 at both ends from u=0u = 0 with backward Euler, τ=0.05\tau = 0.05, until t=2t = 2. Compare with the steady state u=x(1−x)2u = \frac{x(1 - x)}{2} at x=0.5x = 0.5.

Answer

u(0.5)≈0.125u(0.5) \approx 0.125, the steady value.

Solution
python · try it yourself
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…

  1. stable only for r≤1/2r \le 1/2
  2. unconditionally stable
  3. unstable
  4. second order in time

Crank-Nicolson is accurate to…

  1. O(τ+h2)O(\tau + h^2)
  2. O(τ2+h2)O(\tau^2 + h^2)
  3. O(τ)O(\tau)
  4. O(h)O(h)

Crank-Nicolson with large steps on discontinuous data…

  1. is exact
  2. can produce slowly decaying oscillations
  3. blows up
  4. is identical to backward Euler