Skip to content
Tutorials Center
My learning

Topics

Lesson 27 of 39 ยท Numerical Methods

Runge-Kutta Methods

Learn Runge-Kutta methods: midpoint and classical RK4, order of accuracy, Butcher tableaus, and a convergence study that shows why RK4 is the workhorse.

Euler's method samples the slope once per step, at the start. Better accuracy requires better slope estimates. Runge-Kutta (RK) methods sample the slope several times inside each step and combine the samples into a weighted average. Each extra evaluation of f buys a higher order of accuracy, without ever needing derivatives of f.

The best-known member, classical RK4, uses four evaluations per step and has global error proportional to h^4. It is a reliable default for smooth, non-stiff problems and the ancestor of the adaptive methods used by modern libraries.

Second-order: the midpoint method

Go halfway along the Euler tangent, read the slope there, and use that slope for the whole step:

TEXT
k1 = f(t, y)
k2 = f(t + h/2, y + (h/2) k1)
y_new = y + h k2

The midpoint slope is a far better estimate of the average slope over the interval. This method is second order: halve h, and the error drops by a factor of 4. Another second-order variant is Heun's method, which averages the slopes at both ends: k1 = f(t, y), k2 = f(t+h, y + h k1), y_new = y + (h/2)(k1 + k2).

Fourth-order: classical RK4

TEXT
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)
y_new = y + (h/6) (k1 + 2 k2 + 2 k3 + k4)

Think of it as Simpson's rule for integration, adapted to the case where the integrand depends on the unknown solution. Weights 1, 2, 2, 1 out of 6 favor the middle slopes.

Python Euler, midpoint 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.

The three columns shrink at very different speeds. Halving h divides the Euler error by about 2, the midpoint error by about 4 and the RK4 error by about 16.

Order of accuracy, precisely

A method has order p when its global error is O(h^p). Matching the Taylor series up to terms of size h^p is what the coefficients of an RK method are designed for. RK4 matches through h^4. Order 5 needs six function evaluations, so beyond RK4 the cost grows faster than the benefit; that is why order 4 is so popular for fixed steps.

You can measure the order from errors at h and h/2: p โ‰ˆ log2(error(h) / error(h/2)).

Python Estimate the order numerically

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.

You should see values close to 4.

The Butcher tableau

All RK methods share one template. A method with s stages is described by a matrix A (s x s), weights b and nodes c:

TEXT
k_i = f(t + c_i h,  y + h ฮฃ_j a_ij k_j)
y_new = y + h ฮฃ_i b_i k_i

For explicit methods A is strictly lower triangular. Putting c, A, b in a table is called a Butcher tableau, and it lets you write one generic stepper for every explicit RK method.

Python One generic RK stepper driven by a tableau

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.

Euler's energy grows more than sevenfold, Heun drifts by half a percent, and RK4 keeps it to about five digits over this time span. RK4 does not conserve energy perfectly for infinitely long runs, but it drifts very slowly. When long-term conservation matters, special symplectic integrators are used.

Orders and cost

Euler 1 stage, error ~ h.
Midpoint, Heun 2 stages, error ~ h squared.
RK4 4 stages, error ~ h to the 4th.

Order 4 is the sweet spot for fixed-step methods.

1 / 2

Choosing a step size

A common rule of thumb: for RK4, decreasing h ten times lowers the error about ten thousand times, so the step size needed for a given tolerance is moderate. But there is a floor: when h is so small that the truncation error is below rounding error (around 1e-16 relative), smaller steps no longer help and just accumulate more rounding. For most problems you will hit your goals long before this happens.

For problems where the solution changes fast in some places and slowly in others, fixed steps are wasteful. That is the job of adaptive methods.

Common mistakes

  • Computing k2 with y instead of the updated y + h/2 * k1.
  • Using f(t, ...) with the wrong time offsets, which silently degrades the order.
  • Implementing RK4 with weights 1, 1, 1, 1 over 4 instead of 1, 2, 2, 1 over 6.
  • Assuming higher order is always better: for non-smooth right-hand sides the order is not realized.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How many evaluations of f per step does classical RK4 use?

    k1, k2, k3 and k4 each call f once.
  2. 2. If you halve h for a fourth-order method, the error drops by about:

    Error is proportional to h^4, so halving gives 2^4 = 16 times smaller.
  3. 3. The weights in the RK4 combination are:

    RK4 averages slopes as (k1 + 2 k2 + 2 k3 + k4)/6, like Simpson's rule.
  4. 4. A Butcher tableau for an explicit method has a matrix A that is:

    Each stage depends only on earlier stages, so entries on and above the diagonal are zero.

Summary

  • Runge-Kutta methods combine several slope samples per step.
  • Midpoint and Heun are second order; classical RK4 is fourth order.
  • The observed order is log2 of the error ratio when the step is halved.
  • A Butcher tableau describes any explicit RK method for a generic implementation.
  • Fixed steps are wasteful for uneven problems; adaptive control comes next.

Next: Adaptive Steps and solve_ivp