Calculus, Linear Algebra & Discrete Math Differential Equations Euler and Heun (improved Euler) methods

Euler's Method Calculator (ODE Step Table)

This calculator marches an initial value problem forward one step at a time and shows you the arithmetic. Enter the coefficients of dy/dx = f(x,y), an initial point, a step size h and a number of steps, and you get the full step table — the slope used at each step, the increment it produces, and the running value of y. It also runs a fine-grid fourth-order reference solution so you can see how far your Euler answer actually drifts, and repeats the run at h/2 so you can watch the error shrink.

Calculator

This calculator runs in your browser. Enable JavaScript for live results — the inputs, formula and worked example below remain fully readable without it.

Inputs this calculator takes, with typical values
InputWhat to enterExample
MethodForward Euler uses the slope at the left end of the step; Heun averages the left slope with a predicted right-end slope.Euler (forward)
Constant term in f(x,y)The stand-alone number in dy/dx. For dy/dx = x + y this is 0.0
Coefficient of xMultiplies x on its own. For dy/dx = x + y this is 1.1
Coefficient of yMultiplies y on its own. This coefficient is what drives numerical stability.1
Coefficient of x·ySet this to 1 and everything else to 0 for the separable equation dy/dx = xy.0
Coefficient of x²Use this for quadrature-style problems such as dy/dx = x², where the answer is a definite integral.0
Initial x₀The x value at which you know y — the left end of the march.0
Initial y₀ = y(x₀)The known value of y at x₀, taken from the initial condition.1
Step size hThe x increment per step. Smaller h means less error and more arithmetic.0.1
Number of stepsThe march ends at x₀ + n·h, so pick n to land on the x you actually want.10

It returns

  • y at the final x — The approximation produced by the method and step size you chose.
  • Final x reached
  • Accurate reference value — Fourth-order Runge-Kutta on a grid ten times finer, used as a stand-in for the exact solution.
  • Absolute error vs reference
  • Relative error
  • Same method at half the step size — The identical run with h/2 and twice as many steps, ending at the same x.

The formula

yn+1=yn+hf(xn,yn)
yn+1=yn+h2[f(xn,yn)+f(xn+h,y~)]

In plain text: y(n+1) = y(n) + h · f(x(n), y(n)), x(n+1) = x(n) + h

  • y(n)Approximate solution value at step n (—)
  • x(n)Grid point x₀ + n·h (—)
  • hStep size in x (—)
  • f(x,y)The right-hand side of dy/dx = f(x,y) (—)

Forward Euler is the first term of the Taylor expansion of y about xₙ. Its local truncation error is O(h²) per step and its global error is O(h).

Updated Category Differential Equations Verified against published test cases Reading time 12 min

What Euler's method actually does

Euler's method turns a differential equation into repeated addition. You know the value of y at one point, and the equation dy/dx = f(x,y) tells you the slope of the solution curve at any point you can name. So you stand at (x0,y0), read the slope there, walk along that straight line for a short horizontal distance h, and call where you land the solution at x0+h. Then you read the slope again and repeat.

That is the whole method, and its honesty is what makes it the first numerical scheme every course teaches. You are approximating a curve by a chain of straight segments, so you are always slightly wrong, and the error compounds because every step starts from a point that was already off. Understanding how that error grows is the real content of the topic.

You reach for a numerical march when the equation has no closed-form solution, when the closed form exists but is unpleasant to evaluate, or when f comes from measured data rather than an expression. Nonlinear equations such as dy/dx = x·y, population models, and circuit transients with arbitrary driving signals all land here. If your equation happens to be linear and first order, solve it exactly with the integrating factor calculator instead and use Euler only as a check.

Where the update rule comes from, and what Heun adds

Expand the true solution in a Taylor series about xn: y(xₙ + h) = y(xₙ) + h·y′(xₙ) + (h²/2)·y″(ξ). The first two terms are exactly the Euler update, because y′(xₙ) = f(xₙ, yₙ). Everything you throw away sits in the term. That discarded piece is the local truncation error, and it is proportional to h².

