Classical Runge–Kutta Explained with Worked Examples

The classical Runge–Kutta method (RK4) advances by where , , , .

Write 2x or 2*x to multiply, ^ for powers, and ; between simultaneous equations. You can also type factor, expand, derivative of, integrate … from a to b, limit … as x->0 or 15% of 80.

Try an example

Before you start

This page is easier if you are comfortable with:

What Classical Runge–Kutta (RK4) means

For , , : , , , , so against : error of order in a single step, versus for Euler's method (Euler's method). For : , , , , vs the exact . For , : , matching the exact solution .

The weights are those of Simpson's rule (Simpson's rule error) applied to the slope at the start, midpoint (twice) and end. The local error is of order and the global error of order : halving cuts the error by about 16. It needs four function evaluations per step.

RK4 is the default general-purpose solver for non-stiff problems, improving the midpoint method (the midpoint method) and Euler (stability of Euler's method). The solutions it approximates are those of separable and linear equations, and systems are treated in coupled first-order systems.

What is it used for?

RK4 takes four slope samples per step and averages them to follow the solution very accurately.

How to use classical runge–kutta (rk4)

  1. Compute .
  2. Compute and at the midpoint with the half-steps.
  3. Compute at the end point.
  4. Update .

Graph

RK4 step values for y′ = y: y(0.1) = 1.1051708 against e^xy1 = 1.105170800.050.10.150.20.250.30.350.40.450.50.911.11.21.31.41.51.61.7RK4 step values for y′ = y: y(0.1) = 1.1051708 against e^xxyexact e^xGraph with x from 0 to 0.5 and y from 0.9 to 1.7. Shows the curve exact e^x; RK4 point (2); point y1 = 1.1051708 at (0.1, 1.10517).

After one step RK4 agrees with e^0.1 to seven digits.

Worked examples

These examples are worked by hand, step by step, so you can compare your own working.

Example 1: y′ = y

Find one RK4 step with from .

Worked example
Answer
1.10517081.1051708
  1. k₁…k₄
  2. y₁

Example 2: y′ = −2y

Find one RK4 step with from .

Worked example
Answer
0.8187330.818733
  1. k₁…k₄
  2. y₁

Example 3: y′ = x + y

Find one RK4 step with from .

Worked example
Answer
1.1103421.110342
  1. k₁…k₄
  2. y₁

Special cases

A few situations come up again and again. Knowing them saves time and helps you spot answers that cannot be right.

y′ = y, y(0) = 1, h = 0.1

y₁ = 1.105171

Matches e^0.1.

Slopes

k₁ = 1, k₂ = 1.05, k₃ = 1.0525, k₄ = 1.10525

k₂, k₃ at the midpoint.

Exact

e^0.1 = 1.105171

Error ~ 8 × 10⁻⁸.

Euler for comparison

1 + 0.1 = 1.1

Error 0.005.

Word problems

The classical fourth-order Runge–Kutta method: yₙ₊₁ = yₙ + (h/6)(k₁ + 2k₂ + 2k₃ + k₄), with k₁ = f(xₙ, yₙ), k₂ = f(xₙ + h/2, yₙ + hk₁/2), k₃ = f(xₙ + h/2, yₙ + hk₂/2), k₄ = f(xₙ + h, yₙ + hk₃).

The four slopes

For y′ = y, y(0) = 1, h = 0.1 find k₁, k₂, k₃, k₄.

  1. k₁1
  2. k₂y + 0.05 = 1.05
  3. k₃1 + 0.1 × 1.05/2 = 1.0525
  4. k₄1 + 0.1 × 1.0525 = 1.10525
  5. Check: ✓1, 1.05, 1.0525, 1.10525.

The step

Find y₁.

  1. Weighted slope(1 + 2.1 + 2.105 + 1.10525)/6
  2. y₁1 + 0.1 × 1.051708
  3. Check: 1.105171 ✓1.105171.

Error Higher

Compare with e^0.1.

  1. exp(0.1) − y₁
  2. Check: ≈ 8 × 10⁻⁸ ✓8 × 10⁻⁸.

Euler comparison Higher

Find the Euler error for the same step.

  1. 1.105171 − 1.1
  2. Check: 0.005171 ✓0.005171.

Common mistakes

Wrong weights

(k₁ + k₂ + k₃ + k₄)/4 ✗

(k₁ + 2k₂ + 2k₃ + k₄)/6 ✓

Midpoint slopes count double.

Using the old y for k₃

k₃ from yₙ + hk₁/2 ✗

from yₙ + hk₂/2 ✓

Use the latest slope.

Forgetting h/6

adding weighted slopes only ✗

multiply by h/6 ✓

Scale by the step.

Assuming error O(h)

like Euler ✗

O(h⁴) ✓

Fourth-order accurate.

Exam tips

  1. Four slopes.
  2. Weights 1, 2, 2, 1.
  3. h/6.
  4. Fourth-order.
  5. Compare with exact.

Practice questions

Try each one on paper, then open the answer. Use the solver above to see the full working.

1. k₂ for y′ = y at y = 1, h = 0.1 Foundation

1.05

2. k₃ for y′ = y with k₂ = 1.05, h = 0.1 Foundation

1.0525

3. k₄ with k₃ = 1.0525 Foundation

1.10525

4. Weighted slope (k₁ = 1, 1.05, 1.0525, 1.10525) Higher

1.051708

5. y₁ for h = 0.2: e^0.2 to 6 d.p. Higher

1.221403

6. Euler y₁ for h = 0.2 Higher

1.2

Practice generator

Unlimited fresh questions with instant marking. Choose a level, work it out on paper, then type your answer.

Solve: …

Key terms

Runge–Kutta
Family of ODE solvers.
Slope (k)
Estimate of y′.
Step size (h)
Increment in x.
Order
Error O(hᵖ).
Local error
Error per step.
Euler method
First-order method.

Frequently asked questions

What is RK4?

A fourth-order Runge–Kutta method using four slopes per step.

What are the weights?

1, 2, 2, 1 over 6.

How accurate is it?

Global error O(h⁴).

Why better than Euler?

Much smaller error for the same h.

Which page covers Euler stability?

Euler method stability.

Which page covers coupled ODEs?

coupled first-order models.

More in Numerical methods and optimisation and the formula sheets.