BetaFree coursewareRewritten from MIT OpenCourseWareCC BY-NC-SA 4.0
Open CosmologyPhysics & cosmology, from high school to a graduate year

Euler's method and better numerical methods

Most differential equations are solved numerically. Euler's method follows the direction field in straight steps: from (xₙ, yₙ) go a step h along the line element of slope Aₙ = f(xₙ, yₙ), so xₙ₊₁ = xₙ + h and yₙ₊₁ = yₙ + hAₙ. Done by hand, keep a table. Where the solution curve is convex Euler's answer is too low, where concave too high, and y″ can be found from the equation itself without solving it. The error is roughly proportional to h: Euler is a first-order method, so halving the step halves the error. Heun's method (improved Euler, RK2) averages the slope at the start and at the Euler endpoint; its error goes as h². The standard Runge–Kutta method RK4 uses four slopes and has error ∝ h⁴. A pitfall: solutions can blow up at places the equation does not reveal, as y′ = y² shows.

You need this first

Full reading path (15 pages)
  1. The derivative: slope of the tangent line and rate of change (18.01)
  2. Limits and continuity (18.01)
  3. Derivatives of sine and cosine, and the product and quotient rules (18.01)
  4. The chain rule and higher derivatives (18.01)
  5. Implicit differentiation and the derivatives of inverse functions (18.01)
  6. Exponentials and logarithms: the number e and its derivatives (18.01)
  7. Linear and quadratic approximations (18.01)
  8. Curve sketching with the first and second derivative (18.01)
  9. Sketching strategy and maximum–minimum problems (18.01)
  10. More max–min, and related rates (18.01)
  11. A ring on a string, and Newton's method (18.01)
  12. The mean value theorem and inequalities (18.01)
  13. Differentials and antiderivatives (18.01)
  14. Differential equations and separation of variables (18.01)
  15. Direction fields, isoclines and integral curves (18.03)
On this page · 6 sections
  1. Euler's method
  2. Too high or too low?
  3. How the error depends on the step
  4. Better methods: Heun and Runge–Kutta
  5. A pitfall
  6. Summary

In real life most differential equations are solved numerically: even the solution curves a computer draws are computed point by point (transcript, p. 1). This lecture introduces the basic method, Euler's, of which the others are refinements.

Euler's method#

An initial value problem is a differential equation with a starting point (transcript, p. 1):

Initial value problem
y′=f(x,y),y(x0)=y0y' = f(x, y), \qquad y(x_0) = y_0
In words

y prime equals f(x, y), y( x nought ) equals y nought

At (x0,y0)(x_0, y_0) the only information is the line element of the direction field there, so start off in that direction, go a horizontal step hh, recompute the slope, and repeat. The result is a broken-line approximation to the solution (pp. 1–2). (Euler himself used the idea to prove that solutions exist, not to compute them, p. 1.)

From (xn,yn)(x_n, y_n), the line element has slope An=f(xn,yn)A_n = f(x_n, y_n); over a run of hh it rises by hAnhA_n (pp. 2–3):

Euler's method
xn+1=xn+h,yn+1=yn+hAn,An=f(xn,yn)x_{n+1} = x_n + h, \qquad y_{n+1} = y_n + hA_n, \qquad A_n = f(x_n, y_n)
In words

x sub n plus 1 equals x sub n plus h, y sub n plus 1 equals y sub n plus h A sub n, A sub n equals f( x sub n, y sub n )

Example. y′=x2−y2y' = x^2 - y^2, y(0)=1y(0) = 1, which cannot be solved with elementary functions, with h=0.1h = 0.1 (pp. 4–6). Be systematic and keep a table with columns nn, xnx_n, yny_n, AnA_n, hAnhA_n (p. 5):

nnxnx_nyny_nAn=xn2−yn2A_n = x_n^2 - y_n^2hAnhA_n
001−1-1−0.1-0.1
10.10.9−0.80-0.80−0.08-0.08
20.20.82

So Euler gives y(0.2)≈0.82y(0.2) \approx 0.82 (pp. 5–6).

Too high or too low?#

If the solution were a straight line, Euler would be exact. A convex solution curve bends up away from the tangent line Euler follows, so Euler falls too low, and keeps losing ground; a concave curve makes Euler too high (transcript, pp. 7–8). Convex means y′′>0y'' > 0, concave y′′<0y'' < 0, and y′′y'' can be found from the differential equation itself, without solving it (pp. 8–9). For the example, differentiating with the chain rule (p. 9):

Second derivative from the equation
y′′=2x−2yy′,y′′(0)=2(0)−2(1)(−1)=2y'' = 2x - 2yy', \qquad y''(0) = 2(0) - 2(1)(-1) = 2
In words

y double prime equals 2 x minus 2 y y prime, y double prime (0) equals 2(0) minus 2(1)(-1) equals 2

So the solution is convex at the start, and the Euler value 0.82 is too low (pp. 9–10). If the curve changes from convex to concave along the way, no prediction can be made (p. 10).

How the error depends on the step#

