JOVANA
Explore Library Glossary Getting Started Three Levels Fields How it works Mission
Join the mission
All guides

Runge-Kutta Methods and RK4

Euler trusts the slope only at the start of a step; Runge-Kutta peeks at the slope a few times inside the step and blends those peeks into one cleverly weighted move. The classic four-stage RK4 squeezes fourth-order accuracy out of four slope evaluations — the workhorse that quietly runs most of the world's ODE simulations.

Why one slope per step is not enough

We are still solving the same initial-value problem y'(t) = f(t, y) with y(t_0) given, and still marching forward in steps of size h. The previous guides showed the trouble with forward Euler: it reads the slope f(t_n, y_n) once, at the START of the step, then walks in a straight line for the whole interval. But the true solution curves as it goes, so a slope sampled only at the left edge is stale by the time you reach the right edge. That single stale reading is exactly why Euler is only first order — its local truncation error per step is O(h^2), giving a disappointing O(h) global error.

The obvious fix is to not trust one slope blindly. If the curve bends during the step, why not sample the slope at SEVERAL points inside the step and average them, so the straight move you finally take reflects how the slope changed along the way? That is the entire idea of a Runge-Kutta method: take a step using a weighted blend of slopes evaluated at cleverly chosen sub-points within the interval. Crucially, this keeps the method one-step — to advance from y_n to y_{n+1} it uses only y_n, no memory of earlier points — so it is self-starting and copes gracefully when you change h.

Recall from the previous guide that the order of a method, the p in O(h^p) for the global error, is the single most important number describing it. Euler is order 1. Every extra order means that halving h cuts the error not by 2 but by 2^p, so reaching a tight tolerance needs far fewer steps. Runge-Kutta's promise is to buy a high order not by taking smaller steps, but by spending a few extra function evaluations WITHIN each step — and function evaluations of f are usually the expensive part, so the trade has to be worth it.

Warming up: the midpoint method (RK2)

Start with the simplest improvement, using just two slope readings. First take a tentative Euler half-step to probe the slope in the MIDDLE of the interval rather than at its stale left edge. Call the starting slope k1 = f(t_n, y_n); use it to hop halfway, to t_n + h/2 and y_n + (h/2) k1; then read the slope THERE, k2 = f(t_n + h/2, y_n + (h/2) k1). Finally take the real full step using only k2, the midpoint slope: y_{n+1} = y_n + h k2. The first reading was scaffolding, thrown away; the second, taken nearer the middle of the arc, is the one we actually walk on.

Why does this jump from order 1 to order 2? The exact step is captured by a Taylor expansion of y(t_n + h) in powers of h. Euler matches only the h^1 term and misses the (h^2/2) y'' term, which is its leading error. The midpoint method is built so that the midpoint slope, expanded in its own Taylor series, exactly reproduces that missing h^2 term — the two readings are weighted so the second-order error cancels. So its local error per step drops to O(h^3) and the global error to O(h^2). Halving h now cuts the error fourfold instead of twofold. Two slope evaluations, one extra order: a fair trade.

RK4: the classic four-stage workhorse

The most famous member of the family is the classical fourth-order method, RK4. It samples the slope four times across the step and blends them into a single weighted average. Two of the readings are taken at the left edge and the right edge, but the two MIDDLE readings — taken at the half-step point and each refining the previous one — are weighted twice as heavily as the endpoints. The pattern of weights, 1, 2, 2, 1 over a total of 6, is the same Simpson-like rule that makes the classic quadrature methods accurate, and it is no coincidence: integrating y' over the step is exactly a quadrature problem.

Classical RK4, one step from (t_n, y_n) to (t_n+h, y_{n+1}):

  k1 = f(t_n,        y_n)              # slope at the left edge
  k2 = f(t_n + h/2,  y_n + (h/2) k1)  # slope at midpoint, using k1
  k3 = f(t_n + h/2,  y_n + (h/2) k2)  # slope at midpoint, refined by k2
  k4 = f(t_n + h,    y_n +  h    k3)  # slope at the right edge, using k3

  y_{n+1} = y_n + (h/6) (k1 + 2 k2 + 2 k3 + k4)

