8.7 ODE solver sketches
Source: Structural ideas from Computational Physics (Landau, Páez, Bordeianu), §8.7 listings. Original sketches only — do not paste
rk4.py,rk45.py,ABM.py, or VPython GUIs from the book / Mathpix CDN.
Fixed-step RK2 / RK4 (sandbox-ready)
Graded work in 8.4b already implements these. Structure reminder:
# rk_fixed_sketch.py — original structure (pure Python)
def f(t, y, params):
x, v = y
# return [v, F(x,v,t)/m]
...
def rk2_step(t, y, h, params):
k1 = scale(h, f(t, y, params))
y_mid = add(y, scale(0.5, k1))
k2 = scale(h, f(t + 0.5 * h, y_mid, params))
return add(y, k2)
def rk4_step(t, y, h, params):
k1 = f(t, y, params)
k2 = f(t + 0.5 * h, add(y, scale(0.5 * h, k1)), params)
k3 = f(t + 0.5 * h, add(y, scale(0.5 * h, k2)), params)
k4 = f(t + h, add(y, scale(h, k3)), params)
incr = scale(h / 6.0, add(k1, add(scale(2, k2), add(scale(2, k3), k4))))
return add(y, incr)
Swap F for harmonic, , soft , or without changing the stepper.
Adaptive RK45 (sketch only)
Two formulas of different order share stages. Their difference estimates the local error; grow or shrink to hit a tolerance. Pseudocode:
# rk45_adapt_sketch.py — structure only (original)
def rk45_step(t, y, h, tol, params):
# evaluate shared stages → y4 (order 4) and y5 (order 5)
# err ≈ ||y5 - y4||
# if err < tol: accept y5 (or y4), maybe increase h
# else: reject, shrink h, retry
...
Not required for locked labs; useful when stiffness or sharp drives make fixed wasteful.
When to prefer fixed RK4 vs adaptive RK45
| Prefer fixed RK4 | Prefer adaptive RK45 |
|---|---|
| Smooth , known period, you want reproducible | Sharp kicks, stiffness, or unknown timescales |
| Energy / period scans at fixed resolution | Hitting a tolerance with fewer calls |
| Graded sandbox locks (stdout samples) | Local notebooks once RK4 energy checks pass |
Fixed RK4 with a chosen is easier to debug: if energy drifts, shrink or fix . Adaptive methods hide the step size — great for production, worse for learning the truncation story.
Energy check (use the explorer)
For conservative forces, monitor
along the trajectory. A good stepper keeps flat to many digits; Euler drifts; RK4 usually holds until is too coarse. Use the harmonic explorer below to compare methods before trusting a nonlinear run.
Adams–Bashforth–Moulton (sketch only)
Multistep methods store a short history of values. Adams–Bashforth predicts ; Adams–Moulton corrects with an implicit formula (often one evaluation of at the predicted point). Startup needs a single-step RK fill of the history.
# abm_sketch.py — structure only (original)
# history: f_n, f_{n-1}, f_{n-2}, ...
# y_pred = AB_predict(y_n, history, h)
# f_pred = f(t_{n+1}, y_pred)
# y_corr = AM_correct(y_n, history, f_pred, h)
Visualization note
Older book listings couple solvers to VPython animations. Keep the ODE algebra; plot with whatever you use locally. This course’s sandbox is pure Python stdout — energy digits and samples, not 3D springs on a CDN.
Takeaway
| Method | Role in this course |
|---|---|
| RK2 / RK4 fixed | Required mental model + labs |
| Energy flatness | Primary correctness check |
| RK45 adaptive | Optional once fixed- diagnostics click |
| ABM multistep | Optional; needs RK startup history |
ODE solver sketches
1 / 3RK4 advances y with…
Pick an answer to see if you are right.
Next: 9.1 Fourier series.