Skip to content

Partial Differential Equations

Partial differential equations extend ODEs: the evolution depends not only on time but also on the environment in a neighborhood.

The most prominent example is the diffusion of temperature in space (heat equation) which, in its simplest one-dimensional form, is given by

\[ \partial_t u = \frac{1}{2}\partial_{xx} u \]

We consider the equation on the interval \([0, 1]\) with Dirichlet boundary conditions \(u(t, 0) = u(t, 1) = 0\) and a given initial condition \(u(0, x) = u_0(x)\). The factor \(1/2\) is the probabilistic normalization: the fundamental solution (heat kernel) is the density of a Brownian motion,

\[ p_t(x, y) = \frac{1}{\sqrt{2\pi t}} e^{-\frac{(x-y)^2}{2t}}, \quad\text{so that}\quad u(t, x) = \int_0^1 p_t(x, y)u_0(y)\,dy. \]

Space Discretization and the Explicit Scheme

We discretize time and space on a grid \(t_n = n\Delta t\) and \(x_j = j\Delta x\) with \(\Delta x = 1/(J+1)\), writing \(u_j^n \approx u(t_n, x_j)\). We approximate the time derivative by a forward difference and the second space derivative by a central second difference

\[ \begin{equation*} \partial_{xx} u(t_n, x_j) \approx \frac{u_{j+1}^n - 2u_j^n + u_{j-1}^n}{\Delta x^2}. \end{equation*} \]

Substituting yields the explicit scheme

\[ \begin{equation*} u_j^{n+1} = u_j^n + \lambda\left(u_{j+1}^n - 2u_j^n + u_{j-1}^n\right), \qquad \lambda := \frac{\Delta t}{2\Delta x^2}. \end{equation*} \]

In matrix form, with the tridiagonal matrix \(T = \mathrm{tridiag}(1, -2, 1)\) (which is symmetric negative semi-definite with eigenvalues \(-4\sin^2(k\pi\Delta x/2)\), \(k = 1, \ldots, J\)), the scheme reads

\[ \begin{equation*} \mathbf{u}^{n+1} = \left(I + \lambda T\right)\mathbf{u}^n = A\,\mathbf{u}^n. \end{equation*} \]

Theorem: CFL Stability Condition (von Neumann analysis)

The explicit scheme is stable in the sense that \(\sup_j |u_j^n|\) remains bounded by \(\sup_j |u_j^0|\) for all \(n\), if and only if

\[ \lambda = \frac{\Delta t}{2\Delta x^2} \leq \frac{1}{2} \qquad\text{equivalently}\qquad \Delta t \leq \Delta x^2. \]
Proof (von Neumann analysis)

Consider a discrete Fourier mode \(\mathbf{e}^{(k)}_j = e^{ij k \pi \Delta x}\). Since \(T\) is symmetric with eigenvectors \(\mathbf{e}^{(k)}\) and eigenvalues \(\mu_k = -4\sin^2(k\pi\Delta x/2)\), the mode is amplified at each step by the factor

\[ g_k = 1 + \lambda \mu_k = 1 - 4\lambda \sin^2\left(\frac{k\pi\Delta x}{2}\right). \]

Stability requires \(|g_k| \leq 1\) for every \(k\), i.e. \(0 \leq 4\lambda\sin^2(\cdot) \leq 2\) for all \(k\), whose most binding case gives \(4\lambda \leq 2\). The claim follows.

Probabilistic interpretation

The scheme rewrites as \(u_j^{n+1} = (1 - 2\lambda)u_j^n + \lambda u_{j+1}^n + \lambda u_{j-1}^n\): a weighted average of neighboring values. For the maximal stable choice \(\lambda = 1/2\),

\[ u_j^{n+1} = \frac{1}{2}\left(u_{j+1}^n + u_{j-1}^n\right), \]

which is exactly the dynamic programming / martingale property of the symmetric random walk of the CRR model: the scheme computes \(u_j^n = E[u_0(j\Delta x + X_{n\Delta t})]\) for a random walk with \(\pm\Delta x\) steps, the discrete analogue of the Brownian representation \(u(t, x) = E[u_0(x + B_t)]\). The CFL condition is precisely the requirement that the weights stay positive, i.e. that the scheme remains a convex combination — a discrete analogue of the maximum principle.

Implicit Schemes

Choosing the space derivative at time \(t_{n+1}\) instead gives the implicit scheme

