Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Time-Dependent PDEs

Open in Colab

Partial differential equations

A partial differential equation (PDE) relates an unknown function of several variables to its partial derivatives. The partial derivative ∂u/∂x\partial u/\partial x differentiates with respect to xx while the other variables are held fixed, and we often write it with a subscript, uxu_x; likewise ut=∂u/∂tu_t = \partial u/\partial t and uxx=∂2u/∂x2u_{xx} = \partial^2 u/\partial x^2. For a function u(x,t)u(x, t) of one space variable and time, the standard examples are

equationnamedescribes
ut=D uxxu_t = D\,u_{xx}diffusion (heat) equationheat spreading along a bar, a chemical spreading in a tube
utt=c2 uxxu_{tt} = c^2\,u_{xx}wave equationa vibrating string, sound
ut+c ux=0u_t + c\,u_x = 0advection equationa profile carried along by a flow of speed cc

with a diffusion coefficient D>0D > 0 and a wave speed c>0c > 0. The steady states of the diffusion equation, where ut=0u_t = 0, satisfy the Laplace or Poisson equation Boundary Value Problems.

A PDE alone has infinitely many solutions. To single one out on an interval 0≤x≤L0 \le x \le L we need an initial condition u(x,0)=u0(x)u(x, 0) = u_0(x) (for the wave equation, which is second order in time, also the initial velocity ut(x,0)=v0(x)u_t(x, 0) = v_0(x)) and a boundary condition at each end. The three standard choices are:

  • Dirichlet: the value is given, e.g. u(0,t)=0u(0, t) = 0. This models a bar whose end is held at a fixed temperature, or a tube whose open end sits in a large bath that fixes the concentration there.

  • Neumann: the derivative is given, e.g. ux(0,t)=0u_x(0, t) = 0. The flux of heat or of a chemical is −D ux-D\,u_x , so ux=0u_x = 0 means that nothing flows through the end, as at an insulated end of a bar or a closed end of a tube.

  • Periodic: u(0,t)=u(L,t)u(0, t) = u(L, t) and ux(0,t)=ux(L,t)u_x(0, t) = u_x(L, t), so the two ends are joined into a ring.

Analytical solutions

Separation of variables

The diffusion equation ut=D uxxu_t = D\,u_{xx} can be solved by separation of variables. We look for solutions of the form u(x,t)=X(x) T(t)u(x, t) = X(x)\,T(t), a function of space times a function of time. Substituting this into the PDE and dividing by D X(x) T(t)D\,X(x)\,T(t) gives

