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

Runge-Kutta and RK4

Euler trusts a single slope at the start of each step and pays for it. Runge-Kutta samples the slope at several points inside the step, blends them with clever weights, and wins many digits of accuracy for the same step size — RK4 is the workhorse you will reach for first.

Why one slope per step is not enough

The previous two guides built Euler's method and then exposed its weakness honestly: it is only first-order accurate, so halving the step buys you just one extra correct digit, and its local truncation error grows like the step squared. The root cause is simple to picture. To cross a step from x to x + h, Euler looks at the slope at exactly ONE place — the left end — and then marches in that frozen direction for the whole step. If the true curve bends during the step, Euler walks off the tangent and never notices.

Here is the central idea of this guide, and it is almost embarrassingly natural: if one slope sample is crude, take several slope samples inside the step and average them wisely. A driver who only glances at the road at the start of a curve will drift; a driver who keeps re-checking the road as the curve turns stays on it. That is the whole spirit of a Runge-Kutta method — sample the right-hand side f at a few cleverly chosen points across the step, then combine those slopes into one effective slope that captures the curve's bending.

The two-stage methods: a gentle first jump up

Before the famous four-stage formula, meet the two-stage cousins you may already know from guide 2, because they show the trick in miniature. The improved Euler method (also called Heun's method) does a rough Euler step to PREDICT where you land, measures the slope there, then averages that with the starting slope. The starting slope alone tilted you wrong on a bending curve; the predicted-end slope tilts you wrong the other way; their average splits the difference and lands far closer to the truth.

The midpoint method plays the same game differently: take a half-step with Euler to peek at the slope in the MIDDLE of the interval, then go back to the start and take the full step using that midpoint slope. The slope in the middle is a better single representative of the whole step than the slope at either end. Both of these are second-order methods — their order is 2, meaning the error per step scales like h^3 and halving h cuts the global error by roughly four, not two. Two well-placed slope samples already doubled the order over Euler.

RK4: four slopes, weighted like Simpson

Now the star. The classical RK4 method evaluates the slope FOUR times per step — once at the left end, twice at trial midpoints, and once at a trial right end — then blends them with carefully chosen weights to reach order 4. That order is the payoff: per step the local error scales like h^5, and the global error like h^4, so halving the step cuts the error by a factor of about sixteen. Getting one more correct digit costs only a modest shrink in h, which is why RK4 hits a sweet spot of accuracy versus effort that has made it the default first choice for sixty years.

One RK4 step from (x, y), step size h, slope f(x, y):

  k1 = f(x,        y)
  k2 = f(x + h/2,  y + (h/2) k1)
  k3 = f(x + h/2,  y + (h/2) k2)
  k4 = f(x + h,    y + h k3)

  y_next = y + (h/6)(k1 + 2 k2 + 2 k3 + k4)
  x_next = x + h
k1 is the left slope; k2 and k3 are two midpoint slopes (each built on the previous); k4 is a trial right-end slope. The weights 1, 2, 2, 1 over 6 are Simpson's rule in disguise.

Read the weights aloud and the structure sings: the two midpoint slopes count double, the two end slopes count single, all divided by six. That is exactly Simpson's rule for an integral — and there is a reason. If f happened to depend only on x (a pure-time equation y' = f(x)), one RK4 step would BE Simpson's rule applied to that integral, which is itself fourth-order accurate. RK4 is the natural generalization of Simpson to the case where the slope also depends on y, with the inner stages k2, k3, k4 supplying the y-values Simpson needs but does not have in advance.

Walking through a single RK4 step

Concrete beats abstract. Take the test problem y' = y with y(0) = 1, whose true answer is y = e^x, and march ONE step of size h = 0.5. Follow the four stages and watch how each one refines the slope before the final weighted blend.

  1. k1 = f(0, 1) = 1. The left-end slope, exactly what plain Euler would have used.
  2. k2 = f(0.25, 1 + 0.25 k1) = f(0.25, 1.25) = 1.25. A midpoint slope, taken after a half-step using k1.
  3. k3 = f(0.25, 1 + 0.25 k2) = f(0.25, 1.3125) = 1.3125. A second, better midpoint slope, this time built on k2.
  4. k4 = f(0.5, 1 + 0.5 k3) = f(0.5, 1.65625) = 1.65625. A trial right-end slope, using k3 to reach across the full step.
  5. Blend: y_next = 1 + (0.5/6)(k1 + 2 k2 + 2 k3 + k4) = 1 + (0.5/6)(7.875) = 1.6484375. The true value is e^0.5 = 1.6487213, so the error after one step is about 0.0003 — three good digits from a single coarse step.

Compare: plain Euler over that same step gives y_next = 1 + 0.5(1) = 1.5, an error of about 0.15 — five hundred times worse. The catch you should hold honestly: RK4 paid for this by evaluating f four times instead of once. The fair comparison is therefore at EQUAL work, not equal step count. Even there RK4 wins decisively for smooth problems, because its error shrinks so much faster as h drops that you can take far bigger steps and still finish ahead.

What RK4 still cannot do — staying honest

RK4 is wonderful but it is not magic, and four limits matter. First, its fourth-order accuracy assumes the true solution is SMOOTH; if f or the solution has a kink or a near-discontinuity inside a step, the high order silently collapses to something much lower. Second, order is an asymptotic statement about small h — a famous result is that no explicit Runge-Kutta method of order 5 can be built with only 5 stages, so the easy doubling of order with stages stops; buying order beyond 4 costs disproportionately more f-evaluations.

Third, RK4 uses a FIXED step here, which is wasteful where the solution is flat and risky where it suddenly turns sharp; the next guides cure this with predictor-corrector pairs and with adaptive step control such as the embedded RKF45, which estimates its own error on the fly. Fourth, and most important, RK4 is an EXPLICIT method, and every explicit method has a limited region of absolute stability. On a stiff equation — where some hidden mode decays violently fast — RK4 is forced to take absurdly tiny steps not for accuracy but just to avoid blowing up. That failure mode, stiffness, is the subject of the final guide in this rung.