A13 · Lesson 51 · Numerical methods lab
Three robust routines, each checked against an exact answer
Library routines like brentq, quad and solve_ivp are built from the ideas in this course: a fast method guarded by a safe one, an error estimate from two answers, and a step that adapts. In this lab you'll write a small version of each: a Newton solver that falls back to bisection, an adaptive Simpson integrator, and an RK4 integrator that adapts its step by step doubling. Each test compares against an exact answer, and each counts the work, so you can see the adaptivity pay off.
Predict
Your adaptive RK4 integrator solves from y(0) = 0. Where will it take its smallest steps?
At the start. The solution has a fast part that dies within about 0.1, then follows the slow forcing. The error estimate is large while the fast part is alive, so the steps start small and grow once it's gone, by a factor of about 10 here.
Name it
What you'll write
safe_newton(f, df, a, b, tol): keep a bracket [a, b] with a sign change. Try a Newton step from the current point; if it lands outside the bracket, bisect instead. After each step shrink the bracket using the sign of f. Stop when the step is below tol. Return(root, steps).adaptive_simpson(f, a, b, tol): the recursive method of lesson 19. Accept a piece when and return ; otherwise split it, giving each half tol/2. Count function evaluations (reuse the ones you already have). Return(value, evaluations).rk4_adaptive(f, t0, y0, t1, tol): from each point take one RK4 step of size h and two of h/2; the error estimate is . Accept the two-step answer if the estimate is at most tol; either way set the next h to , between h/5 and 5h. Don't step past t1. Return the lists of times and values.
The exact answers
- has its root at 2.0945514815423265.
- and .
- , y(0) = 0, has .
Worked example
Why must the safeguarded solver check the Newton step against the bracket?
Show the solution
- Plain Newton on . From x = 2 it jumps to then to 13.95, and diverges.
- With a bracket. With [−10, 10] the first jump lands inside, but each later wild step falls outside the shrinking bracket and is replaced by bisection.
- Back to Newton. That goes on until the iterate is close enough for Newton to take over.
Answer an unchecked Newton step can leave the region of the root and diverge, as on from 2; checking against the bracket replaces each such step with bisection.
Worked example
How many function evaluations does one step-doubling RK4 step cost?
Show the solution
- Count the evaluations. The full step uses 4 evaluations and the two half steps 8.
- Share one. The full step and the first half step both start with , so 11 in all.
- Compare. That's almost three times one RK4 step, the price of the error estimate. Embedded pairs (RK45) get theirs for 6 or 7.
Answer 11 evaluations.
Check
Step 1. Write safe_newton(f, df, a, b, tol) returning (root, steps).
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 adaptive_simpson(f, a, b, tol) returning (value, evaluations).
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 rk4_adaptive(f, t0, y0, t1, tol) returning lists ts and ys of accepted points (starting with t0, y0).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Step doubling with RK4 gives 0.3679 from one step and 0.3685 from two half steps. What's the estimated error of the two-step answer?
Check
For , y(0) = 0, what's the amplitude of the decaying term in the exact solution (its coefficient's size)?
Explain why
In the adaptive RK4 integrator, why can the step grow so much after the initial transient, when the forcing is still changing?
The step is chosen to keep each step's local error near the tolerance, and that error depends on how fast the solution's higher derivatives are. During the transient the solution contains , whose fifth derivative is times as large, so the steps must be tiny. Once that term has decayed, the solution follows , whose derivatives are all of size about 1, so a much larger step gives the same error. The forcing still changes, but slowly, and the step only has to resolve that slow change. The equation is mildly stiff, though: with k = 50 an explicit method's stability also limits the step to a few hundredths, and the controller finds that limit too, by rejecting steps where the error estimate grows.
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.