A1251 of 55
0 XP
  1. Hook
  2. Predict
  3. Name it
  4. Check
  5. Explain

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 r=12 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 V0/4 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 10−6 at the grid's finest scale. Roughly how big is the wiggle at the end?

Name it

What you'll write

  1. ftcs(u, r, steps): the explicit scheme, ui←ui+r(ui+1−2ui+ui−1) on the interior points, with the two end values held fixed. Use NumPy slices, not a loop over i.
  2. crank_nicolson(u, r, steps): each step solves (I−r2D)un+1=(I+r2D)un on the interior points, where D is the second-difference matrix (−2 on the diagonal, 1 beside it). Build the two matrices once and call np.linalg.solve each step.
  3. jacobi_box(N, iters): an (N+1)×(N+1) 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 sin⁡(πx) on N intervals is an exact mode of both schemes, so after n steps it must be the start times Gn, with

GFTCS=1−4rs2,GCN=1−2rs21+2rs2,s=sin⁡π2N

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
  1. The amplification factor. Here s=sin⁡(π/40)=0.07846, so G=1−4×0.4×0.006156=0.990151
  2. After 100 steps. G100=0.3716
  3. Compare with the PDE. Its own decay over the same time, t=100r/N2=0.1, is e−π2×0.1=0.3727 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 N2 sweeps to settle?

Show the solution
  1. One sweep. Each sweep multiplies the slowest error mode by cos⁡πN≈1−π22N2
  2. Many sweeps. Shrinking that error by a factor e−k takes about 2kN2π2 sweeps.
  3. An example. For N = 20 and a factor of 10−10 (k = 23), that's about 1900.
  4. The fix. That slowness is what SOR and multigrid fix.

Answer each sweep shrinks the slowest error mode only by about 1−π22N2, so settling takes about 2kN2π2 sweeps: about 1900 for N = 20 and a factor of 10−10.

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 (N+1)×(N+1) 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 e−10?

Check

What's Crank–Nicolson's amplification factor for the finest grid mode (kΔx=π) 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?

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.