Runge-Kutta Methods
Euler's method uses the slope only at the start of each step. Runge-Kutta methods evaluate the slope at several points inside the step and combine them so that the Taylor expansion of the true solution is matched to higher order. The classical fourth-order method, RK4, is accurate, simple and self-starting, and it remains a favourite for fixed-step simulation.
Improving Euler
Improved Euler (Heun's method) takes an Euler step as a predictor, evaluates the slope there, and averages:
The midpoint method evaluates the slope halfway: , . Both are second order.
RK4
For independent of it reduces to Simpson's rule. Its global error is for four evaluations of per step.
import numpy as np
def euler_step(f, t, u, h):
return u + h * f(t, u)
def heun_step(f, t, u, h):
k1 = f(t, u); k2 = f(t + h, u + h * k1)
return u + h / 2 * (k1 + k2)
def rk4_step(f, t, u, h):
k1 = f(t, u)
k2 = f(t + h / 2, u + h / 2 * k1)
k3 = f(t + h / 2, u + h / 2 * k2)
k4 = f(t + h, u + h * k3)
return u + h / 6 * (k1 + 2 * k2 + 2 * k3 + k4)
def solve(step, f, a, b, u0, n):
h = (b - a) / n; u = np.array(u0, dtype=float)
for i in range(n):
u = step(f, a + i * h, u, h)
return u
f = lambda t, u: np.array([u[1], -u[0]]) # harmonic oscillator
exact = np.array([np.cos(10.0), -np.sin(10.0)])
print(" n Euler Heun RK4")
for n in (100, 200, 400, 800):
errs = [np.linalg.norm(solve(s, f, 0, 10, [1, 0], n) - exact) for s in (euler_step, heun_step, rk4_step)]
print(f"{n:4d} " + " ".join(f"{e:.2e}" for e in errs)) n Euler Heun RK4
100 6.46e-01 1.67e-02 8.33e-06
200 2.84e-01 4.17e-03 5.21e-07
400 1.33e-01 1.04e-03 3.26e-08
800 6.45e-02 2.60e-04 2.03e-09
Doubling divides the errors by about 2, 4 and 16.
Efficiency: error per function evaluation
Higher order costs more evaluations per step, but for any useful accuracy it wins overwhelmingly. Compare at equal cost: RK4 with steps uses evaluations, Euler with steps the same.
import numpy as np
def rk4(f, a, b, u0, n):
h = (b - a) / n; u = float(u0)
for i in range(n):
t = a + i * h
k1 = f(t, u); k2 = f(t + h/2, u + h/2*k1); k3 = f(t + h/2, u + h/2*k2); k4 = f(t + h, u + h*k3)
u += h / 6 * (k1 + 2*k2 + 2*k3 + k4)
return u
f = lambda t, u: -2 * t * u
exact = np.exp(-4.0)
for evals in (200, 800, 3200):
n_e = evals; h = 2 / n_e; u = 1.0
for i in range(n_e):
u += h * f(i * h, u)
print(f"{evals:5d} evaluations: Euler error {abs(u - exact):.1e}, RK4 error {abs(rk4(f, 0, 2, 1.0, evals // 4) - exact):.1e}") 200 evaluations: Euler error 6.1e-04, RK4 error 1.5e-07
800 evaluations: Euler error 1.5e-04, RK4 error 5.4e-10
3200 evaluations: Euler error 3.8e-05, RK4 error 2.0e-12
Butcher tableaus
A general -stage explicit RK method is written compactly as a Butcher tableau: nodes , coefficients (strictly lower triangular for explicit methods) and weights , with and . Order conditions on these coefficients become numerous quickly: order 4 needs 8 conditions, order 8 needs 200. Beyond order 4 more stages than the order are needed (order 5 needs 6 stages).
Stability
RK4 applied to gives with . The method is stable where ; on the negative real axis this means . Still a finite limit: all explicit methods struggle with stiff problems (Stiff Equations).
In software
solve_ivp(method="RK45")(Dormand-Prince 5(4)),"DOP853"(order 8),"RK23"(Bogacki-Shampine). All adapt their step size (Adaptive Step Size).- Fixed-step RK4 is common in games, robotics and real-time control where the step is set by the frame or control rate.
Exercises
Exercise 1: One RK4 step by hand
Take one RK4 step with for , .
Answer
(exact ).
Solution
, , , ; . This equals .
Exercise 2: Cost comparison
To reach error on a problem where Euler's error is and RK4's is (with constants 1), how many function evaluations does each need on ?
Answer
Euler ; RK4 .
Solution
Euler: , steps. RK4: gives , 100 steps of 4 evaluations.
Exercise 3: Order of the midpoint method (code)
Implement the midpoint method and measure its order on , , .
Answer
The error ratios approach 4: second order.
Solution
import numpy as np
f = lambda t, u: -2 * t * u
errs = []
for n in (50, 100, 200, 400):
h = 2 / n; u = 1.0
for i in range(n):
t = i * h
u += h * f(t + h / 2, u + h / 2 * f(t, u))
errs.append(abs(u - np.exp(-4.0)))
print(np.round(np.array(errs[:-1]) / np.array(errs[1:]), 3))[4.192 4.093 4.046]
Test yourself
RK4 has global error…
For (no dependence), RK4 reduces to…
- the trapezoid rule
- Simpson's rule
- the midpoint rule
- Gauss quadrature
Why is RK4 usually more efficient than Euler despite 4 evaluations per step?
- It is not
- Its error falls so much faster with that far fewer steps are needed
- It needs no evaluations
- It is implicit