The idea: read the slope more than once per step

A differential equation y′ = f(x, y) gives the slope of the solution at every point. Euler's method reads the slope once, at the start of a step, and follows it in a straight line. The curve bends while you walk, so that one slope is already out of date by the end of the step. Runge–Kutta methods read the slope at a few trial points inside the step and walk with a weighted average of them. The classic one, RK4, uses four slopes.

One step of size h = 1 for y prime = y from (0, 1). Slope k1 = 1 at the start, k2 = 1.5 at the midpoint (0.5, 1.5), k3 = 1.75 at (0.5, 1.75), k4 = 2.75 at the end point (1, 2.75). The RK4 step with the average slope 1.708 lands at 2.708, next to the exact e = 2.718; Euler's step lands at 2.
One RK4 step of size 1 on y′ = y: four slopes, averaged 1 : 2 : 2 : 1, land at 2.708 next to e ≈ 2.718; Euler's single slope lands at 2.

Euler: one slope

Euler's step is yn+1 = yn + h·k1 with k1 = f(xn, yn). For y′ = y from y(0) = 1 with h = 1, k1 = 1 and the step lands at 2, while the true value is e ≈ 2.718. The slope field, step size and stability of Euler's method are on Euler's Method; this page compares it with methods that take more samples.

Midpoint RK2: a trial half step

The midpoint method first follows k1 for half a step to a trial midpoint, reads the slope k2 there, then goes back to the start and takes the whole step with k2:

k2 = f(xn + h/2, yn + (h/2)·k1),   yn+1 = yn + h·k2

For the example, k2 = f(0.5, 1.5) = 1.5 and the step lands at 2.5. It is a two-stage Runge–Kutta method, like Heun's method on the Euler page.

RK4: four slopes, weights 1, 2, 2, 1

RK4 reads the slope at the start, twice at the midpoint, and at the end:

stageread atexample (y′ = y, h = 1)weight
k1the start (xn, yn)f(0, 1) = 11/6
k2midpoint reached with k1: yn + (h/2)·k1f(0.5, 1.5) = 1.52/6
k3midpoint reached with k2: yn + (h/2)·k2f(0.5, 1.75) = 1.752/6
k4end point reached with k3: yn + h·k3f(1, 2.75) = 2.751/6

yn+1 = yn + h·(k1 + 2k2 + 2k3 + k4)/6. Here the average slope is (1 + 3 + 3.5 + 2.75)/6 ≈ 1.708 and the step lands at 2.7083, an error of 0.01 after one big step. The weights 1, 4, 1 (the two midpoint slopes share the 4) are Simpson's rule: when f depends on x only, RK4 is exactly Simpson's rule for the integral (see Riemann sums). For y′ = y one RK4 step multiplies y by 1 + h + h²/2 + h³/6 + h⁴/24, the first five terms of the Taylor series of eh. (Demo: one RK4 step on y′ = y, h = 1)

Order: halve h, divide the error by 2, 4 or 16

A method has order p when its global error at a fixed x is about C·hp. Halving h then divides the error by 2p. The error table on the canvas, for y′ = y at x = 1 (exact e ≈ 2.71828):

hEuler errorratioRK2 errorratioRK4 errorratio
0.50.4680.07770.000936
0.250.2771.70.02343.30.000071913.0
0.1250.1521.80.006443.60.0000049814.4
0.06250.08041.90.001693.80.00000032815.2

The ratios approach 2, 4 and 16: Euler is first order, the midpoint method second, RK4 fourth. RK4 with h = 0.5 (2 steps) is about 86 times more accurate than Euler with h = 0.0625 (16 steps). (Demo: error table, halve h)

Log-log chart of the error at x = 1 against step size h from 0.0625 to 0.5. Euler's errors 0.0804 to 0.468 lie on a line of slope 1, RK2's errors 0.00169 to 0.0777 on a line of slope 2, RK4's errors 3.3e-7 to 0.00094 on a line of slope 4.
On log scales each method's error is a straight line whose slope is its order: 1, 2 and 4.

Counting the work

The expensive part of a real solver is computing f (a force, a chemical rate, a whole simulation step). Euler costs 1 slope evaluation per step, RK2 costs 2 and RK4 costs 4. A fair comparison gives every method the same number of evaluations: RK4 with h = 0.5 against Euler with h = 0.125. Because the error falls like h⁴, RK4 still wins by far. The runs table on the canvas shows the evaluations of each run. (Demo: orbit, same work)

The orbit: Euler gains energy, RK4 does not

A mass on a spring, x″ = −x, is the system x′ = v, v′ = −x. Drawn with position x across and velocity v up (the phase plane), the exact motion from (1, 0) goes round the unit circle forever, because the energy E = (x² + v²)/2 never changes (the same motion as the spring oscillator and a circular orbit).

An Euler step moves along the tangent of the circle. The tangent leaves the circle on the outside, so each step multiplies the radius by √(1 + h²) and the energy by 1 + h². The errors never cancel: Euler spirals outward. With h = 0.25, one turn (25 steps) ends at radius 2.13 with 4.6 times the energy. RK4's radius factor is about 1 − h⁶/144: for h = 0.25 the radius after one turn is 0.99996.

one turn, from (1, 0)hstepsslope evaluationsradius (exact 1)energy
Euler0.2525252.134✗ ×4.55
Midpoint RK20.2525501.012×1.025
RK40.25251000.99996✓ ×0.9999
Euler0.12550501.473✗ ×2.17
RK40.513520.9986✓ ×0.997

(Demo: orbit, Euler spirals outward; Demo: orbit, all three methods)

Phase plane x against v. The exact orbit is the unit circle. Euler's method with h = 0.25 spirals outward from (1, 0) and ends one turn later at radius 2.13. RK4 with h = 0.25 lies on the circle.
Same step h = 0.25 for one turn: Euler spirals out to radius 2.13, RK4 stays on the unit circle.

With friction added, the true orbit spirals inward instead; see Damped Oscillation. A numerical method that adds energy can hide that damping or invent motion that is not there, which is why the choice of method matters for long simulations.

RK4 with a fixed step dt = 0.01 traces a chaotic system on The Lorenz Attractor: Deterministic Chaos, where even the step size's tiny error grows until the path is different after t ≈ 20.

RK4 at work on a chaotic system, with the energy drift printed as a check of the integrator: The Double Pendulum: Normal Modes and Chaos.

What the page leaves out

RK4 also loses a little energy (radius factor just below 1), so over millions of steps it slowly spirals inward. For orbits, molecules and games, symplectic methods such as leapfrog / velocity Verlet are used: they are only second order but keep the energy bounded forever. Real solvers (RK45, Dormand–Prince, scipy.integrate.solve_ivp) pair two Runge–Kutta methods of different order to estimate each step's error and change h as they go. Stiff equations need implicit methods, and rounding error, which grows when h is extremely small, is ignored here.