Euler's Method for Numerical ODEs
Problem
Approximate the solution of y′ = x + y with y(0) = 1 on 0 ≤ x ≤ 1 using Euler's method with step size h = 0.2. Compare against the exact solution y = 2e^x − x − 1.
Explanation
The simplest numerical ODE method
Most ODEs you meet in the wild — especially non-linear ones — have no closed-form solution. Numerical methods approximate the solution by stepping along the slope field in small increments. Euler's method is the most basic such method, and understanding it well sets you up for every more-sophisticated solver (Runge-Kutta, Adams, adaptive steppers).
The idea: given and , you know the slope at every point. Start at , take a short step of width along the tangent line, then recompute the slope at the new point and step again.
The update rule is
Geometrically: replace each infinitesimal tangent segment of the true solution with a finite chord of length .
The given IVP
So . Five steps to reach .
Step-by-step Euler computation
Start at .
Step 1 ():
- slope = .
- .
- .
Step 2 ():
- slope = .
- .
- .
Step 3 ():
- slope = .
- .
- .
Step 4 ():
- slope = .
- .
- .
Step 5 ():
- slope = .
- .
- .
Numerical estimate: .
Exact solution — compare
is a first-order linear ODE (#174, #175). Integrating factor :
Integrate by parts:
Apply : .
Exact value at : .
Error: , or about 13% relative error. Ouch — Euler's method is crude.
Why the error is so large — local vs global
Each step introduces a local truncation error that scales as (it's the neglected second-derivative term in the Taylor expansion):
We keep the first two terms (that's Euler) and drop the . Accumulated over steps, errors compound to a global error of . Euler is first-order accurate: halving halves the error (roughly).
How to do better
- RK2 (midpoint / improved Euler): use the slope at the midpoint or an average of start/end slopes. Global error .
- RK4 (classic Runge-Kutta): weighted average of 4 slopes per step. Global error . Standard workhorse.
- Adaptive step size (RK45, Dormand-Prince): adjust on the fly to keep the estimated error under a target. Used by scipy
odeint, MATLABode45, etc. - Implicit methods (backward Euler, BDF): for stiff ODEs where explicit methods require extremely small to stay stable.
Euler is almost never used in production — but it's the conceptual foundation for everything else.
Stability — not just accuracy
For with (a decaying ODE), Euler gives . If , the factor and the numerical solution grows without bound — completely wrong. Stability requires , which can force absurdly small for stiff ODEs with large . This is why implicit methods exist.
Error halves when halves
If you run the same problem with (10 steps), the error at is roughly half what it was with . With , another factor of 2. This linear-in- decay is the signature of a first-order method.
(Contrast RK4, where halving cuts the error by a factor of .)
Where Euler's method shows up pedagogically
- Teaching the concept of numerical integration without the machinery of higher-order methods.
- Simple game-physics simulations: for objects with small velocities and short time steps, Euler is fine. Most game engines start with Euler and graduate to Verlet or RK2 when stability matters.
- First-pass sanity checks: "does the ODE behave qualitatively as I expect?" A quick Euler run answers that.
- Inside more complex methods: multi-step methods like Adams-Bashforth use past Euler-like evaluations.
Common mistakes
- Using instead of . That would be backward Euler (implicit), a different and more stable method, but not what "Euler's method" normally means.
- Forgetting to advance . Each step must update both and .
- Comparing to exact solution at intermediate assuming the exact is at . The numerical is an approximation; the exact value at is different.
- Not matching units. If your ODE is physical (units of = seconds, units of = metres), your step size is in seconds and your has units of metres/second.
Try it in the visualization
Draw the slope field for , overlay the exact solution , and plot the Euler "stairstep" of tangent segments. Slide down from 0.5 toward 0.02 and watch the stairstep hug the exact curve more tightly. Overlay an error curve to see the global error shrinking.
Interactive Visualization
Parameters
Got your own math or physics problem?
Turn any problem into an interactive visualization like this one — powered by AI, generated in seconds. Free to try, no credit card required.