Scientia sine finibus

Differential Equations / Unit 1, Differential Equations and Their Solutions / Topic 1.3

Units and Topics

Unit 1. Differential Equations and Their Solutions

1.3 The Method of Euler

The method of Euler turns a direction field into numbers. It starts at the initial condition and follows the slopes in short straight steps. The result is a table of approximate values of the solution. This topic covers the formula of the method, a complete example, the error of the method, and the choice of the step size.

You need Topics 1.1 and 1.2 for this topic.

1. The Idea of the Method

An initial value problem gives an equation and a starting point.

y′ = f(t, y) with y(t₀) = y₀

At the starting point (t₀, y₀), the slope of the solution is f(t₀, y₀). Over a short time h, the solution curve stays close to its tangent line. So the method moves along the tangent line for the time h and takes the point that it reaches as the next point of the solution.

t₁ = t₀ + hthe time grows by the step size hy₁ = y₀ + h f(t₀, y₀)the value changes by the slope times h

At the new point, the method calculates a new slope and takes the next step. The number h is called the step size.

The method of Euler. Starting from (t₀, y₀), repeat these two lines for n = 0, 1, 2, and so on.

yn+1 = yₙ + h f(tₙ, yₙ)the new valuetn+1 = tₙ + hthe new time

The number yₙ is an approximation of the exact value y(tₙ). The method needs only the equation and the starting point. It does not need a formula for the solution.

2. A Complete Example

Use the method with the step size h = 0.5 to find approximate values of the solution of y′ = t − y with y(0) = 1, up to the time t = 2. Here f(t, y) = t − y, t₀ = 0, and y₀ = 1.

Step 1, from t = 0 to t = 0.5

f(t₀, y₀) = 0 − 1 = −1the slope at the point (0, 1)y₁ = 1 + 0.5(−1) = 0.5the new valuet₁ = 0 + 0.5 = 0.5the new time

Step 2, from t = 0.5 to t = 1

f(t₁, y₁) = 0.5 − 0.5 = 0the slope at the point (0.5, 0.5)y₂ = 0.5 + 0.5(0) = 0.5the new valuet₂ = 0.5 + 0.5 = 1the new time

Step 3, from t = 1 to t = 1.5

f(t₂, y₂) = 1 − 0.5 = 0.5the slope at the point (1, 0.5)y₃ = 0.5 + 0.5(0.5) = 0.75the new valuet₃ = 1 + 0.5 = 1.5the new time

Step 4, from t = 1.5 to t = 2

f(t₃, y₃) = 1.5 − 0.75 = 0.75the slope at the point (1.5, 0.75)y₄ = 0.75 + 0.5(0.75) = 1.125the new valuet₄ = 1.5 + 0.5 = 2the new time

The result, and the exact solution

From Topic 1.2, the general solution of this equation is y = t − 1 + Ce−t. The initial condition gives the constant.

y(0) = 0 − 1 + C = C − 1substitute t = 0C − 1 = 1the initial condition y(0) = 1C = 2add 1 to both sidesy = t − 1 + 2e−tthe exact solution

The table compares the values of the method with the exact values, rounded to four decimal places.

ntₙyₙ from the methodExact value y(tₙ)Error
00110
10.50.50.71310.2131
210.50.73580.2358
31.50.750.94630.1963
421.1251.27070.1457

The values of the method follow the exact solution, but they are not accurate with this large step size.

Try it: the steps of the method of Euler

The purple curve is the exact solution with y(0) = 1. The green points are the values of the method of Euler, joined by straight lines. The menu selects the equation, and the slider sets the step size h.

3. The Step Size and the Error

A smaller step size keeps each step closer to the solution curve. The table shows the value of the method at t = 2 for the example of Part 2, with five step sizes. The exact value is 1.2707.

Step size hNumber of stepsValue at t = 2Error
0.541.12500.1457
0.2581.20020.0704
0.1201.24320.0275
0.05401.25700.0136
0.012001.26800.0027

The error at a fixed time is roughly proportional to the step size. When h is divided by 2, the error is divided by about 2. When h is divided by 10, the error is divided by about 10. A method with this property is called a first order method.

A smaller step size costs more work, because the number of steps is the length of the time interval divided by h. One more correct decimal place needs about ten times as many steps. Topic 7.1 studies the error of the method, and Topic 7.2 teaches methods that reach the same accuracy with far fewer steps.

Where the error comes from

  • In each step the method follows a straight line, and the solution curve bends away from it.
  • After the first step, the method starts from a point that is not on the exact solution curve, so it follows the direction field of a neighboring solution curve. The errors of the steps add up.