The error ee is the difference between the true value and Euler's. Smaller steps follow the curve better, bigger ones worse (transcript, pp. 10–11). Roughly, for small hh (pp. 11–12):

Euler is first order
e≈c he \approx c\,h
In words

e is approximately c h

for some constant cc: Euler's method is a first-order method. Halve the step size and you halve the error (p. 12). ("First order" refers to the power of hh, not the order of the equation.) Halving again and again until the answer stops changing works, but slowly (p. 13).

Better methods: Heun and Runge–Kutta#

Euler's slope AnA_n is systematically wrong when the curve bends. Improve it (transcript, pp. 13–15): take the Euler step to a temporary point y~n+1\tilde y_{n+1}, find the slope BnB_n there, and use the average of AnA_n and BnB_n for the real step:

Heun's method
y~n+1=yn+hAn,Bn=f(xn+1,y~n+1),yn+1=yn+h An+Bn2\tilde y_{n+1} = y_n + hA_n, \qquad B_n = f(x_{n+1}, \tilde y_{n+1}), \qquad y_{n+1} = y_n + h\,\frac{A_n + B_n}{2}
In words

y sub n plus 1 tilde equals y sub n plus h A sub n, B sub n equals f( x sub n plus 1, y sub n plus 1 tilde ), y sub n plus 1 equals y sub n plus h the quantity A sub n plus B sub n, all over 2

This is Heun's method, also called improved Euler, modified Euler, or RK2 (Runge–Kutta second order) (p. 16). It is a second-order method: e≈ch2e \approx ch^2, so halving the step quarters the error (p. 16).

What costs time is evaluating ff, which in practice can be a long formula: Euler evaluates it once per step, Heun twice (p. 16). The standard method, RK4 (Runge–Kutta fourth order), evaluates four slopes and combines them in a weighted average, divided by 6, the sum of the weights; its error goes as h4h^4, and it is what draws the solution curves in the course's computer visuals (pp. 17–18). Mattuck adds that higher order is not free, since more slopes per step means more work (p. 17). (He then says it hardly matters which method is used, comparing RK4 with halving Euler's step four times; but four halvings multiply Euler's work by 16, not 4, so for the same accuracy RK4 is far cheaper.)

y′=yy' = y, y(0)=1y(0) = 1, computed with two steps of h=0.5h = 0.5: Euler's broken line (slopes 1 and 1.5) falls well below the exact solution exe^x, which is convex; Heun's averaged slopes land much closer (2.64 against e≈2.72e \approx 2.72 at x=1x = 1).

A pitfall#

y′=y2y' = y^2 looks harmless, and separation of variables gives the solutions y=1/(c−x)y = 1/(c - x) (transcript, p. 18). Starting from y(0)=1y(0) = 1, the solution 1/(1−x)1/(1 - x) shoots off to infinity at x=1x = 1. Asked for y(2)y(2), a numerical method follows the curve up towards infinity and never finds the other branch (pp. 18–19). Each solution has its own singularity, at x=cx = c, and nothing in the equation predicts where: one of the things that makes numerical work hard (p. 19). (Separation of variables also misses the solution y=0y = 0. A first pitfall was left for the problem set, p. 18.)

Worked example

Euler and Heun on y′ = y

For y′=yy' = y, y(0)=1y(0) = 1, estimate y(1)y(1) (exactly e≈2.718e \approx 2.718): (a) by Euler with h=0.5h = 0.5 and h=0.25h = 0.25; (b) by Heun with h=0.5h = 0.5. Compare the errors.

(a) Euler

With h=0.5h = 0.5 each step multiplies yy by 1+h=1.51 + h = 1.5: y(1)≈1.52=2.25y(1) \approx 1.5^2 = 2.25, error 0.47. With h=0.25h = 0.25: 1.254≈2.4411.25^4 \approx 2.441, error 0.28. Halving hh cut the error by a factor of about 1.7; for smaller hh the factor approaches 2. Both are too low, as expected for a convex solution.

(b) Heun

An=ynA_n = y_n, y~n+1=yn(1+h)\tilde y_{n+1} = y_n(1 + h), Bn=yn(1+h)B_n = y_n(1 + h), so each step multiplies yy by 1+h+h2/2=1.6251 + h + h^2/2 = 1.625. Two steps: 1.6252≈2.6411.625^2 \approx 2.641, error 0.08, already better than Euler with twice as many steps.

Summary#

  • Euler: xn+1=xn+hx_{n+1} = x_n + h, yn+1=yn+hf(xn,yn)y_{n+1} = y_n + hf(x_n, y_n); keep a table.
  • Convex solution: Euler too low; concave: too high; y′′y'' comes from differentiating the equation.
  • Euler's error ≈ch\approx ch (first order); Heun/RK2 ≈ch2\approx ch^2; RK4 ≈ch4\approx ch^4, at the cost of more evaluations of ff.
  • Solutions can blow up at singularities that the equation does not reveal, such as y=1/(1−x)y = 1/(1 - x) for y′=y2y' = y^2.

Symbols on this page

