Flow Lab

Syllabus / Module 3: First-Order ODEs

Lesson 3.2

Direction Fields & Euler's Method Revisited

Slope fields as a picture of every solution at once; isoclines; Euler's method as 'follow the arrows'; first look at convergence.

Written byClaude Sonnet

🌊
Why this matters for simulation: Advecting a particle through a velocity field is following a direction field.

By the end of this lesson you can…

  • Sketch the direction field of a first-order ODE from its right-hand side, and use isoclines to organize the drawing.
  • Sketch solution curves by eye from a direction field, using the tangent, isocline and sign-change rules.
  • Explain Euler's method geometrically as following the arrow for time h, and carry out Euler steps by hand.
  • Derive why Euler's local error is O(h^2) and its global error O(h), and confirm the halving of the error numerically.
  • Predict when Euler misbehaves: the step-size limit 2/k for y' = -ky, and why Euler sails past the finite-time blow-up of y' = y^2.
On this page
·
  1. From a rule to a picture of every solution
  2. Isoclines, and sketching a solution by eye
  3. Euler’s method: follow the arrow for time h
  4. Why the global error is first order
  5. Where Euler goes wrong
  6. Too large a step: y’ = −10y
  7. The solution itself ends: y’ = y²
  8. Exercise: measure the order of convergence
  9. Where this goes next
  10. Key Takeaways

You have written Euler’s method, three variants of it, RK4 and RK45. This lesson meets you on that ground, and it adds two things your code never told you. The first is a picture: the right-hand side of y′=f(t,y)y' = f(t, y) draws an arrow at every point of the plane, and every solution is a curve that follows those arrows. The second is the theory of the error: why Euler is first order, and the two situations where it fails outright. Last lesson (3.1 What Is a Differential Equation?) ended with Euler on y′=y2y' = y^2 from (0,0.5)(0, 0.5) producing finite numbers long after the true solution blew up at t=2t = 2. We now explain that, and we keep the module’s habit of setting the exact solution next to the numerical one.

▶ Video A direction field as a map of every solution, and Euler's method as walking along itcaptions only
Transcript
  1. Last lesson, Euler sailed past a blow-up. To see why, start with a picture of the equation itself.
  2. Take y prime equals t minus y. At every point the right-hand side gives a slope, so draw a short tick with that slope. At zero, one it is minus one. At two, zero, plus two. At one, one, flat. Fill a grid and you have a direction field, with nothing solved.
  3. To draw it fast, use isoclines: curves where the slope is constant. Here slope m lives on the line y equals t minus m, so every tick along one line is parallel. On the line y equals t the slope is zero, and every tick is flat.
  4. A solution is a curve tangent to every tick it passes. Drop a seed at zero, one and follow the ticks. It heads down, flattens as it meets the line y equals t, turns, and climbs. Other seeds give other curves, and none cross, since a crossing needs two slopes at one point.
  5. Now Euler, as geometry. Follow the arrow for a time h, then stop and re-aim. With h a half, we land at a half, a half, then follow a flat arrow to one, a half. The true curve has bent away: at t equals one, we are off by 0.236.
  6. Shrink h. A quarter, an eighth, a sixteenth. The polygon hugs the true curve, and the miss at t equals one falls: 0.103, 0.049, 0.024.
  7. Keep halving h, and each error is about half the last. The ratios are 2.29, 2.12, 2.06, 2.03, heading for two. Halve the step, halve the error. That is a first order method.
  8. Why? A step follows the tangent, and the curve bends, so each step misses by about h squared. Reaching t equals one takes one over h steps. One over h misses of size h squared add up to size h: one power is lost to counting.
  9. Now the first failure. The true solution of y prime equals minus ten y decays smoothly. Euler multiplies by one minus ten h each step. At h of 0.05 the factor is a half, and it decays as it should. At 0.15 it is minus a half: still decaying, but flipping sign.
  10. At h of 0.25 the factor is minus one and a half. We get one, minus one point five, two point two five, minus three point three seven five, five point oh six. The exact answer dies away to zero. Euler flips sign and grows.
  11. The reason is overshoot. The arrow points at zero, but a step along it has length ten h times the size of y, while zero is only the size of y away. Once ten h passes two, the step is more than twice the distance, so every landing is farther out. Euler is stable only below h of 0.2.
  12. The second failure. y prime equals y squared from one half has an exact solution that blows up at t equals two. Euler with h of a half sails past: at t equals two it reports 1.83. Nothing in the algorithm can notice. Each step just adds a finite amount.
  13. It does explode, but late: 3.5, 9.6, 56, 1600. Shrink h, and the numerical blow-up creeps toward the truth. Euler first passes a million at t equals 4.5, 2.7, 2.09, 2.011. Finite output is not proof that a solution exists.
  14. The fluids link: a particle in a steady velocity field obeys x dot equals u of x. The field is the arrow, the path is the solution, and an Euler step follows the arrow for time h. Here arrow length is speed, and the step lands just outside the true circle.
  15. So: a direction field is the equation drawn as arrows, solutions are curves tangent to them, and Euler walks them in steps. Its error is first order, and it fails by overshooting, and by sailing past blow-ups. Next, we start solving exactly, with separable equations.

