Calculus, Linear Algebra & Discrete Math Differential Equations Classical fourth-order Runge-Kutta (Kutta, 1901)

Runge-Kutta RK4 Calculator

Enter the right-hand side of dy/dx = f(x, y), an initial condition, a step size and a final x, and this calculator marches the classical fourth-order Runge-Kutta scheme across the interval - printing k1 through k4 and the updated y at every step so you can check a hand calculation line by line. It also runs Euler over the same interval for comparison, and repeats the march at half the step to give you a Richardson estimate of the error you are actually carrying.

Calculator

This calculator runs in your browser. Enable JavaScript for live results — the inputs, formula and worked example below remain fully readable without it.

Inputs this calculator takes, with typical values
InputWhat to enterExample
dy/dx = f(x, y)Enter the right-hand side only. Use x or t for the independent variable and y for the dependent one.y - x^2 + 1
Initial x₀The x where the solution value is known.0
Initial y(x₀)The known value of y at x₀.0.5
Step size hEnter the magnitude; the direction is taken from the final x. Smaller means more accurate and slower.0.2
Final xWhere the march stops. It may lie to the left of x₀, in which case the method integrates backwards.2

It returns

  • y at the final x — The RK4 estimate of the solution at the end of the interval.
  • Steps taken
  • Estimated error — Richardson estimate: the run at h against the run at h/2, divided by 15.
  • Euler's answer at the same h
  • RK4 minus Euler — How far the first-order method has drifted from the fourth-order one over the same interval.

The formula

yn+1=yn+h6(k1+2k2+2k3+k4)
E|yhyh/2|15

In plain text: yₙ₊₁ = yₙ + (h/6)(k₁ + 2k₂ + 2k₃ + k₄)

  • hStep size in x
  • k₁f(xₙ, yₙ) — slope at the left end of the step
  • k₂f(xₙ + h/2, yₙ + h·k₁/2) — midpoint slope from k₁
  • k₃f(xₙ + h/2, yₙ + h·k₂/2) — midpoint slope from k₂
  • k₄f(xₙ + h, yₙ + h·k₃) — slope at the right end

Local truncation error is O(h⁵) per step; global error over a fixed interval is O(h⁴).

Updated Category Differential Equations Verified against published test cases Reading time 11 min

What RK4 does and why it is the default

The classical fourth-order Runge-Kutta method advances an initial value problem dy/dx = f(x, y), y(x₀) = y₀ one step at a time. At each step it samples the slope four times - once at the start, twice near the middle, once at the end - and takes a weighted average of those four slopes as the effective slope for the whole step.

That averaging is what buys the accuracy. The Euler method uses only the slope at the start of the step, so it consistently lags a curving solution; the global error falls only in proportion to h. RK4 matches the Taylor expansion of the true solution through the h4 term, so its global error falls as h4. Halve the step and Euler's error halves; RK4's error drops by a factor of about sixteen.

The other reason RK4 became the default is that it needs nothing but f itself. No derivatives of f, no algebraic solve inside the step, no history of past points - so it starts itself, handles a change of step size without ceremony, and works on any equation you can evaluate. It costs four function evaluations per step, which is the price.

This calculator also runs Euler over the same interval with the same step, and runs RK4 again at half the step to estimate the error, so you can see what the extra work is buying on your particular problem.

The four stages, and where the weights come from

One step from (xₙ, yₙ) to (xₙ₊₁, yₙ₊₁):

  • k₁ = f(xₙ, yₙ) - the slope at the left-hand end, exactly what Euler would use.
  • k₂ = f(xₙ + h/2, yₙ + h k₁/2) - the slope at the midpoint, reached by taking a half Euler step with k1.
  • k₃ = f(xₙ + h/2, yₙ + h k₂/2) - the midpoint slope again, but reached using the better estimate k2.
  • k₄ = f(xₙ + h, yₙ + h k₃) - the slope at the right-hand end, reached with k3.

and then

yₙ₊₁ = yₙ + (h/6)(k₁ + 2k₂ + 2k₃ + k₄)

The weights 1, 2, 2, 1 divided by 6 are Simpson's rule. That is not a coincidence: if f happens to depend on x only, the differential equation is just an integral, and the four stages collapse to Simpson's rule applied on the step. RK4 is Simpson's rule generalised to the case where the integrand depends on the answer.

The weights are not free either - they are forced. Expand y(xₙ + h) as a Taylor series in h, expand the four-stage formula the same way, and demand that the two agree through h4. That produces a system of eleven order conditions in the stage coefficients, and the tableau above is the classical solution Wilhelm Kutta published in 1901. Other solutions exist - the 3/8 rule is the best known - and they differ in their error constants rather than in their order.

Because the expansion is matched through the fourth power, the error left over in a single step is proportional to h5. Over an interval of fixed length you take a number of steps proportional to 1/h, so those local errors accumulate into a global error proportional to h4. That is the distinction between local truncation error and global error, and it is why a fourth-order method is described by its global exponent.

