Skip to content
Tutorials Center
My learning

Topics

Lesson 28 of 37 ยท Ordinary Differential Equations

Runge-Kutta Methods (RK2 and RK4)

Reach high accuracy for ODEs without computing derivatives. Implement midpoint, Heun and classic fourth-order Runge-Kutta, and verify their convergence order.

Euler's method uses one slope per step, so it is only first order. You could get higher order by using the Taylor series with derivatives of f, but those are painful to compute. Runge-Kutta (RK) methods achieve the same accuracy by evaluating f several times per step at cleverly chosen points and combining the slopes. You write nothing but f.

The famous member is the classic fourth-order method, "RK4". It is the workhorse of simple simulations: accurate, easy to code, and a fine default when you do not need step-size control.

Second-order methods

Midpoint method: take a half Euler step to the middle of the interval, evaluate the slope there, and use it for the full step.

k1 = f(tn, yn) k2 = f(tn + h/2, yn + h/2 * k1) y(n+1) = yn + h * k2

Heun's method (improved Euler): average the slopes at both ends.

k1 = f(tn, yn) k2 = f(tn + h, yn + h * k1) y(n+1) = yn + h * (k1 + k2) / 2

Both have local error O(h^3) and global error O(h^2).

Classic RK4

k1 = f(tn, yn) k2 = f(tn + h/2, yn + h/2 * k1) k3 = f(tn + h/2, yn + h/2 * k2) k4 = f(tn + h, yn + h * k3) y(n+1) = yn + h/6 * (k1 + 2 k2 + 2 k3 + k4)

It is a weighted average (like Simpson's rule; indeed for y' = f(t) it reduces to Simpson's rule on the step). Four evaluations per step, local error O(h^5), global error O(h^4).

flowchart LR
  K1["k1: slope at start"] --> K2["k2: slope at midpoint<br/>using k1"]
  K2 --> K3["k3: slope at midpoint<br/>using k2"]
  K3 --> K4["k4: slope at end<br/>using k3"]
  K4 --> Y["Weighted average<br/>1, 2, 2, 1 over 6"]
Python Euler, Heun and RK4 side by side

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

With h = 0.125, RK4 has an error near 2e-5 while Euler is off by about 0.3. RK4 uses four evaluations per step; Euler would need an enormously smaller h for the same accuracy.

Measuring the order

Always verify that your implementation converges at the expected rate: halve h and check that the error shrinks by 2^p.

Python Error ratios reveal the order

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Ratios near 16 confirm fourth order. If you see 2 or 4 when you expected 16, there is a bug (typically a wrong time argument in k2 or k3).

Butcher tableaus

Every RK method is defined by a table of coefficients: nodes c, weights b and a matrix a. The classic RK4 tableau is

TEXT
0   |
1/2 | 1/2
1/2 | 0    1/2
1   | 0    0    1
----+---------------------
    | 1/6  2/6  2/6  1/6

Explicit methods have a lower-triangular a (each k depends only on earlier ones). Implicit methods have a full a and require solving equations, like backward Euler (the simplest implicit RK method). Reading a tableau lets you implement a new method in a few lines.

Using SciPy

scipy.integrate.solve_ivp offers many RK variants. method="RK45" is the default (an adaptive pair, covered in adaptive steps and stiff ODEs). To compare with a fixed-step RK4, pass t_eval and tight tolerances.

Python Compare with solve_ivp

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A scalar ODE is just a system with one component. To solve systems (pendulums, predator-prey, orbits) the same RK code works with y as a NumPy array; see systems and higher-order ODEs.

Common mistakes

  • Using the wrong time argument for k2, k3, k4; the code still runs but loses accuracy.
  • Assuming a higher-order method fixes instability: explicit RK4 still has a stability limit (roughly h * k < 2.78 for decay).
  • Using RK4 for very non-smooth right-hand sides (switches, impacts): the order drops.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How many evaluations of f does classic RK4 need per step?

    k1, k2, k3 and k4 each need one evaluation.
  2. 2. What is the global error order of RK4?

    Local error O(h^5) accumulates over 1/h steps to O(h^4).
  3. 3. You halve h with a fourth-order method. By about what factor does the error fall?

    Error ~ h^4, so halving h gives a factor 2^4 = 16.
  4. 4. What is Heun's method?

    Heun predicts with Euler, then averages the start and predicted-end slopes.

Summary

  • Runge-Kutta methods combine several slope evaluations per step to reach higher order without derivatives of f.
  • Midpoint and Heun are second order; classic RK4 is fourth order with four evaluations per step.
  • Verify order numerically by halving h and checking error ratios 2^p.
  • Explicit RK methods still have stability limits; stiff problems need other methods.

Next: Adaptive steps and stiff ODEs