JOVANA
Explore Library Glossary Getting Started Three Levels Fields How it works Mission
Join the mission
All guides

Euler's Method: Following the Slope

A differential equation tells you the slope at every point but not the path itself. Euler's method is the simplest way to walk that path: stand where you are, ask the equation which way is downhill, take a small step that way, and repeat. It is crude, but every fancier ODE solver in this rung is built on the idea.

A slope at every point, but no path

A great deal of science arrives in the form 'I know the rate of change, not the thing itself.' A cooling cup of coffee loses heat at a rate proportional to how much hotter it is than the room; a population grows at a rate set by its current size; a planet's velocity changes according to the gravity it feels right now. Each of these is an ordinary differential equation (ODE): a rule of the shape y'(t) = f(t, y), which hands you the slope y' at any moment t given where you currently are, y. Pair that rule with a starting value y(t_0) = y_0 — where the cup, the population, the planet was at the start — and you have an initial-value problem. The question is always the same: from the slope law and the starting point, reconstruct the whole future path y(t).

For a lucky handful of equations you can solve this with calculus and write y(t) as a tidy formula. But the moment f is even mildly realistic — a nonlinear term, a forcing that switches on and off, two or three quantities feeding back on each other — the closed-form solution vanishes, just as it did for the root-finding problems earlier in this ladder. So we do the same thing we always do here: we give up on an exact formula and compute a numerical approximation to the path. The trick is to picture the equation not as something to integrate symbolically, but as a field of little arrows. At every point of the plane, f tells you a direction — the slope a solution passing through that point must have. Solving the ODE means finding a curve that, everywhere along its length, runs tangent to those arrows.

Just take a small step in the slope's direction

Here is the whole idea, and it is almost insultingly simple. You are standing at the start, at the point (t_0, y_0). The equation tells you the slope right there: it is f(t_0, y_0). If you only trust that slope for a tiny stretch of time h — one small step size — then over that stretch the path is approximately a straight line with that slope. So you walk along the straight line: your new time is t_1 = t_0 + h, and your new height is y_1 = y_0 + h * f(t_0, y_0). You have just replaced an unknown curve by its tangent line for one short step. Now you are at a new point, you ask the equation for the slope there, and you step again. Repeat, and you trace out a connected chain of little straight segments that hugs the true curve.

This is Euler's method, the explicit or forward Euler method, and the general step is y_{n+1} = y_n + h * f(t_n, y_n) with t_{n+1} = t_n + h. The word time-stepping names exactly this rhythm: chop the time interval into pieces of width h, and march forward one piece at a time, each step's answer feeding the next. There is a clean way to see where the rule comes from. The derivative is a limit of slopes, y'(t) = lim of (y(t+h) - y(t))/h; if you stop taking the limit and just use a finite h, you get the finite-difference approximation y'(t_n) is about (y_{n+1} - y_n)/h. Set that equal to the slope law f(t_n, y_n) and solve for y_{n+1}, and Euler's formula falls right out. Euler is what a derivative looks like when you refuse to take the limit all the way.

forward_euler(f, t0, y0, h, N):     # N steps of size h
    t = t0
    y = y0
    for n in 1..N:
        y = y + h * f(t, y)          # step along the current slope
        t = t + h
        record (t, y)
    return y

# example: y' = y, y(0) = 1, true answer is e^t = 2.71828...
# h = 1, one step : y1 = 1 + 1*(1)        = 2.0      (error ~0.72)
# h = 0.5, 2 steps: y2 = 2.25             -> 2.25    (error ~0.47)
# h = 0.25,4 steps: y4 = (1.25)^4         = 2.4414   (error ~0.28)
Forward Euler in a few lines, with the classic test y' = y whose exact answer at t = 1 is e = 2.71828. Halving h roughly halves the error — the signature of a first-order method, which the next guide makes precise.

How wrong is one step — and how does it pile up?

