Runge-Kutta Methods
Learning Objectives
- Derive the family of second-order explicit Runge-Kutta methods by matching a Taylor expansion, using no derivatives of .
- Read a Butcher tableau, write classical RK4, and check its order conditions.
- Connect the midpoint, trapezoid-type, and RK4 methods to quadrature rules on .
- Measure orders per evaluation, and show them dropping when the right-hand side lacks smoothness.
- Compute a stability function, and explain why higher order does not cure stiffness.
- Explain how an embedded pair controls the step size, and compare a working solver with
solve_ivpby counting evaluations.
Context & Motivation
The lesson eulers-method-and-local-vs-global-error ended with ten million steps for an error near . Raising the order is the obvious fix, and the direct route is a Taylor method: . But demands partial derivatives of , and the higher terms get worse (the expansion is the one in taylor-and-maclaurin-series-with-error-bounds). Often is a black box.
Runge-Kutta methods get the same order from extra evaluations of alone, by sampling it at well-chosen points inside the step and combining the slopes. The code that runs in scipy.integrate.solve_ivp by default is one of these.
Core Theory
Matching Taylor with two slopes
Try two stages, and , with the update . For , expanding gives , and the update becomes
The exact solution has . They agree through when
No partial derivative of is computed: the difference supplies it implicitly. There is one free parameter. With this is the explicit midpoint method, . With it is the explicit trapezoid method, . Names vary by book: "Heun" and "improved Euler" are attached to different members of this family, so check the formula.
Classical RK4 and Butcher tableaus
An -stage explicit method is fixed by a table: and . Classical RK4 has
plaintextdef rk4_step(f, t, y, h): k1 = f(t, y) k2 = f(t + h / 2, y + h / 2 * k1) k3 = f(t + h / 2, y + h / 2 * k2) k4 = f(t + h, y + h * k3) return y + h / 6 * (k1 + 2 * k2 + 2 * k3 + k4)
Order 4 needs eight algebraic conditions on the table. In exact fractions, RK4 gives , , , , , , , and , all as required. Up to order 4, stages can reach order . That stops there: order 5 has 17 conditions and needs six stages. This is why order 4 is such a common stopping point.
On a pure quadrature problem
If depends only on , then for RK4, and a step becomes , a Simpson panel of trapezoid-and-simpson-rules. In the same way explicit midpoint becomes the midpoint rule, and explicit trapezoid becomes the trapezoid rule. On with 8 steps, RK4 and composite Simpson agree to . So what limits Simpson also limits RK4.
Order needs a smooth right-hand side
The standard order theorem assumes enough derivatives of (for RK4, is the usual statement), and the Lipschitz bound of the previous lesson then carries local order to global order. Fitted orders over , , leaving out errors below :
textproblem Euler exp. trapezoid RK4 y' = y cos t, t=1.5 (smooth) 1.00 2.00 4.00 y' = 1 + |y-1|, y(0)=0, t=1.5 (kink) 0.99 ~2.0 ~2.0 y' = y, then -y after 1/sqrt(2), t=1.5 1.00 1.03 0.75 y' = sqrt(1-y^2), y(0)=0, t=1 1.00 2.00 4.01 y' = sqrt(1-y^2), y(0)=0, t=pi/2 2.00 2.00 2.00
On the kink, RK4 is no better than a two-stage method. Those fitted slopes are 2.00 and 2.01, but read them only as about 2: the order between consecutive scatters from 0.4 to 4.9 for RK4, and more for the two-stage method. At the jump, the error depends on where the jump falls in a step, so the errors scatter too and the 0.75 is rough, around order 1. The fix there is to stop at the jump and restart: with RK4 steps on each side of the jump, doubling from 160 to 320 measures 4.01 again.
The last two rows are the same equation. Its solution is , infinitely smooth, yet at the solution reaches , where has an infinite -derivative (the code clips at 0). RK4 drops to order 2, with each doubling of dividing the error by 4. Smoothness of the solution is not enough; the method samples off the solution. The Euler entry of 2.00 is not a gain: the first-order term of its error happens to vanish at that one time.
Stability functions
On with , every explicit method with gives , where is the Taylor polynomial of of degree .
textmethod R(z) real interval imaginary interval Euler 1 + z [-2, 0] {0} order 2 1 + z + z^2/2 [-2, 0] {0} RK4 1 + z + ... + z^4/24 [-2.785, 0] [-2.828, 2.828]
RK4 reaches a piece of the imaginary axis, so unlike Euler it can run an undamped oscillation at a finite step. But an explicit method has a polynomial , and a nonconstant polynomial is unbounded, so its stability region is bounded. Explicit methods cannot be stable for all on a stiff problem.
Adaptive step size: embedded pairs
Two Runge-Kutta formulas of different order can share stages. The Bogacki-Shampine pair, used by SciPy's RK23, has
and . The third-order result and a second-order result built with differ by . That difference estimates the local error of the second-order formula, with no extra evaluations. Since is the next step's (first same as last), each attempted step costs three evaluations, not four.
The controller scales by , takes a root-mean-square norm of , accepts if , and sets
The exponent is with , the lower order. SciPy keeps the higher-order result even though the error refers to the lower one, which is called local extrapolation. RK45 does the same with the Dormand-Prince 5(4) pair. The documentation is precise about what is bounded: "The solver keeps the local error estimates less than atol + rtol * abs(y)." That is a per-step estimate, not the error of the final answer.
Worked Examples
Example 1: one RK4 step by hand
Return to , , exact solution , with .
So , against the exact , an error of . Euler at the same step gave , an error of .
Example 2: measuring the orders, and the floor
For , , errors at for steps, with the measured order in parentheses:
textn Euler midpoint exp. trapezoid RK4 10 1.482e-01 2.018e-03 1.970e-02 1.726e-05 20 7.467e-02 (0.99) 6.303e-04 (1.68) 4.778e-03 (2.04) 1.057e-06 (4.03) 40 3.746e-02 (1.00) 1.756e-04 (1.84) 1.174e-03 (2.03) 6.510e-08 (4.02) 80 1.876e-02 (1.00) 4.630e-05 (1.92) 2.908e-04 (2.01) 4.034e-09 (4.01) 160 9.386e-03 (1.00) 1.189e-05 (1.96) 7.234e-05 (2.01) 2.510e-10 (4.01) 320 4.695e-03 (1.00) 3.011e-06 (1.98) 1.804e-05 (2.00) 1.565e-11 (4.00)
Midpoint climbs to 2 only slowly, a reminder that order is a limit. In float64, RK4 reaches at and then wanders between and up to : rounding has taken over. In float32 the same loop stops improving near , at about apart from lucky dips, and is at by .
Example 3: the fair comparison is per evaluation
RK4 pays four evaluations per step, so compare at equal budgets. On at :
textevaluations Euler midpoint RK4 8 7.04e-02 3.45e-02 1.06e-02 16 3.45e-02 6.89e-03 4.29e-04 80 6.79e-03 2.34e-04 4.90e-07 800 6.77e-04 2.26e-06 4.55e-11
With 8 evaluations ( for RK4) the advantage is small. With 800 it is seven orders over Euler. Higher order pays once is in the asymptotic range.
Example 4: an eccentric orbit against solve_ivp
An orbit with eccentricity moves fast near the central body and slowly far away. In units with , , start at closest approach, , . After the state must return exactly, so the error is the Euclidean norm of the difference. Evaluations are counted by a wrapper around .
textmethod f calls error after one orbit RK4 fixed, n=2000 8000 1.99e-02 RK4 fixed, n=10000 40000 2.50e-05 RK4 fixed, n=100000 400000 2.38e-09 RK45 rtol=1e-3, atol=1e-6 152 2.59e+00 RK45 rtol=1e-6, atol=1e-9 524 1.89e-02 RK45 rtol=1e-8, atol=1e-10 974 1.22e-04 RK45 rtol=1e-10, atol=1e-12 2204 7.01e-07 RK45 rtol=1e-12, atol=1e-14 5516 4.53e-09 RK23 rtol=1e-6, atol=1e-9 1757 4.53e-03 RK23 rtol=1e-8, atol=1e-10 7547 4.85e-05
An implementation of the Bogacki-Shampine pair with SciPy's controller (including the in the scale, its initial-step rule, and its rule of not growing right after a rejection) reproduces both RK23 rows exactly: 567 accepted steps and 1757 evaluations at the first tolerance, and final states within . The count checks by hand: attempts plus 2 for the start.
To match the last RK45 row, fixed RK4 needs about , or 340000 evaluations (error ), about 60 times as many. That gain combines higher order with adaptivity. At rtol=1e-10, 198 of the 365 steps fall in the of the time closest to the central body, with median there, about right at closest approach (the 41 steps with ), and a median near around the far side. On a problem without such contrast the gain is modest: for to , RK45 uses 1130 evaluations and fixed RK4 uses 1704 for an error near .
Two warnings are in the table. The default tolerances give a final error of 2.59 on a state of size about 4, with status 0 and no warning. And rtol is not the final error: rtol=1e-8 ends with , because local errors accumulate over the orbit.
Example 5: stiffness with counted work
Back to , , whose solution is . Fixed RK4 needs :
textn z=h*lambda f calls error at t=1 350 -2.857 1400 4.26e+11 355 -2.817 1420 8.13e+02 360 -2.778 1440 1.05e-04 1000 -1.000 4000 9.02e-09
Euler's limit was , or 500 evaluations, with error . The higher-order method takes 1440 evaluations to be stable at all, and its error there is larger, so higher order did not help. The adaptive solvers show the same thing. With rtol=1e-6, atol=1e-8 to :
textmethod f calls (counted) nfev field steps LU factorizations error RK23 17912 17912 3981 0 2.39e-07 RK45 21362 21362 3556 0 1.92e-07 Radau 280 278 33 34 1.64e-08 BDF 258 242 106 28 1.76e-08
RK23 settles at a mean step of , which is its stability limit: its has real interval . The error test is what finds that limit, by rejection: over it rejects 199 steps against 401 accepted. The explicit solvers are spending evaluations on stability and not on accuracy. Radau and BDF are implicit. Radau uses 1/76 of the evaluations of RK45 and is about ten times more accurate here, but both implicit methods pay for LU factorizations, which matter for large systems. The nfev field also leaves out the calls of the finite-difference Jacobian, which is why a counting wrapper disagrees with it for those two.
Common Misconceptions & Pitfalls
- "RK4 is fourth order for any ." Under smoothness. On a kink its fitted order was about 2, the same as a two-stage method.
- "A smooth solution guarantees the order." On up to the solution is , and RK4 has order 2.
- "Runge-Kutta needs derivatives of ." It needs only values of . The order result does need to be smooth.
- "Each extra stage adds one order." True up to 4. Order 5 needs six stages.
- "Compare methods per step." RK4 costs four times what Euler costs per step. Compare per evaluation.
- "
rtolis the error of the answer." It bounds a local estimate. On the orbit,rtol=1e-8gave . - "The defaults are fine."
rtol=1e-3is a default, not a judgment about the problem: the orbit ended with an error of 2.59. - "Higher order cures stiffness." Fixed RK4 needed 1440 evaluations where Euler needed 500. Use an implicit method.
- "
nfevcounts everything." It left out Jacobian evaluations forRadauandBDF.
Summary
Runge-Kutta methods sample inside the step and combine the slopes so that the Taylor expansion of the update matches the solution's, without derivatives of . Two stages give a one-parameter family of order 2, and RK4 satisfies the eight conditions for order 4 with four stages. Past order 4 the stage count grows faster than the order. On these methods reduce to quadrature rules, RK4 to Simpson. Measured orders of 1, 2, and 4 hold when is smooth, and fall to about 2 at a kink and about 1 at a jump. They can fall even when the solution is smooth, as on at , where is infinite. Explicit methods have bounded stability regions, so stiffness limits them whatever the order. An embedded pair estimates the local error from shared stages and drives the step size, which on an eccentric orbit saved a factor of about 60 over fixed RK4. The tolerance bounds a local estimate, so the final error has to be checked, and work is honestly compared by counting calls to .
Documentation Links
- Driscoll & Braun: Fundamentals of Numerical Computation, 6.4 Runge-Kutta methods
- Driscoll & Braun: Fundamentals of Numerical Computation, 6.5 Adaptive Runge-Kutta
- Demanet (2012): MIT OCW 18.330, Chapter 5, Methods for ordinary differential equations
- SciPy: scipy.integrate.solve_ivp
- SciPy: scipy.integrate.RK45
- SciPy: scipy.integrate.RK23
- SciPy: scipy.integrate.Radau