A12 · Lesson 51 · PDE lab: the heat and Laplace equations, numerically
Write the schemes, watch one blow up, and check them against exact answers
This lab turns lesson 48 into code. You'll write the explicit heat scheme and check it against its own amplification factor to rounding level, then push it past and watch a millionth of a degree of noise take over. You'll fix that with Crank–Nicolson, which stays stable at any step. Finally you'll relax Laplace's equation in a square box and recover the at the center from lesson 33.
Predict
The explicit scheme runs 60 steps at r = 0.6, starting from a smooth sine plus a wiggle of size at the grid's finest scale. Roughly how big is the wiggle at the end?
About 300. The finest mode's factor is per step at r = 0.6, so after 60 steps it has grown by about . The smooth sine, meanwhile, has decayed a little; the answer is all wiggle.
Name it
What you'll write
ftcs(u, r, steps): the explicit scheme, on the interior points, with the two end values held fixed. Use NumPy slices, not a loop over i.crank_nicolson(u, r, steps): each step solves on the interior points, where D is the second-difference matrix (−2 on the diagonal, 1 beside it). Build the two matrices once and callnp.linalg.solveeach step.jacobi_box(N, iters): an grid with the top row at 1 and the other edges at 0; every interior point repeatedly becomes the average of its four neighbors.
How you'll know they're right
A grid sine on N intervals is an exact mode of both schemes, so after n steps it must be the start times , with
These match to rounding error, which is a far sharper test than comparing with the PDE's exact solution.
Worked example
With N = 20 and r = 0.4, what should 100 explicit steps do to the sine mode?
Show the solution
- The amplification factor. Here , so
- After 100 steps.
- Compare with the PDE. Its own decay over the same time, , is so the grid adds a 0.3% error.
Answer 100 steps should shrink the sine mode to 0.3716 of its start, against the PDE's 0.3727: a 0.3% error.
Worked example
Why does Jacobi need about sweeps to settle?
Show the solution
- One sweep. Each sweep multiplies the slowest error mode by
- Many sweeps. Shrinking that error by a factor takes about sweeps.
- An example. For N = 20 and a factor of (k = 23), that's about 1900.
- The fix. That slowness is what SOR and multigrid fix.
Answer each sweep shrinks the slowest error mode only by about , so settling takes about sweeps: about 1900 for N = 20 and a factor of .
Check
Step 1. Write ftcs(u, r, steps) returning the grid after steps explicit steps, with u[0] and u[-1] held fixed. Don't change the array you're given.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Step 2. Write crank_nicolson(u, r, steps) with the ends held fixed (at zero here), solving the tridiagonal system on the interior points each step.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Step 3. Write jacobi_box(N, iters): an grid (row 0 is the top) with the top row at 1 and the other edges at 0, relaxed iters times by four-neighbor averaging of the interior. Return the grid.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
About how many Jacobi sweeps does an N = 40 box need to shrink its slowest error by a factor ?
Check
What's Crank–Nicolson's amplification factor for the finest grid mode () at r = 5?
Explain why
Why is it a stronger test to compare a scheme with its own amplification factor than with the exact solution of the PDE?
The scheme's result differs from the PDE's exact solution by the discretization error, which is small but not zero (0.3% in this lab), so a comparison with the PDE needs a loose tolerance that could hide a real bug of similar size. A sine on the grid is an exact eigenvector of the discrete scheme, so a correct implementation must multiply it by exactly , to rounding error. Comparing at 1e-12 catches any mistake in the update, an off-by-one in the slices, a wrong sign or a boundary handled wrongly, which the looser comparison would let through.
Practice
Keep practicing
5 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.