Here is the step that surprises people. To cross a fixed interval of length L you take n = L/h steps, so you accumulate roughly L/h errors of size each. The product is proportional to h. That is why Euler's method is called first order: halve the step size and you halve the final error, not quarter it. Doubling your work buys you one extra binary digit of accuracy, which is a poor exchange rate.

Heun's method — improved Euler, or the explicit trapezoidal rule — fixes the most obvious defect. Forward Euler uses only the slope at the left end of the step, which systematically misses whenever the slope is changing across the step. Heun takes a provisional Euler step to get a predicted right-end value y~, evaluates the slope there too, and steps again using the average of the two slopes. Averaging the endpoints is the trapezoidal rule, so the terms cancel and the global error becomes proportional to h². Halve h now and the error falls by a factor of four, for exactly twice the function evaluations.

Notice the special case: when f depends on x alone, the differential equation is just an integral, Euler collapses to the left Riemann sum and Heun collapses to the trapezoidal rule. Every accuracy statement you already know about those quadrature rules carries straight over.

Worked example: dy/dx = x + y, y(0) = 1, four steps of h = 0.25

This problem has the closed-form solution y = 2eˣ − x − 1, so you can check every figure. At x = 1 the exact value is 2e − 2 = 3.436564.

  1. Step 1. Slope f(0, 1) = 0 + 1 = 1. Increment 0.25 × 1 = 0.25. New point: x = 0.25, y = 1.25.
  2. Step 2. Slope f(0.25, 1.25) = 1.5. Increment 0.25 × 1.5 = 0.375. New point: x = 0.5, y = 1.625.
  3. Step 3. Slope f(0.5, 1.625) = 2.125. Increment 0.53125. New point: x = 0.75, y = 2.15625.
  4. Step 4. Slope f(0.75, 2.15625) = 2.90625. Increment 0.7265625. Final point: x = 1, y = 2.8828125.

The error is 3.436564 − 2.882813 = 0.553751, about 16% low. Euler undershoots here because this solution is convex: every straight segment is drawn below the curve it is chasing.

Now halve the work instead. With h = 0.5 you get y₁ = 1 + 0.5(1) = 1.5 and y₂ = 1.5 + 0.5(2) = 2.5, an error of 0.936564. Compare the two errors: 0.936564 / 0.553751 = 1.69. Halving h cut the error by a factor heading toward 2, exactly as first-order convergence predicts, with the ratio approaching 2 as h shrinks.

Run the same interval with Heun at h = 0.5 and watch the difference. Step 1: left slope f(0,1) = 1, predictor ỹ = 1 + 0.5(1) = 1.5, right slope f(0.5, 1.5) = 2, average 1.5, so y₁ = 1 + 0.5(1.5) = 1.75. Step 2: left slope f(0.5, 1.75) = 2.25, predictor 2.875, right slope f(1, 2.875) = 3.875, average 3.0625, so y₂ = 1.75 + 0.5(3.0625) = 3.28125. The error is 0.155314 — six times smaller than Euler at the same step size, for two slope evaluations per step instead of one.

How to read the result and choose a step size

Treat a single Euler run as unverified. The only cheap, reliable check is to halve h and compare: this calculator does that for you and reports the Same method at half the step size figure. If the two runs agree to the precision you care about, you are converged; if they differ in the second significant figure, your step size is far too coarse. The gap between them is itself a decent error estimate — for a first-order method the remaining error in the coarse run is roughly equal to that gap.

Two failure signatures are worth recognising immediately. Sign flipping — values alternating positive and negative while the true solution decays smoothly — means you have crossed the stability limit. For the linear test problem y′ = λy with λ negative, the Euler update multiplies y by (1 + hλ) each step, so the numbers only decay while |1 + hλ| < 1, that is while h|λ| < 2. At h|λ| = 2 exactly you get a perfect ±1 oscillation. Beyond it the approximation grows without bound while the real solution vanishes. The calculator warns you when the local ∂f/∂y puts you at or past that threshold.

Overflow — a march that runs off to enormous values — means either the same instability or a genuine vertical asymptote in the solution. dy/dx = y² style blow-ups are real, and no step size rescues you past the asymptote; you have to stop the interval before it.

For a well-behaved non-stiff problem, a reasonable working rule is to pick h so that the solution changes by no more than a few percent per step, then halve it once and confirm the answer barely moves. If halving h changes your answer by less than your tolerance, stop.

