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

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 Chp, 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 y˙=−1000(y−cos⁡t) with h = 0.01, ten times the fast time constant. What happens?

Name it

Measuring order

Run with h and h/2 and compare the errors at the same time: p≈log2⁡e(h)e(h/2). 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 y˙=λ(y−g(t)), BDF2's implicit step solves in closed form: yn+1=4yn−yn−1−2hλgn+13−2hλ. Take the first step with backward Euler.

Shooting with an IVP solver

Write the second-order ODE as a first-order system (y,y′), integrate with RK4 from a guessed slope, and adjust. For a linear problem with y(0)=0, the end value is proportional to the slope, so one shot and a rescale suffice.

Finite differences with Newton

For y′′=f(y), the residual at each interior point is Fi=yi+1−2yi+yi−1h2−f(yi), and its Jacobian is tridiagonal with −2h2−f′(yi) on the diagonal and 1h2 beside it. Newton's method, y←y−J−1F, usually converges in a handful of iterations from y = 0.

Worked example

Measure AB2's order on y˙=−y to t = 1, starting each run with the exact y1=e−h.

Show the solution
  1. The errors. With h = 0.1 the error at t = 1 is 1.46×10−3, and with h = 0.05 it's 3.75×10−4.
  2. The order. The ratio is 3.90, so p≈log2⁡3.90=1.96 close to 2.
  3. Smaller steps. The ratio approaches 4 as h shrinks; at large h the higher-order terms still show.
  4. The starting value. If it had come from one Euler step, its O(h2) 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 y′′+ey=0, y(0)=y(1)=0, by finite differences and Newton's method.

Show the solution
  1. Set up. With 99 interior points (h=0.01), start from y = 0. The residual is Fi=yi+1−2yi+yi−1h2+eyi and the Jacobian's diagonal is −2h2+eyi.
  2. Newton. It converges in about four iterations, giving y(0.5)=0.1405406.
  3. The exact solution. y=−2ln⁡cosh⁡((x−1/2)θ/2)cosh⁡(θ/4) where θ = 1.51716 solves θ=2cosh⁡θ4. It gives y(0.5)=0.1405392.
  4. The difference. It is 1.4×10−6, the O(h2) discretization error, and it falls by four when h halves.

Answer y(0.5)=0.1405406, 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 y˙=−y to t = 1, starting from y0=1 and the exact y1=e−h, 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 y˙=−1000(y−cos⁡t) 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 106cos⁡1+1000sin⁡1106+1.

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 y′′=y, y(0)=0, y(1)=1 by shooting: integrate (y,y′) 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 y′′=4y, y(0)=1, y(1)=0 with N interior points, and set e9 and e19 to the worst errors for N = 9 and 19 against sinh⁡2(1−x)sinh⁡2.

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 y′′+ey=0, y(0)=y(1)=0 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?

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.