Skip to content
Tutorials Center
My learning

Topics

Lesson 13 of 47 ยท Ordinary Differential Equations

Runge-Kutta Methods

Learn the midpoint and classical fourth-order Runge-Kutta methods, implement RK4 in Python, measure convergence order, and compare accuracy per function evaluation.

Euler's method uses the slope at the start of each step and hopes the slope does not change much. Runge-Kutta (RK) methods are smarter: they sample the slope at several points inside the step and blend them into a better average. The most famous member, RK4, is the workhorse of computational physics. It is simple enough to write from memory and accurate enough for most smooth problems.

The curve e to the t over one step with four coloured slope marks k1 to k4 and the RK4 formula
RK4 samples four slopes in one step: at the start, twice in the middle, and at the end. Here it turns 2.0 (Euler) into 2.708 against the exact 2.718.

What you will learn

  • The midpoint (RK2) method and how it improves on Euler.
  • The classical RK4 formula and a general implementation.
  • How to measure the order of accuracy with a convergence study.
  • Why you should compare methods by cost, not only by step size.

Key terms

Stage: one evaluation of the right-hand side f inside a step (kโ‚, kโ‚‚, โ€ฆ).

Order p: global error scales as h^p.

Function evaluation: a call to f; for expensive physics this is the real cost.

Explicit method: the new value is computed directly from known values, with no equation to solve.

The midpoint method (RK2)

Idea: use the slope at the middle of the step instead of the start.

  1. kโ‚ = f(t, y)
  2. kโ‚‚ = f(t + h/2, y + (h/2) kโ‚)
  3. y_{n+1} = y_n + h kโ‚‚

The estimate of the midpoint value uses an Euler half-step. The result is second order: global error O(hยฒ). Two function calls per step buy an extra power of h.

Classical RK4

RK4 uses four slopes:

kโ‚ = f(t, y) kโ‚‚ = f(t + h/2, y + (h/2) kโ‚) kโ‚ƒ = f(t + h/2, y + (h/2) kโ‚‚) kโ‚„ = f(t + h, y + h kโ‚ƒ)

y_{n+1} = y_n + (h/6)(kโ‚ + 2kโ‚‚ + 2kโ‚ƒ + kโ‚„)

The weights 1, 2, 2, 1 (divided by 6) are chosen so that the Taylor series of the numerical step matches the exact series through the hโด term. The global error is O(hโด): halve h and the error drops by a factor of 16.

For dy/dt = y, yโ‚€ = 1, h = 1, the slopes are 1, 1.5, 1.75, 2.75, giving yโ‚ = 1 + (1 + 3 + 3.5 + 2.75)/6 = 2.7083. Euler gives 2, and the exact value is e = 2.7183.

Python RK4 in ten lines

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 ratio approaches 16, which confirms fourth order.

Measuring order on an oscillator

A convergence study on the oscillator xโ€ณ = โˆ’x with x(0) = 1, v(0) = 0 shows the contrast between methods. The exact answer is x(T) = cos T. Plot the error at T = 10 against h on log-log axes; the slope of each line is its order.

Python Euler, midpoint and RK4 compared

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.

Cost versus accuracy

Smaller steps are not the only way to buy accuracy. A fair comparison counts the number of function evaluations (calls to f): Euler uses 1 per step, midpoint 2, RK4 4. If each call is expensive, such as a force between 10,000 particles, the number of calls is the bill. The run below counts how many evaluations each method needs to reach an error below one millionth: the gap is enormous (millions for Euler, tens of thousands for midpoint, hundreds for RK4).

Python Accuracy per function evaluation

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.

Practical advice

  • Use RK4 for smooth problems where you want good accuracy and do not need long-time energy conservation.
  • Check h by halving it: the answer should change by about 1/16 of the previous change.
  • For problems with very different time scales (stiff problems) or rapidly changing behavior, use an adaptive solver such as solve_ivp (RK45, DOP853, LSODA) that adjusts h automatically.
  • RK4 is not symplectic: over many oscillations it slowly loses or gains energy. The next lesson explains what to do about that.

Common misconceptions

"RK4 is always better than Euler." It is better per unit of accuracy, but it costs four times as much per step. For a crude answer, or for functions that are not smooth, the advantage shrinks.

"Higher order means smaller error for every step size." Order tells you how error shrinks as h โ†’ 0. With a huge step the high-order method can still blow up.

"The four slopes are four independent estimates of the same slope." They are evaluated at different states, and the weights 1, 2, 2, 1 are chosen to cancel Taylor terms, not to average noise.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. RK4 is a method of order

    Its global error is proportional to h^4.
  2. 2. You halve the step in an order-4 method. The error falls by about

    2^4 = 16.
  3. 3. How many evaluations of f does one RK4 step need?

    k1, k2, k3 and k4: four evaluations.
  4. 4. In the RK4 formula the weights on k1, k2, k3, k4 are

    The end slopes count once and the two midpoint slopes count twice, divided by 6.
  5. 5. Which is the fairest way to compare two ODE methods?

    The expensive part of a physics simulation is evaluating the forces.

Summary

  • Runge-Kutta methods sample slopes inside the step and blend them.
  • Midpoint is second order with 2 evaluations; RK4 is fourth order with 4.
  • Measure order with a convergence study: slope on a log-log plot.
  • Compare methods by accuracy per function evaluation.
  • RK4 is not symplectic, so energy drifts slowly over very long runs.

Next: Oscillators and Energy