Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 47 ยท Ordinary Differential Equations

Euler's Method

Learn Euler's method for ordinary differential equations: the algorithm, why its global error is first order, how to test it, and when a large step makes it unstable.

Euler's method is the simplest way to solve an ODE numerically, and it is the idea behind every other method in this chapter. Stand at a point, look at the slope, walk a short distance along it, and repeat. It is easy to code in five lines, easy to understand, and not very accurate, which makes it perfect for learning what accuracy and stability mean.

The exact exponential curve and the Euler polyline with four steps of size 0.5, with the gap at t equal to 2
Euler for dy/dt = y with h = 0.5: the polyline always falls below the exact curve, and the gap grows.

What you will learn

  • The Euler update and how to code it for a system of equations.
  • Local and global error, and why Euler is first order.
  • How to test a solver against a known solution.
  • The stability limit for decay problems, and why Euler fails for oscillators.

Key terms

Local truncation error: the error made in one step, assuming the starting value is exact.

Global error: the accumulated error at the final time.

Order: Euler's global error is O(h); halving h halves the error.

Stability: a method is stable for a problem and step size if errors do not grow without bound.

The method

Take dy/dt = f(t, y) with y(tโ‚€) = yโ‚€. Replace the derivative by a forward difference over one step:

(y_{n+1} โˆ’ y_n) / h โ‰ˆ f(t_n, y_n)

Solve for the new value:

y_{n+1} = y_n + h f(t_n, y_n), t_{n+1} = t_n + h

That is all. The same formula works for a vector y, such as (x, v), by applying it to each component.

In the picture, the pink line segment from each dot has the slope of the ODE at that dot. For dy/dt = y the slope equals the height, so each step multiplies y by 1 + h = 1.5. After four steps you get 1.5โด = 5.0625, while the true value eยฒ = 7.389 is much larger. The curve is convex, so the tangent always undershoots.

Where the error comes from

Expand the exact solution: y(t+h) = y(t) + h yโ€ฒ(t) + hยฒ yโ€ณ(t)/2 + โ€ฆ. Euler keeps only the first two terms, so each step has local error about hยฒ yโ€ณ/2, which is O(hยฒ).

To reach a fixed final time T you need T/h steps. If each step contributes an error of order hยฒ, the total is about (T/h) ร— hยฒ = T h. The global error is first order: O(h). Halve the step, double the work, halve the error. That is a poor deal compared with the methods in the next lesson.

A reusable implementation

The function below works for any system. The right-hand side f(t, y) takes a time and a NumPy array and returns an array of slopes.

Python Euler's method and its error

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The last column, error / h, stays almost constant. That is the signature of a first-order method: error proportional to the step. You should check this for every solver you write.

Stability: when a bigger step is worse than useless

Consider decay dy/dt = โˆ’ฮป y. Euler gives y_{n+1} = (1 โˆ’ ฮป h) y_n. Each step multiplies the value by the factor 1 โˆ’ ฮป h.

  • If 0 < ฮปh < 1 the factor is between 0 and 1: smooth decay, as it should.
  • If 1 < ฮปh < 2 the factor is between โˆ’1 and 0: the numbers flip sign each step but still shrink. The shape is wrong but the error dies out.
  • If ฮปh > 2 the magnitude exceeds 1: the numerical solution explodes, even though the true solution decays.

Stability demands h < 2/ฮป, and for good accuracy h must be much smaller. Try it below with ฮป = 2 (so the stability limit is h = 1). Drag the slider and watch the pink points leave the true curve.

Drag the step size and watch Euler break

Euler versus oscillators

For the frictionless oscillator xโ€ฒ = v, vโ€ฒ = โˆ’ฯ‰ยฒ x, one Euler step multiplies the energy by 1 + ฯ‰ยฒ hยฒ. The energy grows every step, no matter how small h is. After N = T/h steps the energy has been multiplied by about e^{ฯ‰ยฒ h T}, so a smaller step slows the growth but never stops it. Euler is not just inaccurate for oscillators; it adds energy systematically.

The fix is not a smaller step but a better method. The next lessons introduce Runge-Kutta methods for accuracy and symplectic methods for long-term energy behavior.

Python Convergence order of Euler (log-log)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Common misconceptions

"Euler works if I make h small enough." It converges as h โ†’ 0, but the cost grows quickly. For long oscillator runs the energy still drifts upward.

"Instability means a bug." Instability is a property of the method and the step size. The code can be perfectly correct and still explode when h exceeds the stability limit.

"The local error is h, so the global error is h." The local error is hยฒ; adding about 1/h of them gives a global error of h.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is Euler's update for dy/dt = f(t, y)?

    Move along the slope f(t, y) for a time h.
  2. 2. You halve h in Euler's method. The global error changes by about

    Euler's method is first order: error proportional to h.
  3. 3. For dy/dt = -3y, what is the largest step for which Euler is stable?

    The factor is 1 - 3h and must satisfy |1 - 3h| < 1, so h < 2/3.
  4. 4. What does Euler do to the energy of a frictionless oscillator?

    Each step multiplies the energy by 1 + omega^2 h^2.
  5. 5. The local truncation error of Euler's method is of order

    The first neglected Taylor term is h^2 y''/2.

Summary

  • Euler's update is y_{n+1} = y_n + h f(t_n, y_n).
  • Local error is O(hยฒ), global error O(h): first order.
  • Test with a known solution and check that error / h stays constant.
  • Decay problems need h < 2/ฮป for stability.
  • For oscillators Euler pumps in energy at every step, which motivates better methods.

Next: Runge-Kutta Methods