X′′(x)X(x)=T′(t)D T(t).\frac{X''(x)}{X(x)} = \frac{T'(t)}{D\,T(t)}.

The left side does not depend on tt and the right side does not depend on xx, so both sides are equal to the same constant, which we call −b2-b^2. This leaves two ODEs,

X′′=−b2X,T′=−b2D T.X'' = -b^2 X, \qquad T' = -b^2 D\, T.

SymPy can carry out both steps: pde_separate substitutes the product into the PDE and returns the two sides, and dsolve (see Boundary Value Problems) solves the ODEs.

Multiplying the two, with the constant in TT absorbed into C1C_1 and C2C_2, gives the solutions

u(x,t)=e−b2Dt (C1sin⁡bx+C2cos⁡bx).u(x, t) = e^{-b^2 D t}\,(C_1 \sin bx + C_2 \cos bx).

checkpdesol substitutes a candidate solution into the PDE and simplifies what is left over; it returns (True, 0) when the residual is zero.

Boundary conditions

The boundary conditions decide which frequencies bb are allowed. With both ends held at zero, X(0)=0X(0) = 0 forces C2=0C_2 = 0, and then X(L)=C1sin⁡bL=0X(L) = C_1 \sin bL = 0 with C1≠0C_1 \neq 0 forces bLbL to be a multiple of π\pi. The allowed solutions are

sin⁡nπxL  e−D(nπ/L)2t,n=1,2,3,…\sin\frac{n\pi x}{L}\; e^{-D (n\pi/L)^2 t}, \qquad n = 1, 2, 3, \dots

The problem X′′=−b2XX'' = -b^2 X with X(0)=X(L)=0X(0) = X(L) = 0 is the eigenvalue problem for d2/dx2d^2/dx^2 with zero boundary values, and the sines are its eigenfunctions; sampled on a grid, they appeared as the eigenvectors of the second-difference matrix in Boundary Value Problems.

The PDE is linear, so the general solution is the Fourier sine series

u(x,t)=∑n=1∞Bn e−D(nπ/L)2tsin⁡nπxL.u(x, t) = \sum_{n=1}^{\infty} B_n\, e^{-D (n\pi/L)^2 t} \sin\frac{n\pi x}{L}.

At t=0t = 0 the series has to reproduce the initial condition, u0(x)=∑nBnsin⁡(nπx/L)u_0(x) = \sum_n B_n \sin(n\pi x/L). To find the coefficients we use the fact that the sines are orthogonal on [0,L][0, L], which SymPy confirms for positive integers mm and nn:

Multiplying u0u_0 by sin⁡(nπx/L)\sin(n\pi x/L) and integrating over [0,L][0, L] therefore picks out a single coefficient,

Bn=2L∫0Lu0(x)sin⁡nπxL dx.B_n = \frac{2}{L} \int_0^L u_0(x) \sin\frac{n\pi x}{L}\, dx.

Other boundary conditions select other modes. With insulated ends, X′(0)=X′(L)=0X'(0) = X'(L) = 0 forces C1=0C_1 = 0 and again bL=nπbL = n\pi, so the modes are cosines, and now b=0b = 0 is allowed as well, which gives the constant mode. With periodic boundaries XX must repeat with period LL, so bLbL is a multiple of 2π2\pi, and both sines and cosines survive.

Example: a block of heat

Suppose the middle half of a bar starts at temperature 1 and the rest at 0, with both ends held at 0: u0=1u_0 = 1 on (L/4,3L/4)(L/4, 3L/4) and u0=0u_0 = 0 elsewhere. The integral for BnB_n only runs over the block, and SymPy evaluates it for a general nn.

The coefficients with even nn vanish, since the block is symmetric about x=L/2x = L/2 and the even sines are antisymmetric about it; the others decay only like 1/n1/n, because u0u_0 jumps. To evaluate the series numerically we set L=1L = 1 and D=0.1D = 0.1, turn one term into a NumPy function with lambdify (see Symbolic Computing), and add up the first nterms terms.

At t=0t = 0 the partial sums converge slowly, since BnB_n decays only like 1/n1/n, and near each jump they overshoot by about 9% however many terms we keep. This is also called the Gibbs phenomenon. For t>0t > 0 the factor e−D(nπ/L)2te^{-D (n\pi/L)^2 t} damps the high frequencies, so the corners are rounded off almost at once and the solution is smooth even though u0u_0 is not. The same factor makes the series converge quickly.

Waves: d’Alembert’s solution

For the wave equation utt=c2uxxu_{tt} = c^2 u_{xx} on the whole line, every function of x−ctx - ct is a solution: f(x−ct)f(x - ct) is the profile ff moving to the right at speed cc without changing its shape. Likewise, g(x+ct)g(x + ct) moves to the left.

In fact every solution has this form, and choosing ff and gg to match the initial position u0u_0 and the initial velocity v0v_0 gives d’Alembert’s formula

u(x,t)=u0(x−ct)+u0(x+ct)2+12c∫x−ctx+ctv0(s) ds.u(x, t) = \frac{u_0(x - ct) + u_0(x + ct)}{2} + \frac{1}{2c} \int_{x - ct}^{x + ct} v_0(s)\, ds.

A bump released at rest (v0=0v_0 = 0) splits into two copies of half the height, which move apart at speed cc.

The wave equation keeps the shape of its initial data, corners and jumps included, and carries it at the finite speed cc; the value u(x,t)u(x, t) depends only on the initial data in [x−ct,x+ct][x - ct, x + ct]. The diffusion equation instead smooths its initial data at once and, as the heat kernel showed, is felt everywhere immediately.

On [0,L][0, L] with fixed ends, separation of variables gives standing waves such as sin⁡(nπx/L)cos⁡(nπct/L)\sin(n\pi x/L)\cos(n\pi c t/L), the oscillating solutions found from the eigenvectors in Boundary Value Problems.

First-order equations: pdsolve

SymPy’s PDE solver pdsolve handles first-order linear equations. For the advection equation ut+c ux=0u_t + c\,u_x = 0 it returns an arbitrary function FF of x−ctx - ct:

The solution is constant along each line x−ct=constx - ct = \text{const} (a “characteristic”), so the initial profile moves to the right at speed cc, like the right-moving half of d’Alembert’s solution. Spectral Methods uses this equation to test numerical schemes, because after one trip around a periodic domain the exact solution is the initial condition again.

With a decay term, ut+c ux=−auu_t + c\,u_x = -a u, the solution by hand is F(x−ct) e−atF(x - ct)\,e^{-at}, but SymPy writes it differently:

Both are correct. With ξ=x−ct\xi = x - ct we have cx+t=cξ+(c2+1)tcx + t = c\xi + (c^2 + 1)t, so SymPy’s exponential equals e−acξ/(c2+1) e−ate^{-ac\xi/(c^2 + 1)}\, e^{-at}, and the factor that depends only on ξ\xi can be absorbed into the arbitrary function FF. Symbolic results often come back in an unfamiliar but equivalent form, and checkpdesol is a more reliable way to compare them than reading the formulas.

For second-order equations pdsolve gives up:

Discretizing space

From here on we solve the diffusion equation

∂u∂t=D ∂2u∂x2,x∈[0,L],\frac{\partial u}{\partial t} = D\,\frac{\partial^2 u}{\partial x^2}, \qquad x \in [0, L],

numerically. Put down nn equally spaced points xix_i with spacing Δx\Delta x, and let ui(t)u_i(t) approximate u(xi,t)u(x_i, t). The standard second-order approximation to the second derivative is the second difference

∂2u∂x2∣xi≈ui−1−2ui+ui+1Δx2.\frac{\partial^2 u}{\partial x^2}\bigg|_{x_i} \approx \frac{u_{i-1} - 2u_i + u_{i+1}}{\Delta x^2}.

which you can get by adding the Taylor expansions of u(xi±Δx)u(x_i \pm \Delta x). We build this similar to Boundary Value Problems, with a more detailed treatment of the boundary conditoin.

We can check our matrix by applying it to known examples. The second difference should turn sin⁡(kx)\sin(kx) into −k2sin⁡(kx)-k^2\sin(kx), so applying the matrix to a sampled sine should scale it by roughly −k2-k^2.

From PDE to ODE

With space discretized, uu is a vector and the PDE reads

dudt=D A u,\frac{d\mathbf{u}}{dt} = D\,A\,\mathbf{u},

an ODE system with one equation per grid point. We solve it using solve_ivp. Discretizing space while leaving time continuous is called the method of lines.

Two bumps spread, merge, and drain away through the ends. That is roughly fifteen lines of code for a PDE solve, which is the appeal of the method.

What the boundary conditions do

The three bc options differ only in what they assume sits just outside the domain, but they produce qualitatively different physics. A good way to see it is to track the total mass, ∫01u(x,t) dx≈∑iui(t)Δx \int_0^1 u(x,t) \,\mathrm{d} x \approx \sum_i u_i(t) \Delta x.

Dirichlet leaks -- the ends are held at zero, so material leaves. Neumann and periodic conserve the total to several digits, because nothing can escape.

That conservation is not a coincidence, and it is the single most useful test you can write for this kind of solver. For Neumann and periodic boundaries the column sums of AA are zero, so 1⊤A=0\mathbf{1}^\top A = 0, so ddt∑iui=0\frac{d}{dt}\sum_i u_i = 0 exactly, in exact arithmetic, for any initial condition. A test that asserts conservation to near machine precision will pass for a correct implementation and fail for almost any indexing slip.

Stability

solve_ivp picked its own time steps above. Suppose instead we step forward by hand with the simplest possible scheme, forward Euler:

u(t+Δt)≈u(t)+Δt DAu(t).\mathbf{u}(t + \Delta t) \approx \mathbf{u}(t) + \Delta t\, D A \mathbf{u}(t).

Each step multiplies by I+ΔtDAI + \Delta t D A. Writing u\mathbf{u} in the eigenbasis of AA, the component belonging to eigenvalue λ\lambda is multiplied by 1+ΔtDλ1 + \Delta t D \lambda every step, so it stays bounded only if

∣1+Δt Dλ∣≤1.|1 + \Delta t\, D \lambda| \le 1 .

The eigenvalues of the second-difference matrix run from near 0 down to about −4/Δx2-4/\Delta x^2. The eigenvectors are the sines sin⁡(nπx/L)\sin(n\pi x/L) of the exact solution, sampled on the grid, and for small nn the eigenvalues are close to −(nπ/L)2-(n\pi/L)^2, so −Dλ-D\lambda is the decay rate of that mode. Plugging in λ≈−4/Δx2\lambda \approx -4/\Delta x^2 gives

Δt≤Δx22D,\Delta t \le \frac{\Delta x^2}{2D},

Halving Δx\Delta x forces you to quarter Δt\Delta t. Refining the mesh gets expensive fast.

Below the threshold the answer is a sensible ∼0.05\sim 0.05. A 5% overshoot gives 22, and a 20% overshoot gives 107. There is no gentle degradation: the scheme is fine until it is catastrophically not.

For a fine mesh, an explicit solver is forced into tiny steps by stability rather than by accuracy -- the problem is stiff. We can switch to an implicit method, which has no such restriction. We can use solve_ivp(..., method='BDF') and passing the Jacobian D*A.

Second order of convergence

Take an initial condition whose exact solution we know. With Dirchilet boundary condition and u(x,0)=sin⁡(πx/L)u(x, 0) = \sin(\pi x/L), the Fourier sine series has the single term n=1n = 1, so

u(x,t)=e−D(π/L)2t sin⁡(πx/L).u(x, t) = e^{-D(\pi/L)^2 t}\,\sin(\pi x/L).

We then solve numerically on a sequence of meshes, measure the error, and check that it falls like Δx2\Delta x^2.

Animating the solution

A time-dependent solution is easier to read when animated.