Fourth-order accuracy from values of alone (Runge–Kutta), reusing past slopes (Milne and Adams–Bashforth–Moulton), systems of equations, and the stability limits that decide which method survives a stiff problem.
A mass on a spring: position , velocity . Without friction it should oscillate for ever with the same energy .
| method () | energy after 10 periods |
|---|---|
| exact | 1 |
| Euler | 517.4 |
| Heun | 1.0158 |
| RK4 | ? |
Euler (order 1) and Heun (order 2); instability when is too large.
Higher order with more slopes per step, or with slopes from earlier steps.
input f, x0, y0, h, x_end
x ← x0; y ← y0
while x < x_end − h/2:
k1 ← h·f(x, y)
k2 ← h·f(x + h/2, y + k1/2)
k3 ← h·f(x + h/2, y + k2/2)
k4 ← h·f(x + h, y + k3)
y ← y + (k1 + 2k2 + 2k3 + k4)/6
x ← x + h
return yWorks unchanged for systems if and the 's are vectors.
y[0..3] ← RK4 starting values
F[k] ← f(x[k], y[k]), k = 0 … 3
for n = 3, 4, …:
yp ← y[n] + h/24·(55F[n] − 59F[n−1]
+ 37F[n−2] − 9F[n−3])
fp ← f(x[n+1], yp)
y[n+1] ← y[n] + h/24·(9fp + 19F[n]
− 5F[n−1] + F[n−2])
F[n+1] ← f(x[n+1], y[n+1])
err ≈ (19/270)·|y[n+1] − yp|Two new values per step; the error estimate costs nothing.
| quantity | Milne | Adams–Bashforth–Moulton |
|---|---|---|
| slopes | 1, 1.210342, 1.442805, 1.699717 | same |
| predictor | ||
| 1.983642 | 1.983640 | |
| corrector | ||
| exact | 1.583649 | 1.583649 |
Both correctors agree with the exact value to 6 decimals, using only 2 new evaluations of (RK4 would use 4).
No formula gives . RK4 with a step of one day: the infected fraction peaks at 30.4% on day 27, and about 94% of people are eventually infected. This is how epidemic forecasts were computed in 2020.
rk4_step for Rule of thumb: start with an adaptive RK (RK45); if it takes absurdly small steps, the problem is stiff: switch to an implicit solver.
| Method | Order | per step | Self-starting? | Stability ( real) | Error at (, ) |
|---|---|---|---|---|---|
| Euler | 1 | 1 | yes | 0.249 | |
| Heun / RK2 | 2 | 2 | yes | ||
| RK4 | 4 | 4 | yes | ||
| Milne P–C | 4 | 2 | no (needs RK4 start) | weakly unstable | |
| Adams–Bashforth–Moulton | 4 | 2 | no (needs RK4 start) | about | |
| Backward Euler (implicit) | 1 | 1 + equation solve | yes | all | 0.30 |
No method wins everywhere: RK4 for general use, Adams when is expensive, implicit methods when the problem is stiff.
We can now simulate models. The last module asks the reverse question: given data, which model and which parameters?
Click a card to see an RK or multistep solver in action.
Where exactly: public-health modelling. Susceptible () and infected () fractions obey , ; forecasts step this system with RK4 or an adaptive solver.
Peak about 30% infected around day 27. Lockdowns lower : try 0.2 in the first two formulas and watch the peak flatten and move later.
Where exactly: orbit prediction. Newton's law of gravitation gives a system of six first-order ODEs (position and velocity in 3-D); mission design integrates it with high-order Runge–Kutta or multistep (Adams) methods over months.
JPL's planetary ephemerides are produced with variable-order Adams methods.
Where exactly: every scientific Python or MATLAB program that solves ODEs. The default method (RK45, Dormand–Prince) computes a 4th- and a 5th-order RK result from the same six slopes; their difference estimates the error and sets the next step size.
Today's two ideas, RK and stability, are exactly the two choices a user makes when calling solve_ivp.
Where exactly: ecology and economics. Prey grow and are eaten; predators grow by eating and die otherwise: , . The populations cycle; a quantity stays constant on each cycle.
Switch to Euler: the cycles spiral outwards and drifts from 1.546 to 3.65, a numerical artefact that would wrongly predict extinction.
RK4 with keeps the spring's energy at 0.999991 after ten periods (Euler: 517, Heun: 1.016). The largest position error is .
Module 7: least squares and linear regression. From solving models to fitting them to data, the core of machine learning.