Worked example: y′ = y − x² + 1, y(0) = 0.5, h = 0.2

This is the standard textbook test problem, and it has a closed-form solution y = (x + 1)² − 0.5ex to check against. Take the first step by hand.

  1. k₁. f(0, 0.5) = 0.5 − 0 + 1 = 1.5.
  2. k₂. The half step gives y = 0.5 + 0.2(1.5)/2 = 0.65, so f(0.1, 0.65) = 0.65 − 0.01 + 1 = 1.64.
  3. k₃. Now y = 0.5 + 0.2(1.64)/2 = 0.664, so f(0.1, 0.664) = 0.664 − 0.01 + 1 = 1.654.
  4. k₄. The full step gives y = 0.5 + 0.2(1.654) = 0.8308, so f(0.2, 0.8308) = 0.8308 − 0.04 + 1 = 1.7908.
  5. Combine. k₁ + 2k₂ + 2k₃ + k₄ = 1.5 + 3.28 + 3.308 + 1.7908 = 9.8788. Multiply by h/6 = 0.2/6 = 0.0333333: 9.8788 × 0.0333333 = 0.3292933. Add to y₀: y₁ = 0.5 + 0.3292933 = 0.8292933.

The exact solution at x = 0.2 is (1.2)² − 0.5e0.2 = 1.44 − 0.5(1.2214028) = 1.44 − 0.6107014 = 0.8292986. The RK4 step is short by 0.8292986 − 0.8292933 = 0.0000053, about five parts in a million after one step of size 0.2.

Compare Euler on the same step: y₁ = 0.5 + 0.2(1.5) = 0.8, an error of 0.0292986 - roughly 5,500 times larger, for one quarter of the arithmetic. Continue to x = 2 and the exact answer is 9 − 0.5e² = 9 − 3.6945280 = 5.3054720, which RK4 tracks closely all the way while Euler drifts steadily below it.

Choosing h and reading the error estimate

The error estimate reported here is Richardson extrapolation: run the whole interval at h, run it again at h/2, and take the difference divided by 15. The 15 is 24 − 1, and it comes straight from the fourth-order behaviour - if the error at h is Ch⁴, the error at h/2 is Ch⁴/16, so their difference is 15/16 of the larger error.

Use it as a target-driven step selector. If the estimate is ten times bigger than you can tolerate, cut h by a factor of about 1.8, because 1.84 ≈ 10.5. If it is a thousand times smaller than you need, you are paying for accuracy nobody asked for and can raise h by a factor of about 5.6.

Two situations break that reasoning. The first is a stiff equation, where the solution contains a component decaying far faster than the one you care about - y′ = −1000y + cos x is the classic. RK4 is only conditionally stable, so it needs h small enough to resolve the fast component even after that component has died away; exceed the stability limit and the numerical solution oscillates and explodes rather than gently losing accuracy. An implicit method is the answer, not a smaller explicit step.

The second is a solution that is not smooth. RK4's order argument assumes f has four continuous derivatives along the solution. Across a kink, a switch, or a discontinuity in a forcing term, the method silently drops to first order. Stop the integration at the discontinuity and restart it on the other side.

Watch the Euler comparison as a rough diagnostic of curvature. When Euler and RK4 nearly agree, the solution is close to straight over a step and almost any method would do. When they differ sharply, the solution is curving hard relative to your step size, and it is worth checking the Richardson estimate before trusting the result.

How the error falls with step size

Global error behaviour of the standard explicit schemes when the step is halved.
MethodOrderf evaluations per stepError factor when h is halvedSteps needed for 10× less error
Euler11÷ 210× as many
Heun / improved Euler22÷ 43.2× as many
Midpoint (RK2)22÷ 43.2× as many
Classical RK444÷ 161.8× as many
Runge-Kutta 3/8 rule44÷ 161.8× as many
Dormand-Prince RK455 (4 for the error estimate)6, one reused÷ 321.6× as many

The last column is 10^(1/p): the factor by which the number of steps must rise to cut the global error tenfold for a method of order p.

Pitfalls

  • Confusing local and global error. One RK4 step is accurate to O(h5); a whole interval is accurate to O(h4), because you take O(1/h) steps. Quoting the fifth power for the finished answer overstates the accuracy by a factor of 1/h.
  • Using RK4 on a stiff problem. The failure is not gradual: below the stability limit the answer is fine, above it the numerical solution oscillates with growing amplitude. If you find yourself needing a step far smaller than the features you care about, switch to an implicit solver.
  • Integrating through a discontinuity. A switch in a forcing term inside a step destroys the order argument. Break the integration at the switch point.
  • Assuming a smaller h is always better. Truncation error falls as h4, but rounding error accumulates roughly as 1/h. Past a certain point - typically when h is around 10−4 of the interval for double precision - the total error starts rising again.
  • Forgetting the interval must divide by h. This calculator shortens the last step to land exactly on your final x and tells you when it has done so, but a hand-written loop will usually overshoot or stop short.
  • Reading agreement between RK4 and Euler as confirmation. They agree when the solution is nearly straight over a step, which says nothing about whether either has resolved a feature between the steps.

