Skip to content
Tutorials Center
My learning

Topics

Lesson 26 of 39 ยท Numerical Methods

The Euler Method from Scratch

Build Euler's method for ODEs from the tangent-line idea, measure its local and global error, see first-order convergence, and extend it to systems in Python.

Most differential equations cannot be solved with a formula, so computers solve them step by step. The oldest and simplest recipe is Euler's method. It is rarely used in serious software because better methods exist, but it is the clearest way to understand every numerical ODE solver: how steps work, how errors grow and why step size matters.

The idea is a single sentence. Stand at a known point, look at the slope the equation gives you there, and walk a short distance along that tangent line. Repeat from the new point.

The formula

For the initial value problem y' = f(t, y), y(t0) = y0, pick a step size h and set t_{k+1} = t_k + h. Euler's update is

TEXT
y_{k+1} = y_k + h f(t_k, y_k)

This is just the first two terms of the Taylor expansion y(t + h) = y(t) + h y'(t) + (hยฒ/2) y''(t) + ..., with the rest thrown away. The thrown-away part, about (hยฒ/2) y'', is the local truncation error: the mistake made in one step. Over the roughly T/h steps needed to reach time T, these errors accumulate to a global error of order h. That makes Euler a first-order method: halve the step, halve the error.

Python Euler's method for y' = -2y

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 smaller the step, the closer the result. With h = 0.5 Euler even gets the answer exactly 0 at t = 2, because 1 + h*(-2) = 0 after the first step. That is a warning sign: a step that is too large can give nonsense, not just inaccuracy.

Measuring the order

You can confirm the first-order claim numerically. Compute the error at a fixed time for steps h, h/2, h/4, .... If the method has order p, the error ratio between successive halvings approaches 2^p, and the slope on a log-log plot is p.

Python Convergence order of 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.

The ratios settle near 2: halve the step, halve the error. That is the signature of order 1.

Euler for systems

Nothing changes for a system u' = F(t, u); u is a vector, and the update is u_{k+1} = u_k + h F(t_k, u_k). A harmonic oscillator x'' = -x as a system shows a classic weakness. The true solution has constant energy xยฒ + vยฒ, but Euler makes it grow.

Python Euler adds energy to an oscillator

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.

Each Euler step multiplies the energy by exactly 1 + hยฒ, so it grows without bound. For a stable physical system that is an unforgivable defect, and one of the reasons we move on to Runge-Kutta methods.

Euler in one picture

Start A known point (t, y).
Slope Evaluate f at that point.
Step Walk h along the tangent.
Repeat Errors pile up.

Global error is proportional to h: a first-order method.

1 / 2

Variants: backward Euler and the midpoint idea

Backward (implicit) Euler uses the slope at the end of the step: y_{k+1} = y_k + h f(t_{k+1}, y_{k+1}). Now y_{k+1} appears on both sides, so each step needs an equation solved. In return the method is far more stable, which you will see in stiff equations. For y' = -2y it reads y_{k+1} = y_k / (1 + 2h), and it never overshoots no matter how large h is.

Using the slope at the middle of the interval gives the midpoint method, second-order accurate, and the idea that leads to Runge-Kutta.

Practical advice

  • Use Euler to learn and to prototype, not to produce results you rely on.
  • To estimate your error, run with h and h/2; if the answers differ in the digits you care about, the step is too large.
  • Watch out for floating-point accumulation: computing t = t0 + k*h is more accurate than t += h repeated many times.
  • Plot the solution. Visual oscillation, growth or negative values where none should exist are early warnings.

Common mistakes

  • Using the new t or new y inside f for explicit Euler. The slope must use the old values.
  • Choosing h by feel without a convergence check.
  • Computing the number of steps with floating-point division without rounding.
  • Expecting energy or other invariants to be conserved.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Euler's method is derived from:

    y(t+h) โ‰ˆ y(t) + h y'(t) keeps the constant and linear Taylor terms.
  2. 2. If you halve the step size, the global error of Euler's method roughly:

    Euler is first order, so error is proportional to h.
  3. 3. With y' = -2y, y(0) = 1 and h = 0.5, one explicit Euler step gives:

    y1 = 1 + 0.5 * (-2) * 1 = 0.
  4. 4. For the oscillator x'' = -x, explicit Euler makes the energy:

    Each step multiplies xยฒ + vยฒ by 1 + hยฒ, so the energy grows.

Summary

  • Euler's method: y_{k+1} = y_k + h f(t_k, y_k), a tangent-line step.
  • Local error is O(hยฒ), global error O(h): a first-order method.
  • It extends unchanged to systems by using vectors.
  • It can add or lose energy and can be unstable for large h.
  • Implicit (backward) Euler and midpoint ideas lead to better methods.

Next: Runge-Kutta Methods