From a rule to a picture of every solution

The equation y′=f(t,y)y' = f(t, y) says: if you are at the point (t,y)(t, y), your slope is f(t,y)f(t, y). Evaluate ff on a grid and draw a short tick with that slope at each grid point. The result is the direction field (or slope field). It is built without solving anything, only by evaluating ff.

Take y′=t−yy' = t - y. At (0,1)(0, 1) the slope is −1-1; at (2,0)(2, 0) it is +2+2; at (1,1)(1, 1) it is 00. A solution is a curve that is tangent to the tick at every point it passes through. Through every point of the plane there is a tick, so a curve starts at every point: the field is the whole family of solutions drawn at once, which is the family picture of the last lesson without any formula. For a well-behaved ff two curves cannot cross, because at a crossing the tick would need two slopes.

The exact solutions of y′=t−yy' = t - y are y=t−1+Ce−ty = t - 1 + Ce^{-t} (check: y′=1−Ce−t=t−yy' = 1 - Ce^{-t} = t - y). Open the sim below and compare the curves you seed with the ticks they follow.

◆ Interactive Seed solutions in the field, then let Euler follow the arrows with step h

Loading simulation…

A direction field records only the direction at each point, not a speed. For a pure slope field that is enough, because tt is the independent variable and moves at unit speed to the right. Keep this in mind for the fluids connection at the end of the lesson, where the length of the arrow matters.

Isoclines, and sketching a solution by eye

An isocline is a curve along which the slope is constant: f(t,y)=mf(t, y) = m. Every tick on it is parallel, so isoclines make the field quick to draw. For y′=t−yy' = t - y the isocline of slope mm is the line y=t−my = t - m. For y′=y2y' = y^2 it is the pair of horizontal lines y=±my = \pm\sqrt{m}.

The isocline for m=0m = 0 is special: along it the solutions have horizontal tangents, so any solution crossing it is at a local maximum or minimum. For y′=t−yy' = t - y that is the line y=ty = t. Above it y′<0y' \lt 0 and below it y′>0y' \gt 0, so a solution coming from above descends, meets the line, and then climbs: a minimum. The isocline m=1m = 1, the line y=t−1y = t - 1, has slope 11 itself, which matches the ticks along it, so it is also a solution (it is C=0C = 0 above).

Here is the by-eye recipe, and you should try it on the sim with the field and isoclines switched on:

  1. Start at the initial point and move along the tick there.
  2. At each isocline you cross, your slope must be that isocline’s mm.
  3. Cross the m=0m = 0 isocline horizontally. Where the sign of ff changes from minus to plus you have a minimum, and from plus to minus a maximum.
  4. Never cross another solution, and bend gradually: your slope changes continuously from one isocline to the next.