4. The Method for y′ = y, and the Number e

For the equation y′ = y with y(0) = 1, the exact solution is y = et. Here f(t, y) = y, and the steps of the method have a simple form.

yn+1 = yₙ + h yₙ = (1 + h)yₙeach step multiplies the value by 1 + hy₁ = (1 + h)y₀ = 1 + hthe first step, with y₀ = 1y₂ = (1 + h)y₁ = (1 + h)²the second stepyₙ = (1 + h)ⁿafter n steps

To reach the time t = 1 in n steps, the step size is h = 1/n. The value of the method at t = 1 is then (1 + 1/n)ⁿ, and the exact value is e.

Number of steps nStep size h(1 + 1/n)ⁿ
100.12.5937
1000.012.7048
10000.0012.7169

The values approach e = 2.7183 as n grows. This is the limit that defines the number e in calculus, and the method of Euler produces it.

5. A Step Size That Is Too Large

For some equations, a large step size gives values that do not follow the solution at all. The equation y′ = −10y with y(0) = 1 has the exact solution y = e−10t, which decreases quickly toward 0. The steps of the method are

yn+1 = yₙ + h(−10yₙ)the formula of the method, with f(t, y) = −10yyn+1 = (1 − 10h)yₙeach step multiplies the value by 1 − 10h
Step size hFactor 1 − 10hValues y₀, y₁, y₂, y₃Behavior
0.050.51, 0.5, 0.25, 0.125Decreases toward 0, like the solution
0.25−1.51, −1.5, 2.25, −3.375Changes its sign in each step and grows

With h = 0.25, the tangent line at the first point reaches far below zero before the step ends, and each later step makes the error larger. The values grow without limit, and the solution goes to 0. The method is called unstable for this step size. When the values of the method change their sign in each step or grow quickly, repeat the calculation with a smaller step size and compare the results.

6. Using the Method

  • Write the equation in the form y′ = f(t, y), and note the starting point.
  • Choose a step size, and repeat the two lines of the method up to the time that is needed.
  • Repeat the whole calculation with one half of the step size. If the two results agree to the accuracy that is needed, accept them. If they do not agree, halve the step size again.

By hand, the method is practical for a few steps. A computer takes thousands of steps in a fraction of a second, and the Python Practice of this topic shows how. Numerical methods of this kind are the usual way to solve the differential equations of science and engineering, because most of them have no solution formula.

Common Mistakes

  • Using the slope at the new point in a step. The slope of a step is calculated at the point where the step starts.
  • Updating the time before the slope is calculated, so that the slope uses the new time and the old value.
  • Multiplying the slope by the time t in the place of the step size h.
  • Treating the values of the method as exact values.
  • Trusting one calculation with one step size. Compare it with a calculation with a smaller step size.
  • Rounding the values strongly after each step. The rounding errors add up over many steps.

Summary

IdeaStatement
One stepyn+1 = yₙ + h f(tₙ, yₙ) and tn+1 = tₙ + h
Step sizeThe time h between two points of the method
ErrorRoughly proportional to h at a fixed time
CostThe number of steps is the length of the interval divided by h
y′ = yThe method gives (1 + h)ⁿ, which approaches et
InstabilityA step size that is too large can give values that grow while the solution decreases

Practice

Work each problem on paper first. Then open the answer to check your work.

1. Take one step of the method for y′ = 2y with y(0) = 3 and h = 0.1.

f(t₀, y₀) = 2(3) = 6the slope at the starty₁ = 3 + 0.1(6) = 3.6the new valuet₁ = 0 + 0.1 = 0.1the new time

2. Take two steps of the method for y′ = t + y with y(0) = 1 and h = 0.5.

f(t₀, y₀) = 0 + 1 = 1the slope at the point (0, 1)y₁ = 1 + 0.5(1) = 1.5the first new valuet₁ = 0.5the first new timef(t₁, y₁) = 0.5 + 1.5 = 2the slope at the point (0.5, 1.5)y₂ = 1.5 + 0.5(2) = 2.5the second new valuet₂ = 1the second new time

3. Take three steps of the method for y′ = −y with y(0) = 8 and h = 0.5.

yn+1 = yₙ + 0.5(−yₙ) = 0.5yₙeach step multiplies the value by 0.5y₁ = 0.5(8) = 4at t = 0.5y₂ = 0.5(4) = 2at t = 1y₃ = 0.5(2) = 1at t = 1.5

The exact value is 8e−1.5, which is about 1.785.

4. With h = 0.1 the method has the error 0.03 at a certain time. Estimate the error at the same time with h = 0.02, and compare the numbers of steps.

