Task 5 of 5
Time to put the two schemes on the same clock. Both runs below finish at
T = 16: the explicit one in small steps of 0.2, the implicit
one in steps of 2 — ten times larger, and four times past the explicit
limit. A third run takes the explicit scheme at the implicit step size, for the pleasure
of watching it fail in eight steps.
The two survivors will not agree. Backward Euler is first-order accurate,
just like forward Euler, so a ten-times-larger step carries a ten-times-larger error —
it damps sharp features harder than the real equation does. Expect the two fields to
differ by a few percent of the peak. That is the honest trade, and it is worth stating
plainly: unconditional stability is not accuracy. What implicit
stepping buys you is the right to choose dt for the accuracy you need,
rather than having it dictated by the smallest cell in your mesh.
Nor is it free. Each implicit step here costs 25 sweeps, so the coarse run makes
8 × 25 = 200 kernel launches against the fine run's 80 — implicit
loses the launch count at this size. It wins when the explicit limit gets
brutal: refine dx by 10× and the explicit run needs 100× the steps, while
the implicit one needs the same eight and a slightly harder solve.
α arriving as an
argument, work out how many steps of each size reach T, and run all three.alpha is a kernel argument, not a constantsmallSteps and bigSteps are the counts that reach T = 16 at dt = 0.2 and dt = 2Only the spelling changes: a plain alpha where the constant used to
be, and the caller passes it. Everything else is last task's body:
return (uOld[y][x] + alpha * neighbours)
/ (1 + 4 * alpha);Steps × step size = elapsed time, so it is T / dt:
16 / 0.2 = 80 and 16 / 2 = 8.
This page is an interactive exercise — the editor, the GPU runner and your saved progress need JavaScript. The text above is the full brief.