Worked sketch. Start at (0,1)(0, 1) in y′=t−yy' = t - y. The slope there is −1-1, so the curve heads down and to the right, flattening as it nears the line y=ty = t. It meets that line horizontally, at its minimum, and then turns upward. The exact solution y=t−1+2e−ty = t - 1 + 2e^{-t} confirms this: y=ty = t requires e−t=1/2e^{-t} = 1/2, so the minimum is at t=ln⁡2≈0.693t = \ln 2 \approx 0.693 with y≈0.693y \approx 0.693. After that, the 2e−t2e^{-t} term fades and the curve settles onto the straight-line solution y=t−1y = t - 1 from above and never crosses it.

✅ Concept check

For $y' = t - y$, along which curve are all the ticks horizontal?

Euler’s method: follow the arrow for time h

At (tn,yn)(t_n, y_n) the field gives the slope f(tn,yn)f(t_n, y_n). Move along that tick for a horizontal distance hh:

yn+1=yn+h f(tn,yn),tn+1=tn+h.y_{n+1} = y_n + h\,f(t_n, y_n), \qquad t_{n+1} = t_n + h .

That is the whole algorithm, and you coded it. The geometric reading is the tangent line: the first-order Taylor polynomial of the true solution at tnt_n. The numerical solution is a polygon, a chain of straight segments. Each segment follows the arrow at its start, but a real solution bends, so the arrow at the end of the segment has changed and the polygon drifts off the true curve. In the sim, raise hh and watch the polygon corners miss the exact curve. Where solutions curve upward, Euler lands below the curve (its tangent lies under a convex function); where they curve downward it lands above.

Worked example. Solve y′=t−yy' = t - y, y(0)=1y(0) = 1, on [0,1][0, 1] with h=0.5h = 0.5. The exact solution is y=t−1+2e−ty = t - 1 + 2e^{-t}, so y(1)=2/e≈0.7358y(1) = 2/e \approx 0.7358.

  • Step 1: slope at (0,1)(0, 1) is 0−1=−10 - 1 = -1, so y1=1+0.5(−1)=0.5y_1 = 1 + 0.5(-1) = 0.5 at t=0.5t = 0.5.
  • Step 2: slope at (0.5,0.5)(0.5, 0.5) is 00, so y2=0.5y_2 = 0.5 at t=1t = 1.

Euler gives 0.50.5 against the exact 0.73580.7358, an error of 0.2360.236. With h=0.25h = 0.25 the same hand computation gives y(1)≈0.6328y(1) \approx 0.6328, error 0.1030.103. Halving hh cut the error by a factor 2.32.3; we will see why it tends to 22.

Why the global error is first order

Taylor’s theorem for the exact solution gives, with y′′=ddtf(t,y(t))y'' = \dfrac{d}{dt}f(t, y(t)),

y(tn+h)=y(tn)+h y′(tn)+h22 y′′(ξ).y(t_n + h) = y(t_n) + h\,y'(t_n) + \tfrac{h^2}{2}\,y''(\xi) .

Euler keeps the first two terms, so a single step started on the exact curve misses by the third: the local truncation error is about h22 y′′\tfrac{h^2}{2}\,y'', which is O(h2)O(h^2). In the worked example, y′′=1−y′=2y'' = 1 - y' = 2 at (0,1)(0, 1), so the estimate is 0.252⋅2=0.25\tfrac{0.25}{2}\cdot 2 = 0.25; the true one-step miss at h=0.5h = 0.5 is 0.713−0.5=0.2130.713 - 0.5 = 0.213. The estimate is slightly high because y′′y'' shrinks across the step.

To reach t=Tt = T you need N=T/hN = T/h steps. If every step added an error of size h2h^2 and nothing amplified or removed it, the total would be Nh2=ThN h^2 = T h. That is the whole argument for global error O(h)O(h): one power of hh is lost when you add up 1/h1/h steps. The equation also reshapes each step’s error as it is carried forward (damped when solutions converge, amplified when they spread), which changes the constant in front of hh but not the power.