Four evaluations of f per step.  Local error O(h^5), global error O(h^4).
The four-stage classical RK4: probe the slope at the start, twice at the midpoint, and at the end, then combine with weights 1:2:2:1. Each k after the first reuses the previous one.

Read the recipe like a story. k1 is the naive Euler slope at the start. k2 uses k1 to step halfway and re-reads the slope at the midpoint — a first guess at the middle. k3 redoes the midpoint, but starting from k2's better information, so it is a sharper midpoint estimate. k4 uses k3 to reach the far end and reads the slope there. The final move walks along the weighted average, leaning hardest on the two midpoint slopes because the middle of the arc best represents the whole step. Four readings, all but the first reusing the one before, woven into a single confident stride.

Why is order 4 the sweet spot everyone reaches for? It is the best deal on the cost-versus-order curve: four function evaluations buy four orders, exactly one order per evaluation, which is the highest such ratio you can get. Beyond order 4 the order barrier bites — order 5 needs 6 stages, so you pay 6 evaluations for 5 orders and the efficiency slips. That is why RK4 became the textbook default and the quiet engine inside countless physics, robotics, and orbital simulations: dead simple to code, self-starting, and remarkably accurate for its cost.

The Butcher tableau: every RK method as a small table

All the bookkeeping above — where to sample, how far to step, how to weight — is captured compactly by a Butcher tableau. It is a little grid holding three things: the c values (at what fractions of h along the step each slope is sampled), the a values (how much of each earlier slope feeds into the next sub-step), and the b values (the final weights that blend the slopes into the step). Give me a tableau and I can write the method mechanically; the table IS the method.

The tableau also draws a sharp line between two worlds. If the a-grid is strictly lower-triangular, each slope depends only on slopes already computed, so you evaluate them one after another — this is an explicit method, like RK4. If the a-grid has entries on or above the diagonal, a slope depends on ITSELF, and you must solve an equation (often a nonlinear system) at every step to find it — an implicit method, like backward Euler. Implicit RK methods cost much more per step, but, as the stiffness guide will show, they buy stability that explicit methods simply cannot, no matter how small h gets.

One elegant payoff of the tableau view is the embedded Runge-Kutta pair. Choose a tableau whose stages can be combined with TWO different sets of b-weights — one giving, say, order 4 and the other order 5 — sharing the very same k1...k_s slope evaluations. The gap between the two answers is a cheap, on-the-fly estimate of the local error, with almost no extra work since the expensive slopes are reused. That error estimate is exactly the fuel for the adaptive step-size control of the next guide, where the solver shrinks h where the solution is wild and stretches it where the solution is calm.

A tiny worked step, and honest limits

Make it concrete with the toy problem y' = y, y(0) = 1, whose exact answer is y = e^t. Take one RK4 step of size h = 0.5. Here f(t, y) = y, so each slope is just the current y value. We get k1 = 1, k2 = 1 + 0.25(1) = 1.25, k3 = 1 + 0.25(1.25) = 1.3125, k4 = 1 + 0.5(1.3125) = 1.65625. Blending, y_1 = 1 + (0.5/6)(1 + 2(1.25) + 2(1.3125) + 1.65625) = 1.6484375. The exact value is e^0.5 = 1.6487212..., so RK4 is right to about 4 decimal places in a single half-unit step. Plain Euler with the same h would give 1.5 — off in the FIRST decimal. The difference in quality is stark.

Three more honest cautions before you reach for RK4 everywhere. First, fourth-order accuracy is still APPROXIMATE and lives inside floating-point: in floating-point arithmetic each slope and each weighted sum is rounded, so once h is small enough the O(h^4) truncation error sinks below the accumulating round-off and shrinking h further stops helping — there is a round-off floor. Second, with a FIXED step RK4 has no idea how accurate it is being; without an embedded error estimate it cannot adapt, which is why pairs and adaptive control come next. Third, and most important, RK4 is EXPLICIT, so it is only conditionally stable — for a stiff problem its stability limit forces a punishingly tiny h regardless of accuracy, and you must switch to an implicit or BDF method. That cliff is the whole subject of the final guide in this rung.