Case Study · Math · Numerics
A Map of Every Solution
The idea
A first-order differential equation y′ = f(x, y) is usually met as a thing to solve; separate the variables, hunt the integrating factor, arrive at a formula. But there’s a way of seeing one that I find more revealing: at every point of the plane, the equation pins down a slope and nothing else. Draw a short tick with that slope at each grid point and you get the slope field; a map, in the strictest sense this site uses the word, of everywhere a solution could possibly go. Solutions aren’t formulas on this map; they’re routes, curves that keep their heading tangent to the field at every step. Solving an ODE numerically is navigating that terrain.
Once you see it that way, Euler’s method stops being a formula and becomes the obvious first thing any traveler would try: stand at your point, read the local slope, walk straight in that direction for a step of length h, look again. Its failure is equally visible; on curved terrain, every straight stride drifts off the route, and the error compounds with a first-order rate: halve the step, halve the miss. RK4 is the seasoned traveler, sampling the slope four times per stride (start, two midpoints, end) and blending the readings; same terrain, same step size, four decimal places instead of one.
The plate below is that terrain, with its travelers crossing it; explore it first, then read what each equation is teaching.
| step h | Euler y(1) | error vs e |
|---|---|---|
| 0.5 | 2.2500 | 0.4683 |
| 0.25 | 2.4414 | 0.2769 |
| 0.125 | 2.5658 | 0.1525 |
With JavaScript enabled this plate draws the slope field live and integrates from any initial condition you choose, for these equations or one you type yourself. The table above is the figure's heart in numbers: on y′ = y, halving Euler's step roughly halves the error (a first-order method) while RK4 at h = 0.5 is already correct to five decimals.
The approach
The plate above draws the field for four equations chosen because each teaches one thing and every one has a closed-form solution; so the dashed line is truth, not a finer approximation. Exponential growth (y′ = y) shows Euler systematically undershooting terrain that curves away from every tangent. The logistic equation shows both methods funneled by the field itself toward the carrying line y = 1; some terrain forgives error. The driven decay y′ = sin x − y shows transients dying into a steady rhythm all solutions share.
The decay equation y′ = −2y is the quiet star, because it has a stability wall. Euler’s update there is y ← (1 − 2h)y: for h < 1 it decays like the truth, but past h = 1 the factor’s magnitude exceeds one and each stride overshoots the axis by more than it started, oscillating outward to infinity; the method diverges on the most docile ODE imaginable. Slide h to the right and watch it happen. If that threshold feels familiar from elsewhere on this site, it should: gradient descent is Euler’s method, applied to the gradient-flow equation ẇ = −∇L(w), and the learning-rate wall at η = 2/λ on the gradient-descent plate is this exact phenomenon wearing machine-learning clothes. One idea, two atlases.
The plate also lets you swap Euler for a second-order traveler, midpoint or Heun: same terrain, same strides, one full order of accuracy between the coarse first guess and RK4’s four samples. And the equation menu ends in a blank you can fill: type any y′ = f(x, y) and the field redraws around it. Three things worth typing into that blank are at the end of this page.
Reading the error
Under the field is a line of numbers, and it is the part of the plate I would watch first: the exact value at the right edge, then where each traveler actually arrived and how far off it was. Two knobs move those numbers, and they move them by different laws.
An order is a statement about one stride. Expand the true solution in a Taylor series about the current point: Euler’s stride agrees with it through the term in h, so what it throws away is of size h². A second-order stride agrees through h² and throws away h³, and RK4 agrees through h⁴ and throws away h⁵. Crossing a fixed interval takes (b − a)/h strides, which hands one factor of h straight back, so those per-stride misses accumulate into a final error of size h for Euler, h² for midpoint and Heun, h⁴ for RK4. That is the whole content of the word order, and it is why halving the step divides Euler’s final error by about two and RK4’s by about sixteen.
Integrating y′ = y from (0, 1) out to x = 4, where the answer is e⁴ = 54.5982, with the same three steppers the plate uses, the absolute error at the right edge comes out:
| step h | Euler | Heun | RK4 |
|---|---|---|---|
| 0.4 | 25.67 | 4.18 | 0.0334 |
| 0.2 | 16.26 | 1.24 | 0.00247 |
| 0.1 | 9.34 | 0.337 | 0.000168 |
| 0.05 | 5.04 | 0.0876 | 0.0000109 |
Read down a column and the promised factors are there, approximately. Euler’s error shrinks by 1.58, then 1.74, then 1.85 as h halves; Heun’s by 3.37, 3.68, 3.84; RK4’s by 13.6, 14.7, 15.4. Not one of them is the round number, and the drift is the interesting part: an order law is asymptotic, a claim about the limit as h goes to zero, and at h = 0.4 across an interval where the solution grows by a factor of e⁴ the constant in front of it is still moving. Watching 13.6 climb toward 16 taught me more about what “fourth order” means than the exponent did.
Where each method’s stability ends
Accuracy is one axis; stability is another, and the decay equation separates them. On y′ = −2y every method here turns a stride into a multiplication: the height at the next grid point is some factor R times the height at this one, and R depends only on the product z = −2h. Euler gives R = 1 + z. Midpoint and Heun sample in different places and arrive at the same R = 1 + z + z²/2. RK4 gives R = 1 + z + z²/2 + z³/6 + z⁴/24. Those are the Taylor series of the truth, e^z, cut off at each method’s own order, which is the order claim again, read one stride at a time.
The truth always decays: e^z is below 1 in magnitude for every negative z, at any distance. A polynomial cannot do that, because a polynomial eventually runs off to infinity, so past some z its magnitude crosses 1 and every stride grows.
| step h | truth e^z | Euler | Midpoint / Heun | RK4 |
|---|---|---|---|---|
| 0.4 | 0.4493 | 0.2000 | 0.5200 | 0.4517 |
| 0.8 | 0.2019 | −0.6000 | 0.6800 | 0.2704 |
| 1.0 | 0.1353 | −1.0000 | 1.0000 | 0.3333 |
| 1.2 | 0.0907 | −1.4000 | 1.4800 | 0.5584 |
h = 1 is the exact edge for both the first-order and the second-order methods. Euler’s factor there is −1, so its path flips sign every stride and never shrinks; the second-order factor is +1, so its path freezes at the height it started from. One notch further, at the slider’s top, Euler oscillates outward and the order-2 traveler climbs straight up, while RK4 on the same step still contracts by 0.5584 a stride. Accuracy is not what saves it. Against a true 0.0907, a factor of 0.5584 is wrong by a factor of six; it is just wrong in a direction that keeps shrinking, and shrinking is the entire thing stability asks for.
The middle column is the surprise. Trading Euler for a second-order method buys an order of accuracy and buys nothing whatever in stability: both walls stand at z = −2, which is h = 1 on this equation. The column that survives past it is the one whose polynomial keeps two more terms, and RK4’s own wall is at z = −2.7853, or h = 1.39 here, just out of the slider’s reach on this preset. Order and stability being independent is why stiff problems are a subject rather than a nuisance: no amount of order fixes them, and the remedy is implicit methods, whose R is a rational function that stays under 1 all the way out.
What to type in the blank
Three entries in the custom field are worth the trip.
Type -10y and the wall moves. The coefficient is now −10 rather than −2, so
Euler’s threshold drops to h = 0.2, which the slider straddles: the traveler
that behaves impeccably at h = 0.05 tears itself apart at h = 0.25, on an
equation every one of whose solutions decays quietly to zero. That gap between
what the solution does and what the method does is what stiff names, and it is
why a production integrator spends its effort choosing h rather than taking it.
Type y^2 and the difficulty is not the method. The field is perfectly well
defined at every point of the plane, and the solution through (0, 1) is
y = 1/(1 − x), which reaches infinity at x = 1 and does not exist past it. A
slope field is a map of where a solution may go next, and it has no way to show
that the route ends: the travelers run off the top of the frame, the fine-step
reference runs off with them, and all three agree about something that stopped
being true.
Type x^2 + y^2 for the honest common case. That is a Riccati equation, and
substituting y = −u′/u turns it into u″ + x²u = 0, whose solutions are Bessel
functions of order one quarter rather than anything elementary. So the dashed
line has to be a fine-step RK4 reference, and there is nothing better on offer.
Which is the situation numerical integration exists for; the four presets are
the unusual ones.
Scope
The plate uses fixed-step methods by design: production integrators adapt h on the fly and would hide the stability story by simply refusing to take the bad step, so a fixed step is exactly the right instrument for showing why the step size matters. Stiffness appears here in miniature, and the implicit methods that answer it are a natural next plate rather than a gap. Choosing the four presets for their closed forms tilts the gallery toward well-behaved terrain, which is the price of a dashed line a reader can trust. That boundary is itself the lesson: the fields where numerics matter most are the ones where no closed-form truth-line can be drawn.