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·h | 2 |
|---|---|
| Numerical value y_N | 0.1353355284 |
| Steps N | 20 |
| Step size h | 0.1 |
| Derivative evaluations | 80 (4 per step) |
| Integration direction | forward (h > 0) |
| Supplied reference at t_end | 0.1353352832 |
| Absolute error vs supplied reference | 2.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
Trajectory nodes
| n | tₙ | yₙ | y_ref(tₙ) | |yₙ − y_ref| | relative |
|---|---|---|---|---|---|
| 0 | 0 | 1 | 1 | 0.000e+0 | 0.000e+0 |
| 1 | 0.1 | 0.9048375 | 0.904837418 | 8.196e-8 | 9.058e-8 |
| 2 | 0.2 | 0.8187309014 | 0.8187307531 | 1.483e-7 | 1.812e-7 |
| 3 | 0.3 | 0.740818422 | 0.7408182207 | 2.013e-7 | 2.718e-7 |
| 4 | 0.4 | 0.6703202889 | 0.670320046 | 2.429e-7 | 3.623e-7 |
| 5 | 0.5 | 0.6065309344 | 0.6065306597 | 2.747e-7 | 4.529e-7 |
| 6 | 0.6 | 0.5488119344 | 0.5488116361 | 2.983e-7 | 5.435e-7 |
| 7 | 0.7 | 0.4965856187 | 0.4965853038 | 3.149e-7 | 6.341e-7 |
| 8 | 0.8 | 0.4493292897 | 0.4493289641 | 3.256e-7 | 7.247e-7 |
| 9 | 0.9 | 0.4065699912 | 0.4065696597 | 3.315e-7 | 8.153e-7 |
| 10 | 1 | 0.3678797744 | 0.3678794412 | 3.332e-7 | 9.058e-7 |
| 11 | 1.1 | 0.3328714154 | 0.3328710837 | 3.317e-7 | 9.964e-7 |
| 12 | 1.2 | 0.3011945393 | 0.3011942119 | 3.274e-7 | 1.087e-6 |
| 13 | 1.3 | 0.272532114 | 0.272531793 | 3.209e-7 | 1.178e-6 |
| 14 | 1.4 | 0.2465972767 | 0.2465969639 | 3.127e-7 | 1.268e-6 |
| 15 | 1.5 | 0.2231304633 | 0.2231301601 | 3.032e-7 | 1.359e-6 |
| 16 | 1.6 | 0.2018968106 | 0.201896518 | 2.926e-7 | 1.449e-6 |
| 17 | 1.7 | 0.1826838054 | 0.1826835241 | 2.813e-7 | 1.540e-6 |
| 18 | 1.8 | 0.1652991577 | 0.1652988882 | 2.695e-7 | 1.631e-6 |
| 19 | 1.9 | 0.1495688766 | 0.1495686192 | 2.574e-7 | 1.721e-6 |
| 20 | 2 | 0.1353355284 | 0.1353352832 | 2.452e-7 | 1.812e-6 |
Continue
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.