Calculator · Engineering Mathematics

Runge-Kutta 4 (RK4) Calculator

March a scalar first-order initial-value problem with the classical fourth-order Runge-Kutta method: four derivative evaluations per step, fourth-order accuracy at a fixed end time, and a fixed step that adapts to nothing.

Initial-value problem

Variables t and y. Operators + − * / ^, parentheses, pi and e, and sin, cos, tan, asin, acos, atan, exp, sqrt, abs, ln, log, log10. Write 2*y, not 2y.

A negative h integrates backward in time; h = 0 is rejected.

Whole number, 1 to 10,000. The interval is t₀ + N·h; the endpoint is derived, never entered.

A function of t only, e.g. exp(-t). Leave empty for no reference: nothing is then called an error.

The interval is defined by t₀, h and N. The end time is t₀ + N·h = 2: it is derived from the step contract, so no step is ever resized to reach a separately entered endpoint.

Result at the end time

End time t_end = t₀ + N·h2
Numerical value y_N0.1353355284
Steps N20
Step size h0.1
Derivative evaluations80 (4 per step)
Integration directionforward (h > 0)
Supplied reference at t_end0.1353352832
Absolute error vs supplied reference2.451852e-7

The supplied reference agrees with the initial condition at t₀, so the differences below are reported as error relative to that reference.

Trajectory

Classical RK4 yₙy_ref(t) = exp(-t) (supplied reference)
y′ = -y from t₀ = 0 to t_end = 2 with h = 0.1. Every marker is a computed node; the straight segments between them are a reading aid, not computed values.

Trajectory nodes

Every computed node with the supplied reference at the same node time. A relative error is left undefined where the reference is exactly zero.
ntₙyₙy_ref(tₙ)|yₙ − y_ref|relative
00110.000e+00.000e+0
10.10.90483750.9048374188.196e-89.058e-8
20.20.81873090140.81873075311.483e-71.812e-7
30.30.7408184220.74081822072.013e-72.718e-7
40.40.67032028890.6703200462.429e-73.623e-7
50.50.60653093440.60653065972.747e-74.529e-7
60.60.54881193440.54881163612.983e-75.435e-7
70.70.49658561870.49658530383.149e-76.341e-7
80.80.44932928970.44932896413.256e-77.247e-7
90.90.40656999120.40656965973.315e-78.153e-7
1010.36787977440.36787944123.332e-79.058e-7
111.10.33287141540.33287108373.317e-79.964e-7
121.20.30119453930.30119421193.274e-71.087e-6
131.30.2725321140.2725317933.209e-71.178e-6
141.40.24659727670.24659696393.127e-71.268e-6
151.50.22313046330.22313016013.032e-71.359e-6
161.60.20189681060.2018965182.926e-71.449e-6
171.70.18268380540.18268352412.813e-71.540e-6
181.80.16529915770.16529888822.695e-71.631e-6
191.90.14956887660.14956861922.574e-71.721e-6
2020.13533552840.13533528322.452e-71.812e-6

Continue

Open in ODE Solver Lab

The problem, the step and the supplied reference continue into the Lab with RK4 enabled. Euler stays off there until you switch it on: this page never asked for it.

Assumptions & validity

  • · The problem is a single scalar equation y′ = f(t, y) with one initial condition. Systems of equations are not solved here.
  • · The step is FIXED: four stages are evaluated per step and the step size never responds to the behaviour of the solution. This is not an adaptive RK45 method and carries no error estimate of its own.
  • · Every node time is t₀ + n·h and the end time is the derived t₀ + N·h.
  • · f must return a finite value at every stage the method visits; a non-finite stage stops the march and is reported with its step and stage.
  • · Fourth-order behaviour assumes the solution is smooth enough on the whole interval. Across a kink, a singularity or a rapidly varying region, the observed order drops and the O(h⁴) claim must not be quoted.
  • · A supplied reference is used for error only when it matches the initial condition at t₀.
  • · RK4 is explicit and has a bounded stability region; it is not a stiff solver and nothing here detects stiffness.

Technical reference

y′ = f(t, y), y(t₀) = y₀

k₁ = f(tₙ, yₙ)

k₂ = f(tₙ + h/2, yₙ + (h/2)·k₁)

k₃ = f(tₙ + h/2, yₙ + (h/2)·k₂)

k₄ = f(tₙ + h, yₙ + h·k₃)

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

Global error at a fixed end time: O(h⁴)

k₁ … k₄
the four stage slopes of one step
h
signed fixed step; negative integrates backward
N
number of steps taken
yₙ
numerical value at node time tₙ

Four derivative evaluations per step: 4N for N steps. Compared at equal COST rather than equal step count, RK4 still beats Euler decisively on any smooth problem.

The weights 1, 2, 2, 1 over 6 are what make the method fourth order; they are not an average of independent estimates and none of the four stages is an answer on its own.

Halving h divides the error at a fixed end time by about sixteen for a smooth problem, which is why a refinement study is the honest way to judge a result.

Classical RK4 produces NO error estimate. An embedded pair such as RK45 does, by comparing two orders on the same stages; that is deliberately out of scope here.

Stability is bounded: for y′ = λy with λ real and negative, RK4 is stable roughly for hλ greater than about −2.78. A large enough step still diverges, more abruptly than Euler.

A very small h is not automatically better: below a certain step the accumulated rounding of many additions starts to dominate the truncation error.

Related tools