For y′=λyy' = \lambda y you can see the constant exactly. Euler gives yN=(1+hλ)T/h y0y_N = (1 + h\lambda)^{T/h}\,y_0. With ln⁡(1+x)=x−x2/2+⋯\ln(1 + x) = x - x^2/2 + \cdots,

(1+hλ)T/h=exp⁡ ⁣(Th(hλ−h2λ22+⋯))=eλT e−λ2hT/2+⋯,(1 + h\lambda)^{T/h} = \exp\!\Big(\tfrac{T}{h}\big(h\lambda - \tfrac{h^2\lambda^2}{2} + \cdots\big)\Big) = e^{\lambda T}\,e^{-\lambda^2 h T/2 + \cdots} ,

so the relative error is about −λ2hT/2-\lambda^2 h T/2. It is proportional to hh, as claimed. Check it: for λ=−1\lambda = -1, T=1T = 1, h=0.1h = 0.1, Euler gives 0.910=0.34870.9^{10} = 0.3487 against e−1=0.3679e^{-1} = 0.3679, a relative error of −0.052-0.052, and the formula says −0.050-0.050.

🐍 PythonError at t = 1 as h shrinks: the analytic solution beside Euler
⌘/Ctrl + Enter

Read the output: the ratio column heads toward 22 (2.292.29, 2.122.12, 2.062.06, 2.032.03 as hh halves) and the error / h column settles near 0.370.37. That constant is the price of first order.

✅ Concept check

Euler's error at $t = 1$ is $0.0384$ with $h = 0.1$ on this IVP. Predict the error with $h = 0.025$ assuming first-order behaviour.

Where Euler goes wrong

The error analysis above assumed two things: that the true solution exists and is well behaved on the whole interval, and that hh is small enough for the error to behave as a small perturbation. Each of those fails in an instructive example.

Too large a step: y’ = −10y

The exact solution of y′=−10yy' = -10y, y(0)=1y(0) = 1 is e−10te^{-10t}: positive and decaying monotonically. Euler gives

yn+1=yn−10h yn=(1−10h) yn,soyn=(1−10h)n.y_{n+1} = y_n - 10h\,y_n = (1 - 10h)\,y_n , \qquad\text{so}\qquad y_n = (1 - 10h)^n .

The factor 1−10h1 - 10h decides everything:

hhfactor 1−10h1 - 10hwhat Euler does
0.050.050.50.5decays smoothly
0.100.1000jumps to 00 in one step
0.150.15−0.5-0.5decays, but alternates in sign
0.200.20−1-1oscillates forever at constant size
0.250.25−1.5-1.5alternates and grows: −1.5,2.25,−3.375,…-1.5, 2.25, -3.375, \ldots

The mechanism is overshoot. The arrow at yny_n is steep and points at zero. For h<0.1h \lt 0.1 it stops short of zero. At h=0.1h = 0.1 it lands exactly on zero. Beyond that Euler walks past the equilibrium, to the other side, where the arrow now points back. A step has length 10h ∣yn∣10h\,|y_n| and the equilibrium is ∣yn∣|y_n| away, so once 10h>210h \gt 2 the step is more than twice the distance, and each landing is farther from zero than the last. So Euler is stable for h<2/10=0.2h \lt 2/10 = 0.2 and sign-faithful only for h≤0.1h \le 0.1. For general y′=−kyy' = -ky the limit is h<2/kh \lt 2/k.

This is a failure of stability, not of accuracy: it is not a matter of the O(h)O(h) error constant being large. The limit is set by the fastest decay rate kk in the problem, even when you only care about slow behaviour. This is the first appearance of stiffness, treated in 4.5 Stability Regions & Stiffness.

The solution itself ends: y’ = y²

