A13 · Lesson 45 · Lab: ODE and boundary-value solvers in code
Measuring order, stepping a stiff problem, shooting, and finite differences with Newton
Lessons 40 to 44 covered one-step and multistep methods, step control, stiffness and boundary-value problems. This lab puts them into code and measures them. You'll confirm AB2's order by halving the step, march BDF2 through a stiff problem at a step a thousand times longer than its fastest time constant, solve a linear boundary-value problem by shooting with RK4, solve another by finite differences with the Thomas algorithm, and solve a nonlinear one, the Bratu problem, by Newton's method on a tridiagonal system.
- A method of order p has global error about , so on log axes error against step is a straight line of slope p.
- The lines show order 1 (Euler), 2 (AB2, Heun, BDF2) and 4 (RK4) with the same constant. Halving h cuts their errors by 2, 4 and 16.
- Measuring this slope from two runs is the quickest test of whether a solver is coded correctly.
Predict
BDF2 steps through with h = 0.01, ten times the fast time constant. What happens?
It tracks the slow solution, , to about . BDF2 damps the fast mode at any step size, so the step only has to resolve the slow variation of . Forward Euler would need h < 0.002 just to stay stable.
Name it
Measuring order
Run with h and h/2 and compare the errors at the same time: . Start multistep methods with exact or high-order values, or the starting error will mask the method's order.
BDF2 on a linear stiff problem
For , BDF2's implicit step solves in closed form: . Take the first step with backward Euler.
Shooting with an IVP solver
Write the second-order ODE as a first-order system , integrate with RK4 from a guessed slope, and adjust. For a linear problem with , the end value is proportional to the slope, so one shot and a rescale suffice.
Finite differences with Newton
For , the residual at each interior point is , and its Jacobian is tridiagonal with on the diagonal and beside it. Newton's method, , usually converges in a handful of iterations from y = 0.
Worked example
Measure AB2's order on to t = 1, starting each run with the exact .
Show the solution
- The errors. With h = 0.1 the error at t = 1 is , and with h = 0.05 it's .
- The order. The ratio is 3.90, so close to 2.
- Smaller steps. The ratio approaches 4 as h shrinks; at large h the higher-order terms still show.
- The starting value. If it had come from one Euler step, its error would still leave AB2 second-order overall, but a cruder start would cap the observed order.
Answer the measured order is about 1.96, close to 2.
Worked example
Solve the Bratu problem , , by finite differences and Newton's method.
Show the solution
- Set up. With 99 interior points (), start from y = 0. The residual is and the Jacobian's diagonal is .
- Newton. It converges in about four iterations, giving .
- The exact solution. where θ = 1.51716 solves . It gives .
- The difference. It is , the discretization error, and it falls by four when h halves.
Answer , against the exact 0.1405392.
Check
A solver's error is 0.0015 at step h and 0.0015/3.9 at h/2. What order does that suggest?
Check
BDF2 steps a mode decaying at rate 1000 s⁻¹ with h = 0.01 s. By what factor does each step shrink that mode?
Check
Write ab2_error(h) that runs AB2 on to t = 1, starting from and the exact , and returns the absolute error at t = 1. Set e1 = ab2_error(0.1), e2 = ab2_error(0.05) and order = log2(e1/e2).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Step from y(0) = 1 to t = 1 with h = 0.01: one backward Euler step, then BDF2. Set y_end, and ref to the slow solution .
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write rk4(f, y, t0, t1, n) for systems (NumPy arrays). Solve , , by shooting: integrate from (0, 1) with 100 steps, and set s to the slope that hits the target.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write thomas(a, b, c, d) solving a tridiagonal system (a below, b on, c above the diagonal). Use it to solve , , with N interior points, and set e9 and e19 to the worst errors for N = 9 and 19 against .
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Solve the Bratu problem , with 99 interior points by Newton's method from y = 0 (20 iterations, np.linalg.solve on the tridiagonal Jacobian is fine). Set mid to the middle value.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why does measuring the error ratio under step halving test a solver so well?
A correctly coded method of order p has a global error that behaves like for small h, so halving h divides the error by , whatever the constant C is. A coding slip, such as a wrong coefficient, a time argument off by one step, or a starting value from a cruder method, almost always lowers the observed order, even when the answers still look plausible. Measuring the ratio from two or three runs therefore checks the code against a property the correct method must have, without needing the exact answer to many digits, only one with an error well below the method's.
Practice
Keep practicing
4 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.