\[ \begin{equation*} \left(I - \lambda T\right)\mathbf{u}^{n+1} = \mathbf{u}^n, \end{equation*} \]

whose amplification factor \(g_k = (1 + 4\lambda\sin^2(\cdot))^{-1}\) satisfies \(|g_k| \leq 1\) for every \(\lambda\): the scheme is unconditionally stable, at the cost of solving a tridiagonal linear system at each step (\(O(J)\) with the Thomas algorithm, implemented in scipy.linalg.solve_banded).

Averaging the two evaluations at times \(t_n\) and \(t_{n+1}\) yields the Crank–Nicolson scheme

\[ \begin{equation*} \left(I - \frac{\lambda}{2} T\right)\mathbf{u}^{n+1} = \left(I + \frac{\lambda}{2} T\right)\mathbf{u}^n, \end{equation*} \]

which is second-order accurate in time (error \(O(\Delta t^2 + \Delta x^2)\), against \(O(\Delta t + \Delta x^2)\) for the first two) and still unconditionally stable.

Theorem: Lax Equivalence Principle

For a consistent scheme approximating a well-posed linear evolution equation, convergence (as \(\Delta t, \Delta x \to 0\)) holds if and only if the scheme is stable. Consistency alone is not enough: the explicit scheme with \(\lambda > 1/2\) diverges although it is consistent.

Implementation

We take a Gaussian initial condition and compare the schemes with the exact heat-kernel solution.

import numpy as np
import plotly.graph_objs as go

# grid
J = 99
dx = 1 / (J + 1)
x = np.linspace(dx, 1 - dx, J)
T_end = 0.05

# Gaussian initial condition
u0 = np.exp(-200 * (x - 0.5) ** 2)

def tridiag_matrix(J, lam):
    """Matrix lam * T for the scheme."""
    main = -2 * np.ones(J)
    off = np.ones(J - 1)
    return lam * (np.diag(main) + np.diag(off, 1) + np.diag(off, -1))

def explicit(u0, dx, T_end, lam):
    """Explicit scheme; lam must satisfy lam <= 1/2 for stability."""
    dt = 2 * lam * dx ** 2
    n = int(T_end / dt) + 1
    A = np.eye(J) + tridiag_matrix(J, lam)
    u = u0.copy()
    for _ in range(n):
        u = A.dot(u)
        u[0], u[-1] = 0, 0   # boundary conditions
    return u

def implicit(u0, dx, T_end, lam):
    """Implicit scheme (unconditionally stable)."""
    from scipy.linalg import solve_banded
    dt = 2 * lam * dx ** 2
    n = int(T_end / dt) + 1
    ab = np.zeros((3, J))
    ab[0, 1:] = -lam
    ab[1, :] = 1 + 2 * lam
    ab[2, :-1] = -lam
    u = u0.copy()
    for _ in range(n):
        u = solve_banded((1, 1), ab, u)
        u[0], u[-1] = 0, 0
    return u

def exact(u0, x, t):
    """Heat kernel solution by quadrature against the initial data."""
    y = x
    kernel = (1 / np.sqrt(2 * np.pi * t)) * np.exp(-(x[:, None] - y[None, :]) ** 2 / (2 * t))
    dy = y[1] - y[0]
    return kernel.dot(u0) * dy

fig = go.Figure()
fig.add_scatter(x=x, y=u0, name="initial condition")
for lam, name in [(0.5, "explicit lambda=0.5"), (0.25, "explicit lambda=0.25"), (5.0, "implicit lambda=5")]:
    u = (explicit if "explicit" in name else implicit)(u0, dx, T_end, lam)
    fig.add_scatter(x=x, y=u, name=name)
fig.add_scatter(x=x, y=exact(u0, x, T_end), name="exact", line=dict(dash="dot"))
fig.show()
Try the unstable regime

Run the explicit scheme with \(\lambda = 0.51\): within a few dozen steps the numerical solution oscillates with alternating signs and blows up exponentially — the discrete counterpart of the loss of the maximum principle. Plot the sup-norm \(\max_j |u_j^n|\) as a function of \(n\) in both regimes to see stability versus divergence.

The tridiagonal structure means each implicit step costs the same order as an explicit one, so in one dimension Crank–Nicolson is the method of choice: unconditional stability with second-order accuracy in time. In higher dimensions the matrix becomes banded but no longer tridiagonal, and one switches to operator splitting (ADI) or iterative solvers.