Error at x = 1 for dy/dx = y, y(0) = 1, as the step size shrinks

Euler gives exactly (1 + h)^(1/h) and Heun gives exactly (1 + h + h²/2)^(1/h) for this problem, so every row is reproducible with a pocket calculator. The exact answer is e = 2.718282.
Step size hStepsEuler valueEuler errorHeun valueHeun error
112.0000000.7182822.5000000.218282
0.522.2500000.4682822.6406250.077657
0.2542.4414060.2768762.6948560.023426
0.12582.5657850.1524972.7118410.006441
0.1102.5937420.1245392.7140810.004201

Read the two error columns downward. Euler's error falls by roughly a factor of 2 each time h is halved (0.468 → 0.277 → 0.152); Heun's falls by roughly a factor of 4 (0.0777 → 0.0234 → 0.0064). That contrast is what first order versus second order means in practice.

Stability and accuracy are different problems

A step size can be accurate enough and still unstable, and the symptoms look nothing alike. Accuracy error drifts smoothly away from the truth; instability explodes or oscillates. Stiff equations — ones containing both a very fast and a very slow timescale — force explicit Euler to use a step size set by the fastest mode even when you only care about the slow one, which is why stiff problems demand implicit methods such as backward Euler or a BDF solver rather than a smaller explicit step.

Mistakes that make an Euler answer wrong

  • Counting steps instead of grid points. Marching from x₀ to x₀ + n·h takes n steps and produces n + 1 points. Ask for 10 steps from 0 with h = 0.1 and you land on x = 1, not x = 0.9.
  • Updating y before evaluating the slope. The slope in step n must be evaluated at the old pair (xₙ, yₙ). Overwriting y first is the single most common coding error in this method.
  • Using the predicted value as the answer in Heun. The predictor ỹ is scaffolding, not the result. The step value is y plus h times the averaged slope.
  • Reporting more digits than you have. An Euler run with h = 0.1 rarely deserves more than two significant figures. Quote what the halved-step comparison supports.
  • Assuming small h always helps. Below roughly 10⁻⁸ in double precision, rounding error in the repeated additions starts to grow faster than truncation error falls, so the total error stops improving.
  • Ignoring an asymptote. If the true solution blows up inside your interval, no step size gives a meaningful answer past that point. Check the equation for the possibility before trusting the table.

Where Euler sits among the alternatives

Euler's method is the first member of the Runge-Kutta family and is essentially never the right choice for production work. Its value is pedagogical and diagnostic: it is transparent enough that you can verify a single step by hand, which makes it the right tool for checking that you have coded f correctly before switching to something better.

Move up in this order. Heun doubles the work per step and squares the accuracy benefit — take it whenever Euler is not enough. Classical RK4 costs four slope evaluations per step and is fourth order, so on any smooth problem it delivers far more accuracy per unit of work than either; the RK4 calculator shows the same march with k₁ through k₄ printed for every step. Beyond that, adaptive schemes such as Runge-Kutta-Fehlberg estimate the local error at each step and adjust h automatically, and implicit solvers handle stiffness.

Also check whether you need a numerical method at all. Constant-coefficient linear equations have exact closed-form solutions: use the characteristic equation calculator for second-order homogeneous problems, or the Laplace transform calculator when there is a forcing function and initial conditions to carry. An exact answer beats any grid.

Higher-order equations are not an obstacle. Any n-th order ODE becomes a system of n first-order equations by setting u₁ = y, u₂ = y′ and so on, and Euler applies component by component with the same step size. The step table simply gains a column per component.

Key terms

Initial value problem (IVP)
A differential equation together with the value of the solution at one point. The initial condition is what makes the solution unique, and it is where the march starts.
Local truncation error
The error introduced by one single step, assuming the starting point was exact. It is O(h²) for Euler and O(h³) for Heun.
Global error
The accumulated error at the end of the interval. One power of h lower than the local error, because you take O(1/h) steps.
Explicit method
One where the new value is written directly in terms of already-known quantities. Euler and Heun are explicit; backward Euler is implicit and requires solving an equation at each step.
Stiffness
A property of equations containing widely separated timescales, which forces an explicit method to take much smaller steps than accuracy alone would require.
Order of a method
The exponent p in a global error proportional to hᵖ. Euler is order 1, Heun is order 2, classical Runge-Kutta is order 4.

