How to use the differential equation solver
- Type the slope y' = f(x, y) using x and y, operators + - * / ^, parentheses and functions such as sin, cos, exp, ln and sqrt (angles in radians).
- Enter the starting x and starting y, the ending x and the number of steps. The step size is the distance from start to end divided by the number of steps.
- If you know the exact solution, type it as a function of x. The page then measures each method's error against it. If not, errors are measured against Runge-Kutta 4 with ten times as many steps.
- Read the comparison table of the four methods, then the steps of the method chosen under Advanced options.
Formula
Euler: y_next = y + h f(x, y)
Midpoint: y_next = y + h f(x + h/2, y + (h/2) f(x, y))
Heun: y_next = y + (h/2) (f(x, y) + f(x + h, y + h f(x, y)))
Runge-Kutta 4: y_next = y + (h/6) (k1 + 2 k2 + 2 k3 + k4)
Each method moves from one point to the next using slopes. Euler uses one slope, midpoint and Heun use two, and Runge-Kutta 4 blends four. A method of order p has an error that falls by about 2^p when the step is halved.
y' = x + y with y(0) = 1, from 0 to 1 in 10 steps
The exact solution is y = 2e^x - x - 1, so y(1) = 2e - 2 = 3.436563657. Euler gives 3.18748492, an error of 0.2491. Midpoint and Heun both give 3.428161693, an error of 0.0084. Runge-Kutta 4 gives 3.436559488, off by only 4.2e-6.
Halving the step to 20 would cut the Euler error roughly in half and the Runge-Kutta 4 error by a factor of about sixteen. With y' = y from 0 to 1 in 5 steps, Runge-Kutta 4 gives 2.7182511, close to e = 2.7182818.
Choosing a method and a step
Euler is the simplest and the least accurate, and it is mainly useful for seeing how stepping works. Runge-Kutta 4 gives far more accuracy for the same number of steps and is the usual choice for smooth problems. Smaller steps always help, until the number of steps becomes the limit; this page allows up to 2,000.
The error column is the difference at the end point only. The path can be less accurate in the middle, so look at the table of steps when you have the exact solution to compare against.
When the method fails
Some equations grow without bound at a finite x, like y' = y squared from y(0) = 1, which blows up at x = 1. The page stops and tells you when a value stops being finite. Stiff equations, with very different time scales, need much smaller steps or other methods. For finding where a curve crosses a value use the Root Finder, for areas under a curve the Numerical Integration Calculator, and for plotting the Graphing Calculator.
More math tools
For significant figures, complex numbers, polynomials, repeating decimals and shortest paths, try the Shortest Path Calculator, Significant Figures Calculator and Complex Number Calculator.
Assumptions and limits
- The equation is a first-order equation y' = f(x, y) with one starting value.
- The step size is constant, equal to the span divided by the number of steps.
- Trigonometric functions use radians.
- Without an exact solution, the reference is Runge-Kutta 4 with ten times as many steps, which is accurate but not exact.
- The example equation is an illustration. Replace it with your own.
Frequently asked questions
Why do Midpoint and Heun give the same number in the example?
For a slope that is linear in y, as in y' = x + y, the two second-order methods turn out to take identical steps. For other equations they differ slightly.
How many steps should I use?
Start with 10 or 20 and double until the answer stops changing in the digits you need. For smooth problems Runge-Kutta 4 converges quickly.
Can I solve a second-order equation?
Not directly. Rewrite it as two first-order equations, which this page does not handle. It solves one equation for one unknown function.
Why is the error sometimes tiny but the table looks wrong?
The error is shown at the final point. If the exact solution is off or the equation has a sharp feature in the middle, the end can still look fine. Compare the steps column with the exact values.