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

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 y˙=−50(y−cos⁡t) from y(0) = 0. Where will it take its smallest steps?

Name it

What you'll write

  1. 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).
  2. adaptive_simpson(f, a, b, tol): the recursive method of lesson 19. Accept a piece when |S2−S1|≤15tol and return S2+(S2−S1)/15; otherwise split it, giving each half tol/2. Count function evaluations (reuse the ones you already have). Return (value, evaluations).
  3. 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 |yh/2−yh|/15. Accept the two-step answer if the estimate is at most tol; either way set the next h to 0.9h(tol/err)1/5, between h/5 and 5h. Don't step past t1. Return the lists of times and values.

The exact answers

  • x3−2x−5=0 has its root at 2.0945514815423265.
  • ∫01xdx=23 and ∫0πsin⁡xdx=2.
  • y˙=−k(y−cos⁡t), y(0) = 0, has y=k2cos⁡t+ksin⁡t1+k2−k21+k2e−kt.

Worked example

Why must the safeguarded solver check the Newton step against the bracket?

Show the solution
  1. Plain Newton on arctan⁡x. From x = 2 it jumps to 2−arctan⁡2×(1+4)=−3.54 then to 13.95, and diverges.
  2. 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.
  3. 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 arctan⁡x 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
  1. Count the evaluations. The full step uses 4 evaluations and the two half steps 8.
  2. Share one. The full step and the first half step both start with f(t,y), so 11 in all.
  3. 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˙=−20(y−cos⁡t), y(0) = 0, what's the amplitude of the decaying term e−kt 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?

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.