A problem with a known answer
The heat equation of lesson 6.4.1 is . Here is the temperature at position and time . is its rate of change in time, and its second partial derivative in .
Take a rod from to whose ends are held at temperature 0, and start it with the profile . These are one initial condition and two boundary conditions, as in lesson 6.3.2. The solution is known:
It is lesson 6.4.1's decaying wave with . Check it by computing partials, as lesson 6.3.1 does: differentiating in gives , and differentiating twice in gives the same. The profile keeps its shape and shrinks: at the middle value is .
Most problems have no formula like this. A finite-difference scheme finds the solution approximately, from numbers alone, and this exact answer lets you measure how well it does.
The grid
Split the rod into equal cells of width , and time into steps of length . The grid points are for , and the times are for
Rule: A grid for u(x, t)
is the scheme's value at point and time step . The upper index counts time steps; it is not a power. The bottom row is the initial condition, and the two end columns and are the boundary values.
The grid for on a rod: the initial condition fills the bottom row and the boundary conditions fill the end columns. One step of the explicit scheme takes three neighbouring values at one time and gives the middle value one step later.
The explicit update
Replace each derivative in by a difference of grid values, both at time .
- Time: the forward difference of lesson 2.2.2, . Lesson 2.8.1 gave its error: about .
- Space: the second difference of lesson 2.4.2, , with error about (lesson 2.8.1).
Set the two equal and solve for the one value at the new time:
Rule: The explicit scheme
Each new value is the old one plus times its second difference. So it rises where the profile bends up and falls where it bends down, as heat does (lesson 6.4.1). The scheme is explicit: each new value comes straight from old ones.
This is the step lesson 6.4.1's cell took, with and , so . This lesson asks how large may be, and how the error depends on the grid.
is a pure number here, because measures time in units of distance squared. For an equation with a constant , the same steps give .
One step by hand
Take cells, so , and , so . Start from on the grid and apply the update at the three inside points:
| 0 | 0.25 | 0.5 | 0.75 | 1 | |
|---|---|---|---|---|---|
| start, | 0 | 0.7071 | 1 | 0.7071 | 0 |
| second difference | |||||
| one step, | 0 | 0.6036 | 0.8536 | 0.6036 | 0 |
| exact at | 0 | 0.6061 | 0.8571 | 0.6061 | 0 |
At the middle, . The ends stay at 0: they are boundary values, not updated.
Every inside value shrank by the same factor, 0.8536, so the scheme keeps the sine's shape. The exact solution shrinks by in the same time, so the scheme cools the rod slightly too fast.
That common factor is no accident. The second difference of a sine on the grid is a fixed multiple of the sine, so each step multiplies it by . Here that is .
Try it
The grid is np.linspace(0, 1, M + 1), which includes both ends. One line does the update for every inside point at once: u[2:] are the right neighbours, u[:-2] the left ones.
The cell prints the start, the second differences, the step, the exact values and the factor .
Boundary values
The update needs a neighbour on each side, so it runs only at the inside points . At every step the two ends are set by the boundary conditions instead. In code, updating u[1:-1] leaves u[0] and u[-1] alone.
Hold the left end at 0 and the right end at 1, and start with 0 everywhere inside. Take and , so .
- After one step only the point next to the hot end has changed: .
- After two steps, and . Heat enters one cell per step.
- After 25 steps () the middle value is 0.2674; after 125 steps () it is 0.4957.
The profile settles on the straight line between the two end values. There , so nothing changes any more: a steady state. At the scheme is within of the line everywhere.
Try it
explicit runs the update for a number of steps. The ends of u are never touched, so they keep the boundary values they start with.
The loop prints the steps, the profile and its largest distance from the line , at four times.
Stability: r at most one half
A larger means a longer time step and fewer steps to reach a given time. But cannot be as large as you like. Gather the terms of the update by grid value:
The three weights add up to 1. When , none is negative, so each new value is an average of old ones and lies between them: nothing can grow. When , the middle weight is negative.
The zig-zag
A zig-zag is a profile whose grid values alternate: , and so on. At a point with value , the second difference is ; at a point with value it is . So it is times the value everywhere, and one step multiplies the whole zig-zag by .
| 0.25 | 0.4 | 0.5 | 0.55 | 0.6 | |
|---|---|---|---|---|---|
| factor | 0 |
For the factor lies between and 1, so the zig-zag dies away. At it flips sign each step but keeps its size. Above it flips sign and grows, by 1.4 times per step at .
A corner in the profile contains a little of every wave the grid can hold, the zig-zag included, and so does rounding error. So above there is always a zig-zag to grow.
Rule: Stability of the explicit scheme
A scheme is stable if errors do not grow from step to step. The zig-zag is the fastest wave a grid can hold and the first to grow, so this limit on is the whole condition. It is stated here, not proved.
Start from a tent with a corner at the middle, , on cells. Here is the largest value after each number of steps:
| steps | 10 | 20 | 30 | 40 | 50 |
|---|---|---|---|---|---|
| 0.3875 | 0.3407 | 0.3048 | 0.2745 | 0.2481 | |
| 0.377 | 0.3238 | 0.2833 | 0.2494 | 0.2201 | |
| 0.3879 | 0.4022 | 0.7223 | 2.641 | 13.31 | |
| 0.4605 | 2.325 | 47.93 | 1189 | 30,370 |
The tent starts at 0.5 and should only cool. For it does; above, the values explode. After 20 steps at , the seven values around the corner are , 2.0903, , 2.3253, , 2.0903 and : a zig-zag.
Try it
The first lines check the zig-zag: its second difference, and one step at , which turns into away from the ends.
The loop builds the tent table. The last line prints the values around the corner after 20 steps at .
The explorer runs the scheme on a kink, , with its ends held, beside the exact solution. Set the time to 0.5 and pick the presets in turn. At 0.25 and 0.5 the dots sit on the curve; at 0.55 a zig-zag appears at the corner.
At 0.6 the zig-zag swamps everything. Finally, at , move the cells from 30 to 60 and watch it blow up sooner.
Explore
The scheme updates each grid value from its two neighbours, with r = Δt/Δx². Keep r at or below 1/2 and it follows the exact curve; push r past 1/2 and the error grows from step to step until it swamps the answer.
Dashed: the profile at time 0. Dark: the exact solution. Dots: the scheme on its grid.
- Grid
- Δx = 0.2, Δt = r Δx² = 0.016, 6 steps to t = 0.096
- Largest error
- 0.002
The price of stability
The limit ties the time step to the square of the space step. Halve and the largest stable falls 4 times. Reaching the same time then takes 4 times as many steps, each with twice as many points.
| cells | 10 | 100 | 1,000 |
|---|---|---|---|
| largest | 0.005 | ||
| steps to | 20 | 2,000 | 200,000 |
Lesson 6.5.2 removes this limit with schemes that are stable for every .
The error as the grid gets finer
Back to the sine, whose exact solution is known. Keep , run to , and measure the largest error over the grid:
| cells | steps | largest error | error ratio | ||
|---|---|---|---|---|---|
| 10 | 0.1 | 0.004 | 25 | ||
| 20 | 0.05 | 0.001 | 100 | 4.04 | |
| 40 | 0.025 | 0.00025 | 400 | 4.01 | |
| 80 | 0.0125 | 0.0000625 | 1,600 | 4.00 |
Each halving of divides the error by 4, so the error shrinks like . The two parts of lesson 2.8.1's error explain it. The time error, about , equals once ; the space error is about .
Rule: Error of the explicit scheme
and depend on the solution, not on the grid. The scheme is first order in time and second order in space. With fixed, halving cuts the error about 4 times and costs about 8 times the work.
Stability and accuracy are different questions. Stability says the errors do not grow; the step sizes say how large they are to begin with.
Try it
For each grid the cell runs the sine to with . It prints the steps, the largest error and its ratio to the previous one.
round(0.1 / dt) makes the number of steps a whole number; here each one is exact.
Case study 6.5.4 uses these ideas on the Black–Scholes equation, with the improved schemes of lesson 6.5.2.
Common mistakes
- Halving but keeping . That multiplies by 4: a stable becomes , and the answer explodes. Cut by 4 too.
- Using instead of in . With and , is 0.4, not 0.04.
- Updating in place in a loop. Computing
u[i]from au[i - 1]already changed this step gives a different scheme. Keep the old values until every new one is made; the slice line does this. - Updating the ends. The end values come from the boundary conditions, not from the update.
- Reading as a power. The upper index counts time steps.
- Taking stable for accurate. At errors do not grow, but a coarse grid still has large ones.
The explicit scheme
0 of 12 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
How to type answers
- Powers with
^:x^7,2^(1/3),a^-2. - Multiply with
*or by writing side by side:3x^2,2(x+1). ln(x)is the natural log,log(x, 2)is log base 2,eis Euler's number,sqrt(x)is a square root.sin(x),cos(x)andtan(x)take radians; writepifor π andsin(x)^2for sin²x.- Your first answer to each question is saved: it counts towards your score and brings the question back for review.
- Exact answers like
ln(20)/ln(3)are best. Decimals need about 4 significant figures.
Summary card
- A grid has points and times ; approximates . The bottom row is the initial condition, the end columns the boundary values.
- The explicit scheme for : with , at the inside points only.
- It is stable for : then each new value is an average of old ones. Above, a zig-zag grows by per step until it swamps the answer.
- Its error is about : with fixed, halving cuts it 4 times, but the stability limit makes fine grids expensive.