TExcelTAD
Home › Numerical Methods › Runge-Kutta Methods

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:

k1=f(ti,ui),k2=f(ti+h,ui+hk1),ui+1=ui+h2(k1+k2).k_1 = f(t_i, u_i), \quad k_2 = f(t_i + h, u_i + hk_1), \quad u_{i+1} = u_i + \frac{h}{2}(k_1 + k_2).

The midpoint method evaluates the slope halfway: k2=f(ti+h2,ui+h2k1)k_2 = f\left(t_i + \frac{h}{2}, u_i + \frac{h}{2}k_1\right), ui+1=ui+hk2u_{i+1} = u_i + hk_2. Both are second order.

RK4

k1=f(ti,ui),k2=f(ti+h2,ui+h2k1),k3=f(ti+h2,ui+h2k2),k4=f(ti+h,ui+hk3),ui+1=ui+h6(k1+2k2+2k3+k4).\begin{aligned} k_1 &= f(t_i, u_i), & k_2 &= f\left(t_i + \tfrac{h}{2}, u_i + \tfrac{h}{2}k_1\right),\\ k_3 &= f\left(t_i + \tfrac{h}{2}, u_i + \tfrac{h}{2}k_2\right), & k_4 &= f(t_i + h, u_i + hk_3),\\ u_{i+1} &= u_i + \tfrac{h}{6}(k_1 + 2k_2 + 2k_3 + k_4). \end{aligned}

For ff independent of uu it reduces to Simpson's rule. Its global error is O(h4)O(h^4) for four evaluations of ff per step.

python · try it yourself
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 nn 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 nn steps uses 4n4n evaluations, Euler with 4n4n steps the same.

python · try it yourself
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 ss-stage explicit RK method is written compactly as a Butcher tableau: nodes cic_i, coefficients aija_{ij} (strictly lower triangular for explicit methods) and weights bjb_j, with ki=f(t+cih,u+h∑jaijkj)k_i = f(t + c_ih, u + h\sum_ja_{ij}k_j) and ui+1=ui+h∑jbjkju_{i+1} = u_i + h\sum_jb_jk_j. 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 u′=λuu' = \lambda u gives ui+1=R(hλ)uiu_{i+1} = R(h\lambda)u_i with R(z)=1+z+z22+z36+z424R(z) = 1 + z + \frac{z^2}{2} + \frac{z^3}{6} + \frac{z^4}{24}. The method is stable where ∣R(z)∣≤1\lvert R(z)\rvert \le 1; on the negative real axis this means h∣λ∣≤2.785h\lvert\lambda\rvert \le 2.785. 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 h=0.1h = 0.1 for u′=uu' = u, u(0)=1u(0) = 1.

Answer

u1=1.10517083u_1 = 1.10517083 (exact e0.1=1.10517092e^{0.1} = 1.10517092).

Solution

k1=1k_1 = 1, k2=1.05k_2 = 1.05, k3=1.0525k_3 = 1.0525, k4=1.10525k_4 = 1.10525; u1=1+0.16(1+2.1+2.105+1.10525)=1.10517083‾u_1 = 1 + \frac{0.1}{6}(1 + 2.1 + 2.105 + 1.10525) = 1.1051708\overline{3}. This equals 1+h+h22+h36+h4241 + h + \frac{h^2}{2} + \frac{h^3}{6} + \frac{h^4}{24}.

Exercise 2: Cost comparison

To reach error 10−810^{-8} on a problem where Euler's error is hh and RK4's is h4h^4 (with constants 1), how many function evaluations does each need on [0,1][0, 1]?

Answer

Euler 10810^8; RK4 4×100=4004 \times 100 = 400.

Solution

Euler: h=10−8h = 10^{-8}, 10810^8 steps. RK4: h4=10−8h^4 = 10^{-8} gives h=10−2h = 10^{-2}, 100 steps of 4 evaluations.

Exercise 3: Order of the midpoint method (code)

Implement the midpoint method and measure its order on u′=−2tuu' = -2tu, u(0)=1u(0) = 1, t∈[0,2]t \in [0, 2].

Answer

The error ratios approach 4: second order.

Solution
python · try it yourself
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…

  1. O(h)O(h)
  2. O(h2)O(h^2)
  3. O(h4)O(h^4)
  4. O(h5)O(h^5)

For u′=g(t)u' = g(t) (no uu dependence), RK4 reduces to…

  1. the trapezoid rule
  2. Simpson's rule
  3. the midpoint rule
  4. Gauss quadrature

Why is RK4 usually more efficient than Euler despite 4 evaluations per step?

  1. It is not
  2. Its error falls so much faster with hh that far fewer steps are needed
  3. It needs no evaluations
  4. It is implicit