The exact solution of y′=y2y' = y^2 from (0,0.5)(0, 0.5) is y=0.5/(1−0.5t)y = 0.5/(1 - 0.5t), which tends to infinity at t=2t = 2. Euler with h=0.5h = 0.5 gives

tt00.51.01.52.02.53.03.54.0
Euler0.50.6250.8201.1571.8263.4939.59255.591601

At t=2t = 2 the exact solution does not exist and Euler reports 1.831.83. Nothing in the algorithm can notice: each step evaluates ff at one finite point and adds a finite amount. Euler does not solve “the ODE on an interval”; it iterates a map, and the map is perfectly defined for every finite yny_n. It does not stay finite forever: the next values explode, passing 10001000 at t=4t = 4 and 10610^6 at t=4.5t = 4.5. But that is a blow-up at t≈4t \approx 4 to 4.54.5, not at 22.

Refining hh moves the numerical blow-up toward the truth. The step at which ∣y∣|y| first exceeds 10610^6 occurs at t=4.5t = 4.5 for h=0.5h = 0.5, 2.702.70 for h=0.1h = 0.1, 2.092.09 for h=0.01h = 0.01 and 2.012.01 for h=0.001h = 0.001. This does not contradict the first-order theory: convergence is a statement about intervals [0,T][0, T] with T<2T \lt 2, where the exact solution is bounded. The error constant (the size of y′′y'' and the growth of errors) grows without limit as T→2T \to 2.

🐍 PythonPlayground: y' = -10y at several h, and Euler on y' = y^2
⌘/Ctrl + Enter

Exercise: measure the order of convergence

Implement Euler for a scalar ODE and measure the error at t=1t = 1 for h=0.1,0.05,0.025,0.0125h = 0.1, 0.05, 0.025, 0.0125 on the IVP y′=t−yy' = t - y, y(0)=1y(0) = 1, whose exact value is 2/e2/e. If your implementation is first order, the error ratio between consecutive step sizes will be close to 22, and the slope of log⁡(error)\log(\text{error}) against log⁡h\log h will be close to 11.

🧪 Coding exerciseExercise: Euler, error at t = 1, and the observed order
⌘/Ctrl + Enter
✏️ Pen & paperIsoclines and a turning point●○○

For y′=t+yy' = t + y: (a) find the isocline of slope mm; (b) show that the isocline m=−1m = -1 is itself a solution; (c) find where solutions have horizontal tangents, and decide whether each is a minimum or a maximum.

Show worked solution

(a) t+y=mt + y = m gives the line y=m−ty = m - t.

(b) For m=−1m = -1 the line is y=−1−ty = -1 - t, with slope −1-1. The slope of the field along it is t+y=−1t + y = -1. The two agree at every point, so it is a solution. (Equivalently, y=−t−1+Cety = -t - 1 + Ce^t is the general solution, and C=0C = 0.)

(c) Horizontal tangents occur on m=0m = 0: the line y=−ty = -t. Differentiating y′=t+yy' = t + y gives y′′=1+y′y'' = 1 + y', which equals 1>01 \gt 0 where y′=0y' = 0. So every such point is a minimum.

✏️ Pen & paperEuler by hand and the ratio●●○

Apply Euler to y′=yy' = y, y(0)=1y(0) = 1, to estimate y(1)=ey(1) = e. Use h=0.5h = 0.5 and then h=0.25h = 0.25. Compute both errors and their ratio, and say why the ratio is not exactly 22.

Show worked solution

With h=0.5h = 0.5: y1=1.5y_1 = 1.5, y2=2.25y_2 = 2.25, error e−2.25=0.4683e - 2.25 = 0.4683.

With h=0.25h = 0.25: each step multiplies by 1.251.25, so y(1)=1.254=2.4414y(1) = 1.25^4 = 2.4414, error 0.27690.2769.

The ratio is 0.4683/0.2769=1.690.4683 / 0.2769 = 1.69. The error is c1h+c2h2+⋯c_1 h + c_2 h^2 + \cdots, and the h2h^2 term is still noticeable at h=0.5h = 0.5. The ratio tends to 22 as h→0h \to 0, as in the table above.

