Formula: Runge-Kutta (RK4) vs. Euler Formula
- initialValueY0: Initial Value Y0 (y₀)
- growthRateCoeffR: Growth Rate Coeff R (r)
- stepSizeH: Step Size H (h)
- numberOfStepsN: Number Of Steps N (steps)
Compare 4th-order Runge-Kutta (RK4) and forward Euler numerical ODE integration against the exact analytical solution.
Euler uses one slope from the start of each step, so it matches the true solution only to first order. RK4 combines four slopes taken at the start, two midpoints and the end, which on this equation reproduces the first five terms of the exponential series. On the default run that turns an Euler error of 0.1245 into an RK4 error of 0.000002.
Per step, it evaluates the slope four times instead of once, so the work is roughly four times higher. But it is so much more accurate that for a target error it needs far fewer steps. On the default problem, Euler would need hundreds of thousands of steps to match what RK4 achieves in 10, so RK4 wins comfortably.
It is the absolute difference between the Euler result and the exact value y₀·e^(rx) at the final x. It includes the build up of small errors from every step, which is why it is called global. The RK4 error is not shown as its own line, but the interpretation sentence gives it to six decimal places.
Watch the product r·h. Keeping it around 0.1, as the defaults do, gives RK4 errors in the sixth decimal place on this equation. If you double r, halve h. For decay problems with a large negative r, a step that is too large can make Euler unstable, so reduce h until the result stops changing much.
With r = −5 and h = 0.5, Euler multiplies y by −1.5 each step. That flips the sign and grows the magnitude, the opposite of decay. Four steps give 5.0625. It is the classic instability of forward Euler when the step is too large for the decay rate, and a smaller h fixes it.
Not directly. The calculator always starts at x = 0 with the y₀ you enter. Because this equation does not depend on x itself, a start at another point gives the same shape, so you can treat the reported x as a distance travelled from your own starting point.
Only if it has the form dy/dx = r·y. The calculator has no box for a constant term or any other function of x and y. It is meant to show how the two methods behave against a known exact answer. For other equations, the same RK4 and Euler update rules apply, but you would need to compute the slopes yourself.
The headline and supporting values round to three decimals, and on the default run both are 2.718. The difference is in the sixth decimal place. The interpretation sentence shows more digits, with RK4 at 2.71828 and an error of 0.000002. For coarse steps such as h = 1, the gap becomes visible.
No. For this linear equation, scaling y₀ scales every result and every error by the same factor. The 0.6 and 1.5 presets show Euler errors of 0.0747 and 0.1868, both about 4.6% of the answer, the same as the default. Accuracy depends on r·h, not on y₀.
It describes how the global error falls as h shrinks. For a fourth order method, halving h cuts the error by about 2⁴, or 16. On this page, RK4's error is 0.000031 at h = 0.4 and 0.000002 at h = 0.2, close to that ratio. Euler, a first order method, only halves its error when h is halved.
Match its inputs to your problem, set the number of steps to however many you worked by hand, and compare values. One step is the easiest to check, since the result should be 1.1 for Euler and about 1.105171 for RK4 with the defaults. If yours differ, recheck which y value each RK4 stage uses and the 1, 2, 2, 1 weights.
It is simpler to write and each step is cheaper, which is why it is taught first and still used for quick sketches or when a step must be very small anyway. On this page, though, it is never more accurate than RK4 for the same step size. Its weakness on stiff decay, shown by the r = −5 case, is another reason it is rarely used alone in practice.
Every run of this calculator solves the same equation, dy/dx = r·y, starting at x = 0 with y equal to the initial value you enter. That is exponential growth or decay, and it has a known exact answer, y₀·e^(rx). The calculator steps forward with RK4 and with forward Euler and puts both next to the exact value, so you can see the error directly.
The defaults are y₀ = 1, r = 0.5, a step size h of 0.2 and 10 steps. Ten steps of 0.2 reach x = 2, so the exact answer is e^(0.5 × 2), which is e itself, 2.71828.
RK4 lands on 2.71828 with an error of 0.000002. Forward Euler gives 2.593742, an error of 0.1245. Same equation, same steps, and one method is about sixty thousand times closer than the other.
Because the problem is linear with a clean exact solution, it is the standard classroom case for comparing methods. It is also forgiving: real equations are rarely this smooth, and the gap between methods can be smaller or larger there.
Set the number of steps to 1 and the calculator stops at x = 0.2. It is worth doing that step on paper once.
Euler uses only the slope at the start. With y = 1 and r = 0.5, the slope is 0.5, so the new value is 1 + 0.2 × 0.5 = 1.1. That is exactly what the calculator reports.
RK4 samples the slope four times. The first slope, k₁, is 0.5 × 1 = 0.5. The second uses a half step along k₁: y = 1 + 0.1 × 0.5 = 1.05, so k₂ = 0.525. The third uses a half step along k₂: y = 1.0525, so k₃ = 0.52625. The fourth uses a full step along k₃: y = 1.10525, so k₄ = 0.552625. The weighted average (k₁ + 2k₂ + 2k₃ + k₄) / 6 is about 0.52585, and 1 + 0.2 × 0.52585 gives 1.1051708.
The exact value is e^0.1, which is 1.1051709. RK4 is off in the seventh decimal place after one step. Euler is off by about 0.0052, which matches the calculator's one step result of 1.105171 for RK4 and 1.1 for Euler.
If you are checking homework, this is the most useful way to use the page. Work one step by hand, set N to 1 with the same y₀, r and h, and compare. A mismatch usually means a half step was taken with the wrong slope, most often using k₁ where k₂ belongs in the third stage, or forgetting that the weights in the average are 1, 2, 2 and 1 and the sum is divided by 6.
For this equation, each Euler step multiplies y by the same factor, 1 + r·h. With the defaults that is 1.1. Ten steps multiply by 1.1 ten times, and 1.1 to the tenth power is 2.593742. The calculator's Euler value is exactly that.
RK4 also multiplies by a fixed factor each step. For dy/dx = r·y, the four stage average works out to 1 + z + z²/2 + z³/6 + z⁴/24, where z = r·h. That is the first five terms of the series for e^z. With z = 0.1, the missing terms start at about 0.0000001, which is why RK4 is so close.
Seen this way, the difference between the methods is simply how many terms of the exponential series they keep. Euler keeps two. RK4 keeps five. Each additional term matters less when z is small, which is the case here.
It also explains where both methods go wrong. When z = r·h is large, the series terms that RK4 drops are no longer tiny, and the method begins to lose accuracy too. The sections below show what happens when r or h grows.
The claim behind the two methods is that Euler's global error shrinks in proportion to h and RK4's in proportion to h⁴. This calculator lets you check it in a few runs, always ending at x = 2.
For Euler, the default h = 0.2 gives an error of 0.1245. At h = 0.1 with 20 steps the error drops to 0.065. At h = 0.05 with 40 steps it is 0.0332. Each halving of h roughly halves the error. That is first order.
For RK4, the default error is 0.000002. At h = 0.4 with 5 steps it is 0.000031, about sixteen times larger for twice the step size, which is the 2⁴ that fourth order predicts. With h = 1 and only 2 steps, RK4 still gives 2.717346, an error of 0.000936, while Euler collapses to 2.25.
The practical lesson is about effort. Euler's error only halves when the step count doubles, so matching RK4's default accuracy with Euler would take hundreds of thousands of steps. RK4 does four slope evaluations per step instead of one, but it earns that back many times over.
At h = 0.1 the interpretation line reports RK4's error as 0, because it rounds to six decimal places.
The presets change only y₀. With y₀ = 0.6, RK4 gives 1.630968 and Euler 1.556245, an error of 0.0747. With y₀ = 1.5, RK4 gives 4.07742 and Euler 3.890614, an error of 0.1868.
Those numbers are just the default results multiplied by 0.6 and 1.5. The equation is linear, so scaling the starting value scales the whole solution, the Euler result and the error by the same factor. The relative error is unchanged at about 4.6% for Euler in all three cases.
So these presets are not testing anything new about the methods. They are useful as a check that you understand linearity, and that an error figure on its own means little without the size of the answer next to it.
To see the methods behave differently, change r or h instead. Those change z = r·h, which is the quantity that actually controls the accuracy of both.
A negative r gives exponential decay. With r = −0.5 and the default step, the exact answer at x = 2 is e^−1, 0.36788. RK4 matches it and Euler gives 0.348678, an error of 0.0192. Both behave sensibly.
Now make the decay fast and the step coarse. With r = −5, h = 0.5 and 4 steps, the exact value at x = 2 is about 0.00005, essentially zero. Euler's factor per step is 1 + (−5)(0.5) = −1.5. Four steps give (−1.5)⁴ = 5.0625. The solution should die away, but Euler makes it flip sign and grow. That is numerical instability, not a bug in the calculator.
RK4 does better but is not right either. Its factor at z = −2.5 is about 0.648, so four steps give 0.176796, against a true value near zero.
This is what makes stiff problems hard. A fast decay rate forces a small step for stability, even when the solution itself is boring. Try h = 0.1 with 20 steps at r = −5 and both methods settle down: RK4 gives 0.000046 and Euler 0.000001, so both now decay toward zero as they should. Euler's value is still far from the true 0.000045 in relative terms, a reminder that stability and accuracy are separate questions. Explicit methods like these two simply need z to stay small enough.
Growth has its own problem. With r = 2 and the default h = 0.2 and 10 steps, the exact answer at x = 2 is e⁴, about 54.598. RK4 gives 54.565, an error of about 0.033. Euler gives 28.926, barely more than half the true value.
Here z = r·h is 0.4, four times the default. RK4's error is now visible in the third decimal place, and Euler's factor of 1.4 per step falls well short of e^0.4, which is about 1.49. Compounded over ten steps, that shortfall almost halves the result.
The guide for choosing h is the same in both directions: what matters is the product r·h, not h by itself. If you double r, halve h to keep the same accuracy.
This also shows why absolute error alone can mislead. An RK4 error of 0.033 sounds larger than the default 0.000002, but relative to an answer of 54.6 it is still well under a tenth of a percent.
The headline and supporting lines round to three decimals, so on the default run RK4 and the exact solution both show 2.718 and look identical. Read the interpretation sentence for six decimal places and the actual RK4 error. Even that rounds tiny errors to 0, as at h = 0.1.
The page's description mentions dy/dx = r·y + b, but there is no b input. The calculator only solves r·y. If you need a constant term, the exact solution changes and you would have to compute it yourself.
The number of steps must be a whole number from 1 up to two hundred, and h must be above zero; anything else returns an error rather than a result. The end point is always N × h, measured from x = 0, so to reach a particular x you choose the pair of values that multiply to it.
The unit labels such as y(x) and |err| are just tags. The quantities are dimensionless numbers on this test equation.