MathsCalculus

Differential Equation
solver.

Follow an equation for y′ forward from one starting point, a small step at a time.

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.

yn+1\displaystyle y_{n+1}=yn+hf(xn,yn)\displaystyle {}= y_n + h\,f(x_n, y_n)

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.

yn+1\displaystyle y_{n+1}=yn+h2(k1+k2)\displaystyle {}= y_n + \frac{h}{2}\left(k_1 + k_2\right)

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.

yn+1\displaystyle y_{n+1}=yn+h6(k1+2k2+2k3+k4)\displaystyle {}= y_n + \frac{h}{6}\left(k_1 + 2k_2 + 2k_3 + k_4\right)

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 x=2x = 2, 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.

Open this example in the tool

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.

y′\displaystyle y'=−0.1(y−20)\displaystyle {}= -0.1\left(y - 20\right)

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.

Open this example in the tool

A spring with no damping

A mass on an ideal spring obeys y′′=−yy'' = -y. Let it go from y=1y = 1 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.

Open this example in the tool

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 y′=yy' = y with y(0)=1y(0) = 1, the true value at x=1x = 1 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.

Error at x=1x = 1 for y′=yy' = y
hEulerHeunRK4
0.50.46830.07779.4 × 10⁻⁴
0.250.27690.02347.2 × 10⁻⁵
0.10.12450.00422.1 × 10⁻⁶
0.050.0650.00111.4 × 10⁻⁷

Stiff equations. On y′=−10yy' = -10y, each Euler step multiplies y by 1−10h1 - 10h. With h=0.05h = 0.05 that factor is 0.5, and Euler ends at 9.1 × 10⁻¹³, near the true 2.1 × 10⁻⁹. With h=0.25h = 0.25 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 y′=y2y' = y^2 with y(0)=1y(0) = 1 is 11−x\frac{1}{1 - x}, which runs off to infinity at x=1x = 1. 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 −2v−y-2v - y. If your equation uses t, write x instead.

Questions people ask

If the solution is smooth and changes slowly, every method is already close, so better ones have little left to fix. Try a larger step or a harder equation to see them separate. Check also that your code uses the new slopes at each stage, since a repeated slope makes the methods collapse into one.

Sources