Euler's method is wrong, and it is worth being precise about how. In a single step you replaced a curving solution by its straight tangent, so you slightly miss wherever the curve bends away from the line. The size of that one-step slip is the local truncation error, and a quick look at the Taylor expansion of y(t+h) pins it down: the true step is y_n + h*y'_n + (h^2/2)*y''_n + higher terms, while Euler keeps only y_n + h*y'_n. The first thing it throws away is the (h^2/2)*y''_n term, so the error made in one step is proportional to h^2. Halve the step and a single step becomes four times more accurate. The curvier the solution (the bigger y'') the worse one step does, which matches intuition: a straight line tracks a gently bending curve well and a sharply bending one badly.

But you do not take one step — you take many, and the small slips accumulate. This is the crucial and slightly surprising part. To cross a fixed time interval of length T you need N = T/h steps; if each contributes an error of size about h^2, then naively N of them might add to (T/h)*h^2 = T*h. And indeed the global error — the gap between the numerical answer and the truth at the end — is proportional to h, not h^2. One power of h is lost simply because you must take more steps when each is smaller. So Euler's method is first order: the local error per step is O(h^2), but the global error you actually care about is O(h). Halving the step roughly halves the final error — better, but only linearly better, and you pay for it with twice as many steps.

Forward and backward: who supplies the slope?

There is a sibling worth meeting now, because it quietly governs the whole rest of this rung. Forward Euler uses the slope at the start of the step, where you already are: y_{n+1} = y_n + h * f(t_n, y_n). The backward (implicit) Euler method instead uses the slope at the end of the step, where you are about to land: y_{n+1} = y_n + h * f(t_{n+1}, y_{n+1}). Read that twice — the unknown y_{n+1} appears on both sides. You cannot simply compute the right-hand side and read off the answer; you must solve an equation for y_{n+1} at every single step. That is what 'implicit' means, and it is real work: if f is nonlinear you typically solve that equation with Newton's method each step, which is why implicit methods cost more per step than explicit ones.

If backward Euler is more expensive and no more accurate — it is also only first order — why would anyone use it? The answer is stability, the deepest theme of this rung, and we will only foreshadow it here. Imagine an equation whose true solution decays rapidly and smoothly to zero, like y' = -100*y. Forward Euler can do something alarming on such a problem: unless the step h is small enough, its numerical answer does not decay but instead oscillates with growing swings, exploding to nonsense even though the real solution is calmly heading to zero. Backward Euler, on exactly the same problem, decays smoothly for any step size you choose. The slope you trust — the one at the start versus the one at the end — turns out to control whether errors die away or blow up.

That difference has a name we will study carefully later: forward Euler is only conditionally stable, meaning its step is capped by the problem, while backward Euler is unconditionally stable on such decaying problems. Equations where the safe explicit step is punishingly tiny are called stiff, and they are exactly where you are forced to pay for an implicit method. So forward and backward Euler are not just two ways to write almost the same formula; they are the two ends of the central trade-off in this whole subject — cheap-per-step but fragile, versus costly-per-step but robust.

Why bother with so crude a method?

Let us be blunt: nobody runs serious simulations on plain forward Euler. Its first-order accuracy is poor — to gain one more correct digit you must take ten times as many steps — and its fragile stability means stiff problems can wreck it. The methods later in this rung exist precisely to fix these flaws. The Runge-Kutta methods, and the famous RK4 in particular, sample the slope at several cleverly chosen points inside each step and blend them, buying much higher order for a modest extra cost; multistep methods reuse past slopes for efficiency; adaptive solvers grow and shrink h on the fly to hit a target accuracy. Every one of those is, at heart, a smarter answer to the same question Euler asked first: given where I am and the slope law, how do I take one good step forward?

So why start here? Because Euler is the clean, honest skeleton on which all of that flesh hangs. Once you have truly seen that an ODE solver is just 'evaluate the slope, step, repeat,' the rest of the rung stops being a parade of magic formulas and becomes a series of answers to clear questions you can now ask yourself. How big is the error in one step, and how does it add up over many? That is the next guide on local and global error and order. How can I sample the slope more cleverly within a step to do far better than a straight tangent? That is Runge-Kutta. How do I choose h automatically and lean on history? That is adaptive and multistep methods. And when does the explicit step explode, forcing me to go implicit? That is stiffness, the climax of the rung. Euler is small, but it is the doorway to all of it.