Differential Equation
solver.
Follow an equation for y′ forward from one starting point, a small step at a time.
- Euler
- 4.8658
- Heun
- 5.2331
- Known solution
- 5.3055
With a step of 0.2, RK4's error is about 4,036 times smaller than Euler's, for four slopes per step against one.
Step size and known solution
Three methods on one graph
Drag the end point to solve further. Tap a method in the legend to hide it.
y′ = y − x² + 1 from x = 0 to x = 2 with 10 steps. At the end RK4 gives 5.3054, Heun 5.2331 and Euler 4.8658; the known solution is 5.3055.
Working
- 1Divide the interval into equal stepsh = 2 − 010 = 0.2
- 2Euler, first step: follow the slope at the starty₁ = y₀ + h k₁ = 0.5 + 0.2 × 1.5 = 0.8
- 3Heun, first step: average the slopes at both endsy₁ = y₀ + h2(slope at start + slope at Euler’s end) = 0.826
- 4RK4, first step: four slopes, weightedk₁ = 1.5, k₂ = 1.64, k₃ = 1.654, k₄ = 1.7908; y₁ = y₀ + h6(k₁ + 2k₂ + 2k₃ + k₄) = 0.829293
- 5After 10 steps, at x = 2Euler 4.865785, Heun 5.233055, RK4 5.305363, known solution 5.305472
- 6Check: the slope of the RK4 values against the equationAt x = 1 the values change at 2.640881 and the equation gives 2.640823, a gap of 5.81 × 10⁻⁵
Solution at each step
| Step | RK4 | Heun | Euler | Known | |
|---|---|---|---|---|---|
| 0Step 0 | 0 | 0.5Heun 0.5Euler 0.5 | 0.5 | 0.5 | 0.5 |
| 0.2Step 1 | 1 | 0.829293Heun 0.826Euler 0.8 | 0.826 | 0.8 | 0.829299 |
| 0.4Step 2 | 2 | 1.214076Heun 1.20692Euler 1.152 | 1.20692 | 1.152 | 1.214088 |
| 0.6Step 3 | 3 | 1.648922Heun 1.637242Euler 1.5504 | 1.637242 | 1.5504 | 1.648941 |
| 0.8Step 4 | 4 | 2.127203Heun 2.110236Euler 1.98848 | 2.110236 | 1.98848 | 2.12723 |
| 1Step 5 | 5 | 2.640823Heun 2.617688Euler 2.458176 | 2.617688 | 2.458176 | 2.640859 |
| 1.2Step 6 | 6 | 3.179894Heun 3.149579Euler 2.949811 | 3.149579 | 2.949811 | 3.179942 |
| 1.4Step 7 | 7 | 3.73234Heun 3.693686Euler 3.451773 | 3.693686 | 3.451773 | 3.7324 |
| 1.6Step 8 | 8 | 4.283409Heun 4.235097Euler 3.950128 | 4.235097 | 3.950128 | 4.283484 |
| 1.8Step 9 | 9 | 4.815086Heun 4.755619Euler 4.428154 | 4.755619 | 4.428154 | 4.815176 |
| 2Step 10 | 10 | 5.305363Heun 5.233055Euler 4.865785 | 5.233055 | 4.865785 | 5.305472 |
Halve the step, and see each method’s order
Final value as the step is halved
| Method | 10 steps | 20 steps | 40 steps | Order seen |
|---|---|---|---|---|
| Euler | 4.865785 | 5.0635 | 5.17800610 steps 4.86578520 steps 5.0635 | 0.79 |
| Heun | 5.233055 | 5.286567 | 5.30065210 steps 5.23305520 steps 5.286567 | 1.93 |
| Runge–Kutta 4 | 5.305363 | 5.305465 | 5.30547210 steps 5.30536320 steps 5.305465 | 3.96 |
Each halving shrinks the change in the answer by about 2 to the power of the method’s order, so expect about 1, 2 and 4 on a smooth equation.
How it's worked out
A differential equation tells you the slope of y at any point. Most have no formula for y, so you step forward instead. Euler's method reads the slope where you are and follows it for one step of size h. Here n counts the steps.
Heun's method, also called improved Euler, takes that Euler step as a trial. It reads the slope at the far end too, and moves along the average of the two.
In that line, k₁ is the slope at the start and k₂ the slope at the end of the trial step. RK4, the fourth-order Runge–Kutta method, reads four slopes: one at the start, two at the midpoint and one at the end. The midpoint slopes count double.
Why three methods. Euler's total error shrinks in step with h, Heun's with h squared and RK4's with h to the fourth. Halve the step, and RK4's error drops about 16 times. The tool reruns your equation with two and four times the steps and reports the order it sees.
Second-order equations become two first-order ones. Write v for y′, so that y′ = v, and v′ is your right side. The same formulas then run on both at once. The result is y; the slopes in the table are those of v.
Worked examples
Priya checks the textbook test problem
Priya's numerical methods class uses this equation because its true solution is known. With 10 steps of 0.2 up to , the true value is 5.305472. RK4 gives 5.305363, Heun 5.233055 and Euler 4.865785.
So the errors are 0.4397 for Euler, 0.0724 for Heun and 1.1 × 10⁻⁴ for RK4. RK4 reads four slopes per step against Euler's one, and its error is about 4,036 times smaller, so the extra work pays well.
On this equation, the step-halving table shows an order of 3.96 for RK4 and 0.79 for Euler. Euler sits below 1 here because 10 steps is still too coarse for it to settle.
Marco tracks a cooling cup of coffee
Marco's coffee starts at 90 °C in a 20 °C room. It loses heat at a rate of 0.1 per minute times its lead over the room, which is Newton's law of cooling. The rate 0.1 is a figure for this example. A real cup has its own, so he would fit it to two readings.
With one-minute steps, RK4 says the cup reaches 23.49 °C after 30 minutes. Euler says 22.97 °C. Euler runs cold here, because it holds the steep early slope for a whole minute and so overshoots each drop.
A spring with no damping
A mass on an ideal spring obeys . Let it go from at rest. The true solution is cos x, so after one full swing it should be back at 1.
With 40 steps, RK4 ends within 4.2 × 10⁻⁶ of that. Euler ends at 1.626, higher than the mass started. It gains a little energy on every step, so the swing keeps growing. That is a fault of the method and not of the spring.
Reading your result
The answer is RK4's value at the end point, since it is usually the most accurate of the three on a smooth equation. It is still an estimate: no method here gives you a formula.
The known solution is optional. If you have a formula for y, enter it to get each method's true error. Without one, the step-halving table is your guide. Once a doubling of the steps no longer changes the digits you care about, the step is small enough.
How the step changes the error. For with , the true value at is e. Each row solves the same problem with a smaller step, and you can see the orders at work. Halving h about halves Euler's error, quarters Heun's and cuts RK4's about 16 times.
| h | Euler | Heun | RK4 |
|---|---|---|---|
| 0.5 | 0.4683 | 0.0777 | 9.4 × 10⁻⁴ |
| 0.25 | 0.2769 | 0.0234 | 7.2 × 10⁻⁵ |
| 0.1 | 0.1245 | 0.0042 | 2.1 × 10⁻⁶ |
| 0.05 | 0.065 | 0.0011 | 1.4 × 10⁻⁷ |
Stiff equations. On , each Euler step multiplies y by . With that factor is 0.5, and Euler ends at 9.1 × 10⁻¹³, near the true 2.1 × 10⁻⁹. With the factor is −1.5, so y flips sign and grows, reaching 25.6. Equations with such fast parts are called stiff, and larger solvers switch to methods built for them.
Common mistakes
Keep the limits in mind too. This solver handles one first- or second-order equation at a time, from starting values, with a fixed step. Larger systems and problems with a condition at each end need other tools.
- Reading the steps as accuracy. A large step count does not help once rounding takes over, and a small count can look fine on a slow part of the curve. Judge by whether the answer settles, not by the count.
- Solving past a blow-up. The solution of with is , which runs off to infinity at . The tool stops and says where the values broke down, because nothing past that point means anything.
- Forgetting the second start value. A second-order equation needs both y and y′ at the start. Change only y, and you get a different motion.
- Typing y′ in the second-order box. Write v for y′, so a damped spring is . If your equation uses t, write x instead.