Learning Objectives
By the end of this section you will be able to:
- Derive Euler's update rule directly from the definition of the derivative, without memorization.
- Visualize the method as walking along tangent lines of the slope field.
- Execute the algorithm by hand on a small initial-value problem and check the answer against the exact solution.
- Distinguish local truncation error from global error and explain why one is bigger than the other.
- Empirically verify the first-order convergence rate using a log-log plot.
- Implement Euler's method in plain Python and as a vectorized PyTorch routine that supports batching and autograd.
- Recognize the stability barrier that limits how big can be on stiff problems.
Why We Need Numerical Methods
In Section 20.3 we solved differential equations by separation of variables. That trick is wonderful when it works — but it works rarely. Most equations that describe real physical systems have no elementary closed-form solution at all. Try to analytically solve
and you will quickly discover there is no antiderivative we can write down with familiar functions. Yet a physicist still needs to know what is. The way out is to give up on a formula and ask only for numbers: a table of pairs that approximate the true curve as closely as we want.
The numerical pivot. Instead of asking "what is the function y(t)?" we ask "starting from y(0), where will y be a tiny step later, and a tiny step after that, and after that…?"
Euler's method, published by Leonhard Euler in 1768 in his Institutionum Calculi Integralis, is the oldest and simplest answer to that question. Every modern ODE solver — from Runge–Kutta to the implicit BDF integrators that underlie scientific Python — can be understood as a smarter cousin of Euler's idea. Once you grasp Euler, the rest of the family becomes natural.
The Core Idea: Walk Along the Tangent
Imagine you are standing on a hillside in dense fog. You cannot see the valley, but the differential equation acts like a compass that tells you, at every spot you stand on, the exact direction of steepest descent. What do you do? You take one careful step in that direction, look at the compass again, take another step, and so on. Each step is a straight line, but stitched together they trace out a path that closely follows the true downhill curve.
That is precisely Euler's method. The differential equation gives the slope of the unknown solution at every point, even though we do not know the solution itself. We choose a small step size and we walk along the tangent for that length, recompute the slope at the new point, walk again, recompute, repeat.
The one sentence summary
Deriving the Update Rule
Start from the very definition of the derivative:
For a small but nonzero we drop the limit and accept an approximation:
Multiply through by and rearrange:
Now let stand for our running approximation to and let . Replacing the unknown true values by our approximations we obtain Euler's rule:
Euler's update
Repeated indefinitely from the initial condition , this produces the entire approximate trajectory.
It is worth pausing on what just happened. We replaced an infinite limit — the derivative — by a finite difference. That single substitution is the bridge between calculus and computation. Every numerical ODE method does some version of it; what differs is how cleverly each method recovers the curvature that this naive truncation throws away.
We can also reach the same formula from the first-order Taylor expansion:
Euler keeps the linear term and discards everything from onward. That discarded tail is the local error per step — we will quantify it in a moment.
Geometric Picture
Recall the slope field from Section 20.2: at every point we draw a tiny arrow whose direction is the slope . The true solution through is the curve tangent to every arrow it passes through.
Euler's method is the polygonal-line approximation to that curve: instead of following the field continuously, we read the arrow once, stride units along it, read the arrow at our new location, stride again, and so on. The result is a piecewise-linear path that secretly lives entirely on tangent lines of the true solution family — but never quite on the true curve we want.
What goes wrong, in one picture
The Algorithm in Plain Steps
- Inputs. The slope function , the initial point , the stopping time , and a step size .
- Setup. Set the step count , set , , and record .
- Repeat times.
- Evaluate the slope at the current point: .
- Take a tangent step: .
- Advance time: .
- Record the new .
- Output. The list of pairs which discretely approximate the solution curve.
Notation we will use throughout
Interactive Explorer
The visualization below lets you take Euler steps yourself on six different equations. The green curve is the true solution; the orange polygon is Euler's approximation. Pull the step size slider toward zero and watch the orange path snap onto the green one. Press Step to take a single Euler step at a time — the dashed red line is the tangent actually used to take the next step.
Things to try
- Choose Logistic, set and watch Euler overshoot the carrying capacity before relaxing back. This is a tiny taste of numerical instability.
- On Exponential growth, halve and observe the final error roughly halving. That is the empirical signature of a first-order method.
- On dy/dt = sin(t) - 0.3 y, no closed form exists — but a high-resolution reference is plotted as the green curve. Make tiny and you reproduce it.
Worked Example (by hand)
We pick an equation whose true solution we happen to know, so we can score each Euler step directly.
Its analytic solution (verifiable by direct substitution) is . We will compute four Euler steps to reach and compare against the truth.
Click to expand the full hand calculation
Step 1. Start at .
- Slope: .
- Update: .
- New time: .
- True value: . Euler error: .
Step 2. Now at .
- Slope: .
- Update: .
- True value: . Euler error: .
Step 3. Now at .
- Slope: .
- Update: .
- True value: . Euler error: .
Step 4. Now at .
- Slope: .
- Update: .
- True value: . Euler error: .
Observation. The errors grow as we march forward. That growth is the global error compounding — exactly as theory predicts. If we cut from to and re-run, the final error drops from to , a factor of for a 5× reduction in — broadly consistent with the linear rate, give or take a constant that depends on .
Here is a summary of the trace, computed and verified in Python:
| step | t | Euler y | slope | true y | |error| |
|---|---|---|---|---|---|
| 0 | 0.0 | 0.5000 | 1.500 | 0.5000 | 0.0000 |
| 1 | 0.5 | 1.2500 | 2.000 | 1.4256 | 0.1756 |
| 2 | 1.0 | 2.2500 | 2.250 | 2.6409 | 0.3909 |
| 3 | 1.5 | 3.3750 | 2.125 | 4.0092 | 0.6342 |
| 4 | 2.0 | 4.4375 | — | 5.3055 | 0.8680 |
How Wrong Is It? Local and Global Error
Two different errors are at play and it is essential to keep them apart.
Local truncation error
Suppose by some miracle we are exactly on the true curve at time . How wrong is a single Euler step? From the Taylor expansion we already wrote,
So per step, the error is . Halve and a single step becomes four times more accurate.
Global error
But we take roughly steps, and each step injects its own error into the input of the next step. A careful argument (using the Lipschitz continuity of ) shows that all those contributions add up to an accumulated error
where the constant depends on the problem (specifically, on the size of and on the Lipschitz constant of ). This is why we say Euler is a first-order method: global error scales with the first power of .
| quantity | scaling | meaning |
|---|---|---|
| Local truncation error per step | O(h²) | How much one step deviates from the truth |
| Global error after N steps | O(h) | How much the whole trajectory has drifted |
| Cost (function evaluations) | O(1/h) | More accuracy ⇒ proportionally more work |
The grim trade-off
Convergence Demo: Order of Accuracy
The convergence claim above is testable. The widget below solves the chosen IVP with successively halved step sizes and plots against . A first-order method must land on a line of slope 1.
The empirically fitted slope (green) sits right next to the reference slope-1 line (purple dashed) for every problem. This is experimental confirmation of the theory: Euler is first-order, no more and no less.
Stability: When Small Steps Are Still Not Small Enough
Accuracy is not the only concern. Some equations punish big not by being slightly wrong, but by producing solutions that explode to infinity even though the true solution is calm. The cleanest example is the linear decay equation
whose true solution decays smoothly to zero. Applying Euler:
So . For the Euler solution to also decay, we need , which requires
Push past this barrier and Euler oscillates with growing amplitude — a complete failure mode unrelated to accuracy. Equations with very large (or, more generally, with very fast and very slow dynamics mixed together) are called stiff, and explicit Euler is a poor tool for them. Implicit methods (Section 20.x and beyond) cure this.
Mental rule of thumb
Python Implementation
Below is the cleanest possible Python implementation, with every line annotated. Read the explanations to the right — they explain not just what each line does but why the algorithm needs it.
And here is the experiment that confirms the first-order rate — the same one whose plot you played with above, but now as code you can actually run:
When you run it you should see the column roughly halve every time doubles, and the rightmost column (the empirical ) converge to a single constant — the punchline of the whole error analysis.
PyTorch / Vectorized Implementation
The plain version is great for understanding but slow for large problems. The PyTorch version below is structurally identical — same update rule, same loop — but every value is a tensor. Three real advantages drop out for free:
- Batching. Solve a whole population of trajectories (different initial conditions, different parameters) in a single call.
- GPU. Move tensors to
cudaand the same code runs there. - Autograd. Because every step is a differentiable tensor op, we can differentiate the final state with respect to anything that fed into it — initial conditions, model parameters, the integration time. This is exactly the trick that makes Neural ODEs trainable.
From textbook to research
y_T.sum().backward() — is the conceptual seed of the entire Neural ODE literature. Replace the hand-coded f with a neural network, integrate with Euler (or RK4), backprop the downstream loss, and you have a learnable continuous-depth model.Beyond Euler: A Glimpse at RK4 and Neural ODEs
Once you see that Euler is just "estimate the slope, take a step", every improvement is easy to anticipate:
| method | slope used | global order |
|---|---|---|
| Forward Euler | slope at the start of the interval | O(h) |
| Midpoint (RK2) | slope at the interval midpoint | O(h²) |
| Classical RK4 | weighted average of four slope estimates inside the interval | O(h⁴) |
| Implicit (Backward) Euler | slope at the end of the interval (solve a small equation) | O(h), but unconditionally stable |
| Adaptive RK (RKF45, Dormand–Prince) | two RK estimates of different orders, used to pick h | variable, controlled by tolerance |
Every one of these is a refinement on the same skeleton you just coded. The whole field of numerical analysis for ODEs is the search for ways to extract more accuracy or more stability per slope evaluation than Euler manages.
Common Pitfalls
1. Using the same h that worked on another problem
2. Reading the slope at the wrong point
3. Storing only the endpoint when you need the path
4. Confusing local and global error
Summary
Euler's method is the canonical bridge from calculus to computation. Its update rule
falls out directly from the definition of the derivative; geometrically it walks along tangent lines of the slope field. The cost of this simplicity is first-order accuracy — global error scales linearly with the step size — and a stability barrier on stiff equations. Yet every fancier ODE solver is a refinement of the same skeleton, and the PyTorch version is the first step toward Neural ODEs and the modern continuous-depth models built on top of them.
Take home. If you remember one thing, remember this rhythm: read the slope, walk a step, repeat. Everything beyond Euler is just how to read the slope more cleverly.