0.1/0.02 = 5the step size is divided by 50.03/5 = 0.006the error is divided by about 5

The calculation with h = 0.02 needs 5 times as many steps.

5. For the equation y′ = −4y, find the factor by which the method multiplies the value in each step with h = 0.1 and with h = 0.6. Describe the values in each case.

yn+1 = yₙ + h(−4yₙ) = (1 − 4h)yₙthe factor is 1 − 4h1 − 4(0.1) = 0.6the factor for h = 0.11 − 4(0.6) = −1.4the factor for h = 0.6

With h = 0.1 the values decrease toward 0, like the solution. With h = 0.6 the values change their sign in each step and grow, so the method is unstable.

6. A falling object has the equation dv/dt = 9.8 − 0.2v with v(0) = 0. Take two steps of the method with h = 1 second.

f(0, 0) = 9.8 − 0.2(0) = 9.8the acceleration at the startv₁ = 0 + 1(9.8) = 9.8the velocity after 1 secondf(1, 9.8) = 9.8 − 0.2(9.8) = 7.84the acceleration at the new pointv₂ = 9.8 + 1(7.84) = 17.64the velocity after 2 seconds

The values are in meters per second. The acceleration decreases as the velocity grows, as the direction field of Topic 1.2 shows.

Python Practice

Step 1. One step of the method

The code below takes the first step of Part 2. The line y = y + h * slope is not an equation. Python first calculates the right side with the old value of y, and then it stores the result under the name y. After this line, y holds the new value. The line for t works in the same way.

Python

The result is the point (0.5, 0.5) of Part 2. Now change the step size to 0.1. The new point should be (0.1, 0.9).

Python

Step 2. Many steps with a loop

A loop repeats lines of code. The line for n in range(4): says that the lines under it are run 4 times. The lines that belong to the loop are indented, which means that they start with four spaces. The code below takes the four steps of Part 2 and prints the point after each step.

Python

The four lines of the result are the rows of the table in Part 2. The last value is 1.125. Now reach the time t = 2 with the step size 0.25, which needs 8 steps. The last value should be about 1.2002.

Python

Step 3. The error

The exact solution of the example is y = t − 1 + 2e−t. The code below stores the number of steps under the name steps and calculates the step size from it. The print instruction is outside the loop, because it is not indented, so it runs once after the last step. It shows the value of the method at t = 2, the exact value, and the error.

Python

The error is about 0.1457, as in Part 3. Now use 200 steps. The step size is then 0.01, and the error should be about 0.0027.

Python

Step 4. The error for several step sizes

A loop can contain another loop. In the code below, the outer loop takes the numbers of steps 4, 8, 16, and 32 from a list, one after the other. For each number, the inner loop does the whole calculation of Step 3. The lines of the inner loop are indented twice. After each calculation, the code prints the number of steps, the step size, and the error.

Python

Each line has twice as many steps as the line before it, and about one half of its error. Now add the number 64 at the end of the list. The new last line should show an error of about 0.0085.

Python

Step 5. The number e

The code below uses the method for y′ = y with y(0) = 1 and reaches the time t = 1 in 10 steps, as in Part 4. It prints the value of the method and the exact value e, which is typed as np.exp(1).

Python

The value of the method is 2.5937, and e is 2.7183. Now use 1000 steps. The value should be about 2.7169.

Python

Step 6. A step size that is too large

The code below takes six steps for the equation y′ = −10y of Part 5 with the step size 0.25, and it prints the value after each step.

Python

The values change their sign in each step and grow, although the exact solution decreases toward 0. Now change the step size to 0.05. The values should be 0.5, 0.25, 0.125, and so on.

Python

Quiz

This quiz has 5 questions on this topic. A question asks you either to choose one answer or to type numbers. The page grades your answers and explains each one. A number can be typed as a whole number, a decimal, a fraction such as 1/3, or a calculation such as sqrt(18)/2 or arccos(1/2). The page shows the value of a calculation next to the box. Angles in a calculation are in degrees. The Calculator button under each question opens a calculator. Your answers are saved in your browser, so they are still on the page when you come back to it.

Choose how your answers are checked

1. Take one step of the method of Euler for y′ = 2y with y(0) = 1 and h = 0.1. Find y₁.

2. Take two steps of the method of Euler for y′ = t + y with y(0) = 1 and h = 0.5. Find y₂.

3. The step size of the method of Euler is divided by 2. What happens to the error at a fixed time?

4. Along which line does one step of the method of Euler move?

5. The method of Euler is used for y′ = −10y with y(0) = 1 and h = 0.25. How do the values behave?

Next: Topic 2.1, Separable Equations