Frequently asked questions

How do I choose the step size h?

Pick a step that changes the solution by only a few percent, then halve it and check the answer barely moves. This calculator runs the halved-step version for you automatically. If the two results agree to the precision you need, stop; if they differ in the second significant figure, keep halving. There is no universal value — the right h depends entirely on how fast your solution turns.

Why is my Euler answer always too low?

Because the solution curve is convex over your interval. Euler follows the tangent line at the left of each step, and a tangent to a convex curve lies below it, so every step lands short. On a concave solution the same argument runs the other way and Euler overshoots. That systematic direction is a feature of using left-end slopes, and it is exactly what Heun's averaged slope removes.

What is the difference between Euler and improved Euler?

Improved Euler, also called Heun's method, evaluates the slope twice per step: once at the current point, once at a predicted end point, then steps using the average of the two. That averaging cancels the leading error term, so the global error goes from proportional to h down to proportional to h². The cost is one extra function evaluation per step, which is almost always worth paying.

How accurate is Euler's method with h = 0.1?

For dy/dx = y from y(0) = 1, ten steps of h = 0.1 give 2.593742 against the true value e = 2.718282 — an error of 0.124539, about 4.6%. That is typical: a few percent over a unit interval for a smoothly growing solution. Heun on the same grid gives 2.714081, an error of 0.004201. Neither figure transfers to a different equation, so always run the halved-step check on your own problem.

Why do my values oscillate between positive and negative?

Your step size has crossed the stability limit. For dy/dx = λy with λ negative, Euler multiplies y by (1 + hλ) each step, so you need h·|λ| below 2 to get decay. At exactly 2 the multiplier is −1 and the values flip sign forever; above it they flip and grow. Reduce h below 2/|λ|. This calculator raises a warning when ∂f/∂y at the initial point puts you at or past that threshold.

Can Euler's method handle a second-order equation?

Yes, after you convert it to a system. Write u₁ = y and u₂ = y′; then y″ = f(x, y, y′) becomes the pair u₁′ = u₂ and u₂′ = f(x, u₁, u₂), and you apply the same update rule to both components using the same h. This calculator handles the single-equation case; for a constant-coefficient second-order problem, the exact characteristic-root solution is usually the better route.

What does the reference value in the results mean?

It is a fourth-order Runge-Kutta solution computed on a grid ten times finer than yours, used as a stand-in for the exact answer. On a smooth problem RK4 at h/10 is typically many orders of magnitude more accurate than Euler at h, so the difference between them is, to good approximation, your Euler error. It is not the analytic solution, and on a badly behaved or near-singular problem it should be treated with the same caution as any numerical result.

Does making h smaller always improve the answer?

Only down to a point. Truncation error falls as you shrink h, but the number of floating-point additions rises, and each carries a rounding error. Below roughly 10⁻⁸ in double precision the accumulated rounding starts to dominate and the total error stops improving, then slowly worsens. Long before that, the runtime becomes the binding constraint — which is the practical argument for using a higher-order method instead of a smaller step.

Why does the calculator ask for coefficients instead of an equation?

So that the arithmetic is unambiguous and every result is reproducible. The form a + b·x + c·y + d·x·y + e·x² covers the equations set in a first course — dy/dx = y, x + y, xy, x², 2x − 3y + 1 and so on — without any expression parsing to go wrong. Set the coefficients you need and leave the rest at zero.

References

  • Numerical Analysis, 10th ed. (Chapter 5, Initial-Value Problems for Ordinary Differential Equations) — Burden & Faires, Cengage Learning
  • Elementary Differential Equations and Boundary Value Problems, 11th ed. (Chapter 8, Numerical Methods) — Boyce & DiPrima, Wiley
  • NIST Digital Library of Mathematical Functions, §3.7 Ordinary Differential EquationsNational Institute of Standards and Technology
  • Numerical Recipes: The Art of Scientific Computing, 3rd ed. (Chapter 17) — Press, Teukolsky, Vetterling & Flannery, Cambridge University Press