Where RK4 fits among the solvers

RK4 is the middle of the family. Below it, the Euler method is the one-stage member, useful mainly for teaching and for showing what first-order accuracy costs. Above it sit the embedded pairs - Runge-Kutta-Fehlberg 4(5) and Dormand-Prince 5(4), the latter being MATLAB's ode45 and the default in most scientific libraries - which compute two solutions of different order from nearly the same stages and use their difference to steer an adaptive step size. That is the same Richardson idea this calculator uses, done inside each step instead of over the whole interval.

For stiff problems the explicit family is the wrong family entirely. Implicit schemes - backward Euler, the trapezoidal rule, and the backward differentiation formulas behind ode15s - solve an algebraic equation at each step, usually with a Newton or secant iteration inside the loop, and buy unconditional stability with that extra work.

When the equation happens to be linear and first order, you do not need a numerical scheme at all: the integrating factor method gives an exact answer. For constant-coefficient linear equations of any order the Laplace transform does the same. Numerical integration earns its keep on the equations those methods cannot touch - and it is worth solving one linear problem both ways to see how closely RK4 reproduces the exact answer.

A higher-order system is handled by the same code. Write y″ = g(x, y, y′) as the pair u′ = v, v′ = g(x, u, v) and apply RK4 to the vector (u, v), computing all four stages for both components before either is updated. This calculator handles the single-equation case; the arithmetic for a system is identical, component by component.

Frequently asked questions

Why does RK4 use four slope evaluations?

Because four is the smallest number of stages that can match the Taylor expansion of the true solution through the h⁴ term. Matching through h² needs two stages, through h³ needs three, and through h⁴ needs four - but the pattern stops there. Fifth-order accuracy requires six stages, not five, a result proved by Butcher. That kink in the cost curve is a large part of why the four-stage method became the standard.

What step size should I use?

Start with something that divides the interval into 20 to 100 steps, then look at the error estimate the calculator reports. If it is smaller than you need, raise h; if larger, cut it. Because the error scales as h⁴, small changes in h move the error a long way: halving h cuts the error by roughly sixteen, and a factor of 1.8 in h moves it by about ten.

How does RK4 compare with Euler in practice?

On the default problem here, one RK4 step of size 0.2 lands within 0.0000053 of the exact value, while one Euler step of the same size is off by 0.0292986. RK4 costs four function evaluations against Euler's one, so it is four times the work per step and thousands of times more accurate. The comparison only tilts towards Euler when f is extremely expensive and you need barely any accuracy.

Can I use this for a second-order equation?

Not directly - this page solves a single first-order equation. Convert first: write y″ = g(x, y, y′) as the system u′ = v, v′ = g(x, u, v). Then apply the same four stages to both components, computing k₁ for both before computing k₂ for either. The arithmetic is identical; there are just two of everything.

What does the error estimate actually measure?

The difference between the run at h and the run at h/2, divided by 15. It estimates the global error of the coarser run, on the assumption that the error really is proportional to h⁴. That assumption fails near a discontinuity or beyond the stability limit, where the two runs can differ for reasons that have nothing to do with truncation, so treat a very large estimate as a signal to investigate rather than as a number to trust.

Why did my solution blow up?

Either the equation genuinely has a finite-time blow-up - y′ = y², y(0) = 1 escapes to infinity at x = 1 - or the step exceeds the stability limit for a rapidly decaying term. The two look different: a genuine blow-up grows smoothly and monotonically as you approach the singular point, while a stability failure oscillates in sign with growing amplitude. Halving h fixes the second and does nothing for the first.

Is RK4 the same as Simpson's rule?

It reduces to it. When f depends on x alone, the four stages become f at the two endpoints and twice f at the midpoint, weighted 1, 2, 2, 1 over 6 - exactly Simpson's rule on the step. That is why RK4 integrates any cubic in x exactly, and why the calculator reports a zero error estimate for such problems: there is no truncation error left to measure.

How do I enter the differential equation?

Enter only the right-hand side of dy/dx = f(x, y). For dy/dx = y − x² + 1 you type y - x^2 + 1. Use x or t for the independent variable and y for the dependent one, ^ for powers, and names like sin, cos, exp, ln and sqrt. Multiplication may be written or implied, so 2xy and 2*x*y are the same.

References

  • Numerical Analysis, 10th ed. (Chapter 5, Initial-Value Problems for Ordinary Differential Equations) — Richard L. Burden and J. Douglas Faires, Cengage Learning
  • Numerical Methods for Ordinary Differential Equations, 3rd ed. — J. C. Butcher, Wiley
  • Solving Ordinary Differential Equations I: Nonstiff Problems, 2nd ed. — E. Hairer, S. P. Norsett and G. Wanner, Springer