Show the 19 symbols used here
≈\approx approximately equal.
Is approximately equal to. Say it: “is approximately”.
a~\tilde a tilde.
A modified version of a quantity (ã, k̃); its meaning is given where it is introduced. Say it: “tilde”.
cc c.
A constant (in the error estimate, or in the solutions 1/(c − x)). Say it: “c”.
xx x.
The independent variable. Say it: “x”.
yy y.
The unknown function. Say it: “y”.
nn n.
The step number. Say it: “n”.
ee e.
The error of the method (in e ≈ 2.718 and eˣ, Euler's number). Say it: “e”.
ff f.
The right-hand side of y′ = f(x, y): the slope of the direction field. Say it: “f”.
hh h.
The step size. Say it: “h”.
y′y' y prime.
The derivative dy/dx. Say it: “y prime”.
x0x_{0} x nought.
The starting value of x. Say it: “x nought”.
y0y_{0} y nought.
The starting value of y. Say it: “y nought”.
y′′y'' y double prime.
The second derivative of the solution. Say it: “y double prime”.
xn+1x_{n+1} x n plus 1.
The x value after step n + 1. Say it: “x sub n plus 1”.
xnx_{n} x n.
The x value after n steps. Say it: “x sub n”.
yny_{n} y n.
The Euler approximation to y after n steps. Say it: “y sub n”.
yn+1y_{n+1} y sub n plus 1.
The approximation to y after step n + 1. Say it: “y sub n plus 1”.
AnA_{n} A sub n.
The slope f(xₙ, yₙ) at the start of step n. Say it: “A sub n”.
BnB_{n} B sub n.
In Heun's method, the slope at the end of the Euler step. Say it: “B sub n”.

Tap or click a symbol in a formula to see the same explanation. All symbols: symbols and notation.

Words on this page

Show the 27 words used here

New here

initial value problem.
A differential equation together with the starting values that pick out one particular solution of it.
convex.
Of a curve: bending upwards, so that it lies above its tangent lines; its second derivative is positive.
concave.
Of a curve: bending downwards, so that it lies below its tangent lines; its second derivative is negative.
Euler's method.
The simplest numerical way to follow a differential equation: step forward along the current slope, find the new slope, and repeat.
Heun's method.
A numerical method that averages the slopes at the start of each step and at Euler's trial end point; also called improved Euler.
Runge–Kutta method.
A family of accurate numerical methods that sample the slope at several points in each step; RK4 is the standard fourth-order one.

Also used

differential equation.
An equation that links an unknown function to its derivatives; solving it means finding the function. Explained in Circular motion dynamics: worked problems.
p..
Page, in a reference to a source; pp. means pages, as in pp. 3–5.
line element.
A short line segment, drawn at a point, with the slope that a differential equation gives there. Explained in Direction fields, isoclines and integral curves.
direction field.
A picture of a differential equation: at many points, a short line with the slope the equation gives there. Explained in Direction fields, isoclines and integral curves.
tangent.
A straight line that touches a curve at a point and runs in the same direction as the curve there. Explained in Angles, radians and trigonometry.
differentiation.
Finding a derivative: working out how fast a function changes. Explained in The derivative: slope of the tangent line and rate of change.
chain rule.
The rule for the derivative of a function of a function: multiply the derivative of the outer one by that of the inner one. Explained in Circular motion: polar coordinates, position and velocity.
weighted average.
An average in which some values count more: each is multiplied by its weight, and the total divided by the sum of the weights. Explained in Centre of mass and its motion.
separation of variables.
A way to solve a differential equation by putting everything with one variable on one side, the rest on the other, and integrating both sides. Explained in The rocket equation.
singularity.
A point where a quantity becomes infinite or undefined, so the usual equations stop working there. Explained in Black holes.

Academic

numerically.
In terms of actual numbers rather than symbols; often worked out step by step, by computer, instead of from an exact formula.
transcript.
A written record of exactly what was said in a recorded lecture, word for word.
approximation.
Close to, but not exactly, the true value; an approximation is such a value or formula, chosen because it is simpler.
systematic.
Following a fixed method; of an error, one that pushes every result the same way, so averaging does not remove it.
error.
The difference between a measured or computed value and the true one; often how large that difference may be.
in practice.
In reality, as things are actually done or measured, rather than in theory.
accuracy.
Closeness to the true value: an accurate measurement or calculation gives a result near the right answer.
pitfall.
A hidden or easily made mistake.
problem set.
A weekly sheet of exercises that students work through and hand in, to practise what the lectures taught.
by a factor of.
Multiplied or divided by the stated number.
blow up.
In maths, to grow without limit; to become infinite.

Tap or click a word, here or in the text, for its meaning in this lesson. All words: the glossary. How many words are marked in the text: Reading options (the “Aa” button).

Where this leads

In cosmology

32 more cosmology pages that build on this

Pages that build on this

Practise

Check your understanding

  1. One Euler step from (xn,yn)(x_n, y_n) with step hh gives yn+1=y_{n+1} =…
  2. The solution curve is convex near the start. Euler's method gives values that are…
  3. Halving the step size with Heun's method (RK2) divides the error by about…
0 of 3 answered

Flashcards

1 / 3