Skip to content
Tutorials Center
My learning

Topics

Lesson 27 of 37 ยท Ordinary Differential Equations

Euler's Method for Ordinary Differential Equations

Solve initial value problems step by step with forward and backward Euler. Learn local and global error, order of convergence and stability with Python code.

An ordinary differential equation (ODE) describes how something changes: a cooling cup, a growing population, a falling object. An initial value problem (IVP) gives the rule y' = f(t, y) and a starting value y(t0) = y0, and asks for y at later times. Most ODEs have no formula for the solution, so we step forward numerically.

The simplest method is Euler's method. It is rarely the best choice, but every other method is a refinement of it, and it makes errors, order and stability easy to see.

The method

At the current point (tn, yn) the slope is f(tn, yn). Walk along that tangent line for a step h:

y(n+1) = yn + h * f(tn, yn), t(n+1) = tn + h.

Geometrically you follow the tangent, then recompute the slope at the new point and turn. The formula is the first-order Taylor expansion: it is exact for y' = constant and approximate otherwise.

Python Euler's method from scratch

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 numbers drift apart over time: errors accumulate.

Local and global error

  • Local truncation error: the mistake made in one step, assuming the previous value was exact. For Euler it is O(h^2), the first Taylor term we dropped.
  • Global error: the accumulated error at a fixed end time. There are about 1/h steps, each adding O(h^2), so the global error is O(h).

That is why Euler is called a first-order method: halve h, and the final error halves too. You pay twice the work for half the error, which is a poor trade compared with higher-order methods in the next lesson.

Python Check first-order convergence

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 ratio approaches 2, confirming order one.

Stability: when a stable equation blows up

Take y' = -k y with k > 0, whose true solution decays smoothly. Euler gives y(n+1) = (1 - h k) yn. The numerical solution decays only if |1 - h k| < 1, i.e. h < 2 / k. If h is larger, the numbers oscillate and grow even though the true solution shrinks to zero. This stability limit has nothing to do with accuracy; it is a hard requirement.

Python Euler goes unstable

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.

For h = 0.05 the factor is 1 - 2.5 = -1.5, so the values flip sign and grow.

Backward (implicit) Euler

Evaluate the slope at the new point:

y(n+1) = yn + h * f(t(n+1), y(n+1))

The unknown y(n+1) appears on both sides, so each step needs a solve (exact for linear equations, Newton's method in general; see root finding). The price is a nonlinear equation per step. The reward: for y' = -k y it gives y(n+1) = yn / (1 + h k), which decays for any h > 0. Backward Euler is unconditionally stable on this test problem, which makes it the starting point for stiff problems (adaptive steps and stiff ODEs).

Python Forward versus backward Euler

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.

Backward Euler stays positive and shrinking, although with this big step it is not very accurate. A stable answer is better than an exploding one.

The improved Euler idea

A cheap accuracy boost: predict with Euler, then average the slopes at both ends (Heun's method, a Runge-Kutta method of order two). That is the bridge to the next lesson.

flowchart LR
  A["Slope at start"] --> B["Predict y with Euler"]
  B --> C["Slope at predicted end"]
  C --> D["Average the two slopes<br/>take the step"]

Common mistakes

  • Choosing h by accuracy alone and ignoring stability.
  • Counting steps with float arithmetic: use round((t_end - t0) / h), not a while t < t_end loop that may do an extra step.
  • Expecting Euler to conserve energy in oscillating systems; it drifts (it spirals outward for a spring).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the global error order of Euler's method?

    Each step has local error O(h^2), and about 1/h steps accumulate to O(h).
  2. 2. For y' = -k y, what step size keeps forward Euler stable?

    The update factor is 1 - hk, and |1 - hk| < 1 requires h < 2/k.
  3. 3. What makes backward Euler harder to implement than forward Euler?

    The new value appears on both sides of the formula, so each step solves an equation.
  4. 4. You halve h in Euler's method. What happens to the final error?

    A first-order method has error proportional to h.

Summary

  • Euler's method follows the tangent: y(n+1) = yn + h f(tn, yn).
  • Local error O(h^2), global error O(h): first order.
  • Explicit Euler is only conditionally stable (h < 2/k for the decay test problem).
  • Backward Euler is implicit, needs a solve per step, and is unconditionally stable on the test problem.

Next: Runge-Kutta methods