✏️ Pen & paperHow large a step can y' = -50y take?●●●

Euler is applied to y′=−50yy' = -50y, y(0)=1y(0) = 1. (a) Find the range of hh for which Euler is stable. (b) Find the range for which the numerical solution has the same sign at every step, as the exact one does. (c) For h=0.05h = 0.05, find the size of y10y_{10}. (d) Explain, in terms of the arrow, why the solution grows.

Show worked solution

Euler gives yn=(1−50h)ny_n = (1 - 50h)^n.

(a) Stability needs ∣1−50h∣<1|1 - 50h| \lt 1, which gives 0<h<0.040 \lt h \lt 0.04.

(b) Same sign needs 1−50h≥01 - 50h \ge 0, so h≤0.02h \le 0.02. At h=0.02h = 0.02 the factor is 00 and y1=0y_1 = 0 already.

(c) 1−50(0.05)=−1.51 - 50(0.05) = -1.5, so y10=(−1.5)10=57.7y_{10} = (-1.5)^{10} = 57.7. The exact value at t=0.5t = 0.5 is e−25≈1.4×10−11e^{-25} \approx 1.4\times 10^{-11}.

(d) The arrow at yny_n points at zero with slope −50yn-50y_n. Following it for h=0.05h = 0.05 moves a distance of 2.5 ∣yn∣2.5\,|y_n|, which is more than twice the distance to the equilibrium, so Euler lands on the other side at 1.51.5 times the distance it started from. Each step repeats this.

✅ Concept check

Euler is applied to $y' = -10y$, $y(0) = 1$, with $h = 0.25$. What is $y_2$, after two steps? (Give a number.)

Where this goes next

Euler’s method is the geometry of the direction field turned into an algorithm, and its first-order error is the accumulation of 1/h1/h steps that each miss by h2h^2. In 3.3 Separable Equations we stop sketching and start solving: for many equations the curves in the field can be written down exactly. 3.7 Existence & Uniqueness returns to the one thing the field makes visible and we have only assumed, that exactly one curve passes through each point and exists up to a stated time (the y′=y2y' = y^2 case shows why that needs care). The question of how step size, order and stability interact is taken up fully in 4.1 Local Error, Global Error & Order and 4.5 Stability Regions & Stiffness.

Key Takeaways

  • A direction field draws the slope f(t,y)f(t, y) at every grid point; solutions are exactly the curves tangent to it, and distinct solutions never cross.
  • Isoclines f(t,y)=mf(t, y) = m organize the drawing. The m=0m = 0 isocline marks the horizontal tangents, where solutions have maxima and minima.
  • Euler’s method follows the arrow at the start of a step for time hh: yn+1=yn+hf(tn,yn)y_{n+1} = y_n + h f(t_n, y_n). The numerical solution is a polygon of tangent segments.
  • The local error is h22 y′′\tfrac{h^2}{2}\,y'', O(h2)O(h^2); summing T/hT/h steps gives a global error O(h)O(h). Halving hh halves the error (for y′=t−yy' = t - y at t=1t = 1, the error was about 0.37 h0.37\,h).
  • For y′=−kyy' = -ky Euler multiplies by 1−kh1 - kh each step. It is stable only for h<2/kh \lt 2/k and sign-faithful only for h≤1/kh \le 1/k: a stability limit, set by the fastest rate in the problem, and independent of accuracy.
  • For y′=y2y' = y^2 Euler produces finite numbers past the true blow-up at t=2t = 2, because it iterates a map and never checks that a solution exists. Convergence only holds on intervals where the exact solution is bounded.
  • Advecting a particle through a velocity field is the same computation: field, path and Euler step.
Finished this lesson?

Mark it complete to track your progress (saved in this browser). Reading every section does this for you.

Where this leads

These later lessons build directly on this one: