# Euler s Numerical Method

## Transcript Of Euler s Numerical Method

9

Euler’s Numerical Method

In the last chapter, we saw that a computer can easily generate a slope ﬁeld for a given ﬁrst-order differential equation. Using that slope ﬁeld we can sketch a fair approximation to the graph of the solution y to a given initial-value problem, and then, from that graph, we ﬁnd ﬁnd an approximation to y(x) for any desired x in the region of the sketched slope ﬁeld. The obvious question now arises: Why not let the computer do all the work and just tell us the approximate value of y(x) for the desired x ?

Well, why not? In this chapter, we will develop, use, and analyze one method for generating a “numerical solution” to a ﬁrst-order differential equation. This type of “solution” is not a formula or equation for the actual solution y(x) , but two lists of numbers,

{ x0 , x1 , x2 , x3 , . . . , xN } and { y0 , y1 , y2 , y3 , . . . , yN } with each yk approximating the value of y(xk) . Obviously, a nice formula or equation for y(x) would be usually be preferred over a list of approximate values, but, when obtaining that nice formula or equation is not practical, a numerical solution is better than nothing.

The method we will study in this chapter is “Euler’s method”. It is but one of many methods for generating numerical solutions to differential equations. We choose it as the ﬁrst numerical method to study because is relatively simple, and, using it, you will be able to see many of the advantages and the disadvantages of numerical solutions. Besides, most of the other methods that might be discussed are reﬁnements of Euler’s method, so we might as well learn this method ﬁrst.

9.1 Deriving the Steps of the Method

Euler’s method is based on approximating the graph of a solution y(x) with a sequence of tangent line approximations computed sequentially, in “steps”. Our ﬁrst task, then, is to derive a useful formula for the tangent line approximation in each step.

191

192

Euler’s Numerical Method

Y y(xk + x) y(xk) + y

y(xk)

y(x)

Y

Lk

(x5, y5) L5

y

xk

x

(a)

X xk + x

L1 y0 x0

L2 (x1, y1)

L4 (x4, y4) L3 (x3, y3) (x2, y2)

X

(b)

Figure 9.1: (a) A single tangent line approximation for the Euler method, and (b) the approximation of the solution curve generated by ﬁve steps of Euler’s method.

The Basic Step Approximation

Let y = y(x) be the desired solution to some ﬁrst-order differential equation

dy = f (x, y) , dx

and let xk be some value for x on the interval of interest. As illustrated in ﬁgure 9.1a, (xk, y(xk)) is a point on the graph of y = y(x) , and the nearby points on this graph can be approximated by corresponding points on the straight line tangent at point (xk, y(xk)) (line Lk in ﬁgure 9.1a). As with the slope lines in the last chapter, the differential equation can give us the slope of this line:

the slope of the approximating line = dy at (xk, y(xk)) = f (xk, y(xk)) . dx

Now let x be any positive distance in the X direction. Using our tangent line approximation (again, see ﬁgure 9.1a), we have that

where So,

y(xk + x) ≈ y(xk) + y y = slope of the approximating line = f (xk, y(xk)) . x

y = x · f (xk, y(xk))

and

y(xk + x) ≈ y(xk) + x · f (xk, y(xk)) .

(9.1)

Approximation (9.1) is the fundamental approximation underlying each basic step of Euler’s method. However, in what follows, the value of y(xk) will usually only be known by some approximation yk . With this approximation, we have

y(xk) + x · f (xk, y(xk)) ≈ yk + x · f (xk, yk) ,

which, combined with approximation (9.1), yields the approximation that will actually be used

in Euler’s method,

y(xk + x) ≈ yk + x · f (xk, yk) .

(9.2)

The distance x in the above approximations is called the step size. We will see that choosing a good value for the step size is important.

Computing Via Euler’s Method (Illustrated)

193

Generating the Numerical Solution (Generalities)

Euler’s method is used to solve ﬁrst-order initial-value problems. We start with the point (x0, y0) where y0 = y(x0) is the initial data for the initial-value problem to be solved. Then, repeatedly increasing x by some positive value x , and computing corresponding values of y using a formula based on approximation (9.2), we will obtain those two sequences

{ x0 , x1 , x2 , x3 , . . . , xN } and { y0 , y1 , y2 , y3 , . . . , yN }

with yk ≈ y(xk) for each k . Plotting the (xk, yk) points, and connecting the resulting dots with short straight lines leads to a piecewise straight approximation to the graph of the solution y(x) as illustrated in ﬁgure 9.1b. For convenience, let us denote this approximation generated by the Euler method by yE, x .

As already indicated, N will denote the number of steps taken. It must be chosen along with x to ensure that xN is the maximum value of x of interest. In theory, both N and the maximum value of x can be inﬁnite. In practice, they must be ﬁnite.

The precise steps of Euler’s method are outlined and illustrated in the next section.

9.2 Computing Via Euler’s Method (Illustrated)

Suppose we wish to ﬁnd a numerical solution to some ﬁrst-order differential equation with initial data y(x0) = y0 , say,

5 dy − y2 = −x2 with y(0) = 1 .

(9.3)

dx

(As it turns out, this differential equation is not easily solved by any of the methods already discussed. So if we want to ﬁnd the value of, say, y(3) , then a numerical method may be our only choice.)

To use Euler’s method to ﬁnd our numerical solution, we follow the steps given below. These steps are grouped into two parts: the main part in which the values of the xk’s and yk’s are iteratively computed, and the preliminary part in which the constants and formulas for those iterative computations are determined.

The Steps in Euler’s Method

Part I (Preliminaries)

1. Get the differential equation into derivative formula form, dy = f (x, y) . dx

For our example, solving for the derivative formula form yields dy = 1 y2 − x2 .

dx 5 2. Set x0 and y0 equal to the x and y values of the initial data.

194

Euler’s Numerical Method

In our example, the initial data is y(0) = 1 . So

x0 = 0 and y0 = 1 .

3. Pick a distance x for the step size, a positive integer N for the maximum number of steps, and a maximum value desired for x , xmax . These quantities should be chosen so that xmax = x0 + N x .

Of course, you only choose two of these values, and compute the third. Which two are chosen depends on the problem.

For no good reason whatsoever, let us pick

x = 1 and N = 6 .

2

Then

xmax = x0 + N x = 0 + 6 · 1 = 3 .

2

4. Write out the equations

xk+1 = xk + x

(9.4a)

and

yk+1 = yk + x · f (xk, yk)

(9.4b)

using the information from the previous steps. For our example,

f (x, y) = 1 y2 − x2

and

5

So, for our example, equation set (9.4) becomes

x=1 .

2

xk+1 = xk + 1 2

(9.4a ′)

and yk+1 = yk + 1 · 1 y2 − x 2

25

= yk + 1 yk 2 − xk 2 .

10

(9.4b ′)

Formula (9.4b) for yk+1 is based on approximation (9.2). According to that approximation, if y(x) is the solution to our initial-value problem and yk ≈ y(xk) , then

y(xk+1) = y(xk + x) ≈ yk + x · f (xk, yk ) = yk+1 .

Because of this, each yk generated by Euler’s method is an approximation of y(xk) .

Computing Via Euler’s Method (Illustrated)

195

Part II of Euler’s Method (Iterative Computations)

1. Compute x1 and y1 using equation set (9.4) with k = 0 and the values of x0 and y0 from the initial data. For our example, using equation set (9.4 ′) with k = 0 and the initial values x0 = 0 and y0 = 1 gives us

x1 = x0+1 = x0 +

x =0+ 1 = 1 ,

22

and y1 = y0+1 = y0 + x · f (x0, y0)

= y0 + 1 y02 − x02 10

= 1 + 1 12 − 02 = 11 .

10

10

2. Compute x2 and y2 using equation set (9.4) with k = 1 and the values of x1 and y1 from the previous step.

For our example, equation set (9.4 ′) with k = 1 and the above values for x1 and y1 yields

x2 = x1+1 = x1 +

x= 1+1 =1 ,

22

and y2 = y1+1 = y1 + x · f (x1, y1)

= y1 + 1 y12 − x12 10

= 11 + 1 10 10

11 2 1 2

290

−

=

.

10

2

250

3. Compute x3 and y3 using equation set (9.4) with k = 2 and the values of x2 and y2 from the previous step.

For our example, equation set (9.4 ′) with k = 2 and the above values for x2 and y2 yields

x3 = x2+1 = x2 +

x =1+ 1 = 3 ,

22

and y3 = y2+1 = y2 + 1 y22 − x22

10

= 29 + 1 250 10

29

2

− 12

= 774,401

.

250

625,000

For future convenience, note that y3 = 774,401 ≈ 1.2390 . 625,000

196

Euler’s Numerical Method

(d), (e), … In each subsequent step, increase k by 1 , and compute xk+1 and yk+1 using equation set (9.4) with the values of xk and yk from the previous step. Continue until xN and yN are computed.

For our example (omitting many computational details): With k + 1 = 4 ,

x4 = x3+1 = x3 +

x= 3+1 =2 ,

22

and y4 = y3+1 = y2 + 1 y32 − x32 = · · · ≈ 1.1676 .

10

With k + 1 = 5 ,

x5 = x4+1 = x4 +

x =2+ 1 = 5 ,

22

and y5 = y4+1 = y4 + 1 y42 − x42 = · · · ≈ 0.9039 .

10

With k + 1 = 6 ,

x6 = x5+1 = x5 +

x= 5+1 =6 ,

22

and y6 = y5+1 = y5 + 1 y52 − x52 = · · · ≈ 0.3606 .

10

Since we had earlier chosen N , the maximum number of steps, to be 6 , we can stop computing.

Using the Results of the Method

What you do with the results of your computations in depends on why you are doing these computations. If N is not too large, it is usually a good idea to write the obtained values of

{ x0 , x1 , x2 , x3 , . . . , xN } and { y0 , y1 , y2 , y3 , . . . , yN }

in a table for convenient reference (with a note that yk ≈ y(xk) for each k ) as done in ﬁgure 9.2a for our example. And, whatever the size of N , it is always enlightening to graph — as done in ﬁgure 9.2b for our example — the corresponding piecewise straight approximation y = yE, x (x) to the graph of y = y(x) by drawing straight lines between each (xk, yk) and (xk+1, yk+1) .

On Doing the Computations

The ﬁrst few times you use Euler’s method, attempt to do all the computations by hand. If the numbers become too awkward to handle, use a simple calculator and decimal approximations. This will help you understand and appreciate the method. It will also help you appreciate the tremendous value of programming a computer to do the calculations in the second part of the

What Can Go Wrong

197

k xk

yk

00

1

1 0.5 1.1000

2 1.0 1.1960

3 1.5 1.2390

4 2.0 1.1676

5 2.5 0.9039

6 3.0 0.3606

Y 1.0 0.5

0 0 0.5 1.0 1.5 2.0 2.5 3.0 X

(a)

(b)

Figure 9.2:

Results of Euler’s method to solve 5y′ − y2 = −x2 with y(0) = 1 using x = 1/2 and N = 6 : (a) The numerical solution in which yk ≈ y(xk) (for

k ≥ 3 , the values of yk are to the nearest 0.0001 ). (b) The graph of the corresponding approximate solution y = yE, x (x) .

method. That, of course, is how one should really carry out the computations in the second part of Euler’s method.

In fact, Euler’s method may already be one of the standard procedures in your favorite computer math package. Still, writing your own version is enlightening, and is highly recommended for the good of your soul.

9.3 What Can Go Wrong

Do not forget that Euler’s method does not yield exact answers. Instead, it yields values

{ x0 , x1 , x2 , x3 , . . . , xN } and { y0 , y1 , y2 , y3 , . . . , yN }

with yk ≈ y(xk) for k > 0 .

What’s more, each yk+1 is based on the approximation

y(xk + x) ≈ y(xk) + x · f (xk, y(xk))

with y(xk) being replaced with approximation yk when k > 0 . So we are computing approximations based on previous approximations.

Because of this, the accuracy of the approximation yk ≈ y(xk) , especially for larger values of k , is a serious issue. Consider the work done in the previous section: Just how well can we trust the approximation

y(3) ≈ 0.3606 obtained for the solution to initial-value problem (9.3)? In fact, it can be shown that

y(3) = −.23699 to the nearest 0.00001 .

198

Euler’s Numerical Method

Y

Y

4

4

3

approx. soln.

3

2

2

true soln.

1

1

0

0

0

1

2

3

4X

0

1

2

3

4X

−1

−1

(a)

(b)

Figure 9.3:

Catastrophic failure of Euler’s method in solving y′ = (y − 1)2 with y(0) = −1.3 : (a) Graphs of the true solution and the approximate solution. (b)

Same graphs with a slope ﬁeld, the graph of the equilibrium solution, and the graph of the true solution to y′ = (y − 1)2 with y(x1) = y1 .

So our approximation is not very good! To get an idea of how the errors can build up, look back at ﬁgure 9.1a on page 192. You can see

that, if the graphs of the true solutions to the differential equation are generally concave up (as in the ﬁgure), then the tangent line approximations used in Euler’s method lie below the true graphs, and yield underestimates for the approximations. Over several steps, these underestimates can build up so that the yk’s are signiﬁcantly below the actual values of the y(xk)’s .

Likewise, if the graphs of the true solutions are generally concave down, then the tangent line approximations used in Euler’s method lie above the true graphs, and yield overestimates for the approximations.

Also keep in mind that most of the tangent line approximations used in Euler’s method are not based on lines tangent to the true solution, but on lines tangent to solution curves passing through the (xk, yk)’s . This can lead to the “catastrophic failure” illustrated in ﬁgure 9.3a. In this ﬁgure, the true solution to

dy = (y − 1)2 dx

with y(0) = − 13 , 10

is graphed along with the graph of the approximate solution generated from Euler’s method with x = 1/2 . Exactly why the graphs appear so different becomes apparent when we superimpose

the slope ﬁeld in ﬁgure 9.3b. The differential equation has an unstable equilibrium solution y = 1 . If y(0) < 1 , as in the above initial-value problem, then the true solution y(x) should converge to 1 as x → ∞ . Here, however, one step of Euler’s method overestimated the value of y1 enough that (x1, y1) ended up above equilibrium and in the region where the solutions diverge away from the equilibrium. The tangent lines to these solutions led to higher and higher values for the subsequently computed yk’s . Thus, instead of correctly telling us that

lim y(x) = 1 ,

x →∞

Reducing the Error

199

Y y(xmax)

y = y(x)

Y y(xmax)

y = y(x)

yN yN

y0

x0 (a)

X xmax

y = yˆ(x) X

x0

xmax

(b)

Figure 9.4:

Two approximations yN of y(xmax) where y is the solution to y′ = f (x, y) with y(x0) = y0 : (a) Using Euler’s method with x equaling the distance from x0 to xmax . (b) Using Euler’s method with x equaling half the distance from x0 to xmax (Note: yˆ is the solution to y′ = f (x, y) with y(x1) = y1 .)

this application of Euler’s method suggests that

lim y(x) = ∞ .

x →∞

A few other situations where blindly applying Euler’s method can lead to misleading results are illustrated in the exercises (see exercises 9.6, 9.7, and 9.8, 9.9). And these sorts of problems are not unique to Euler’s method. Similar problems can occur with all numerical methods for solving differential equations. Because of this, it is highly recommended that Euler’s method (or any other numerical method) be used only as a last resort. Try the methods developed in the previous chapters ﬁrst. Use a numerical method only if the other methods fail to yield usable formulas or equations.

Unfortunately, the world is ﬁlled with ﬁrst-order differential equations for which numerical methods are the only practical choices. So be sure to skim the next section on improving the method. Also, if you must use Euler’s method (or any other numerical method), be sure to do a reality check. Graph the corresponding approximation on top of the slope ﬁeld for the differential equation, and ask yourself if the approximations are reasonable. In particular, watch out that your numerical solution does not “jump” over an unstable equilibrium solution.

9.4 Reducing the Error

Smaller Step Sizes

Suppose we are applying Euler’s method to a given initial-value problem over some interval [x0, xmax] . The one parameter we can adjust is the step size, x (or, equivalently, the number of steps, N , in going from x0 to xmax ). By shrinking x (increasing N ), at least two good things are typically accomplished:

1. The error in the underlying approximation

y(xk + x) ≈ y(xk) + x · f (xk, y(xk))

is reduced.

200

Euler’s Numerical Method

Y x =1

1.0

0.5

0 0

x=1 2

true solution

x=1 4

x=1 8

0.5

1.0

1.5

2.0

2.5

3.0 X

Figure 9.5: Graphs of the different piecewise straight line approximations of the solution to 5y′ − y2 = −x2 with y(0) = 1 obtained by using Euler’s method with

different values for the step size x = 1/2 . Also graphed is the true solution.

2. The slope in the piecewise straight approximation y = yE, x (x) is recomputed at more points, which means that this approximation can better match the bends in the slope ﬁeld for the differential equation.

Both of these are illustrated in ﬁgure 9.4. Accordingly, we should expect that shrinking the step size in Euler’s method will yield

numerical solutions that more accurately approximate the true solution. We can experimentally test this expectation by going back to our initial-value problem

5 dy − y2 = −x2 dx

with y(0) = 1 ,

computing (as you’ll be doing for exercise 9.5) the numerical solutions arising from Euler’s method using, say,

x=1 ,

x=1 , 2

x = 1 and 4

x=1 , 8

and then graphing the corresponding piecewise straight approximations over the interval [0, 3]

along with the graph of the true solution. Do this, and you will get the graphs in ﬁgure 9.5.1 As

expected, the graphs of the approximate solutions steadily approach the graph of the true solution

as x gets smaller. It’s even worth observing that the distance between the true value for y(3)

and the approximated value appears to be cut roughly in half each time x is cut in half.

In fact, our expectations can be rigorously conﬁrmed. In the next section, we will analyze

the error in using Euler’s method to approximate y(xmax) where y is the solution to a ﬁrst-order

initial-value problem

dy = f (x, y) dx

with y(x0) = y0 .

1 The graph of the “true solution” in ﬁgure 9.5 is actually the graph of a very accurate approximation. The difference between this graph and the graph of the true solution is less than the thickness of the curve used to sketch it.

Euler’s Numerical Method

In the last chapter, we saw that a computer can easily generate a slope ﬁeld for a given ﬁrst-order differential equation. Using that slope ﬁeld we can sketch a fair approximation to the graph of the solution y to a given initial-value problem, and then, from that graph, we ﬁnd ﬁnd an approximation to y(x) for any desired x in the region of the sketched slope ﬁeld. The obvious question now arises: Why not let the computer do all the work and just tell us the approximate value of y(x) for the desired x ?

Well, why not? In this chapter, we will develop, use, and analyze one method for generating a “numerical solution” to a ﬁrst-order differential equation. This type of “solution” is not a formula or equation for the actual solution y(x) , but two lists of numbers,

{ x0 , x1 , x2 , x3 , . . . , xN } and { y0 , y1 , y2 , y3 , . . . , yN } with each yk approximating the value of y(xk) . Obviously, a nice formula or equation for y(x) would be usually be preferred over a list of approximate values, but, when obtaining that nice formula or equation is not practical, a numerical solution is better than nothing.

The method we will study in this chapter is “Euler’s method”. It is but one of many methods for generating numerical solutions to differential equations. We choose it as the ﬁrst numerical method to study because is relatively simple, and, using it, you will be able to see many of the advantages and the disadvantages of numerical solutions. Besides, most of the other methods that might be discussed are reﬁnements of Euler’s method, so we might as well learn this method ﬁrst.

9.1 Deriving the Steps of the Method

Euler’s method is based on approximating the graph of a solution y(x) with a sequence of tangent line approximations computed sequentially, in “steps”. Our ﬁrst task, then, is to derive a useful formula for the tangent line approximation in each step.

191

192

Euler’s Numerical Method

Y y(xk + x) y(xk) + y

y(xk)

y(x)

Y

Lk

(x5, y5) L5

y

xk

x

(a)

X xk + x

L1 y0 x0

L2 (x1, y1)

L4 (x4, y4) L3 (x3, y3) (x2, y2)

X

(b)

Figure 9.1: (a) A single tangent line approximation for the Euler method, and (b) the approximation of the solution curve generated by ﬁve steps of Euler’s method.

The Basic Step Approximation

Let y = y(x) be the desired solution to some ﬁrst-order differential equation

dy = f (x, y) , dx

and let xk be some value for x on the interval of interest. As illustrated in ﬁgure 9.1a, (xk, y(xk)) is a point on the graph of y = y(x) , and the nearby points on this graph can be approximated by corresponding points on the straight line tangent at point (xk, y(xk)) (line Lk in ﬁgure 9.1a). As with the slope lines in the last chapter, the differential equation can give us the slope of this line:

the slope of the approximating line = dy at (xk, y(xk)) = f (xk, y(xk)) . dx

Now let x be any positive distance in the X direction. Using our tangent line approximation (again, see ﬁgure 9.1a), we have that

where So,

y(xk + x) ≈ y(xk) + y y = slope of the approximating line = f (xk, y(xk)) . x

y = x · f (xk, y(xk))

and

y(xk + x) ≈ y(xk) + x · f (xk, y(xk)) .

(9.1)

Approximation (9.1) is the fundamental approximation underlying each basic step of Euler’s method. However, in what follows, the value of y(xk) will usually only be known by some approximation yk . With this approximation, we have

y(xk) + x · f (xk, y(xk)) ≈ yk + x · f (xk, yk) ,

which, combined with approximation (9.1), yields the approximation that will actually be used

in Euler’s method,

y(xk + x) ≈ yk + x · f (xk, yk) .

(9.2)

The distance x in the above approximations is called the step size. We will see that choosing a good value for the step size is important.

Computing Via Euler’s Method (Illustrated)

193

Generating the Numerical Solution (Generalities)

Euler’s method is used to solve ﬁrst-order initial-value problems. We start with the point (x0, y0) where y0 = y(x0) is the initial data for the initial-value problem to be solved. Then, repeatedly increasing x by some positive value x , and computing corresponding values of y using a formula based on approximation (9.2), we will obtain those two sequences

{ x0 , x1 , x2 , x3 , . . . , xN } and { y0 , y1 , y2 , y3 , . . . , yN }

with yk ≈ y(xk) for each k . Plotting the (xk, yk) points, and connecting the resulting dots with short straight lines leads to a piecewise straight approximation to the graph of the solution y(x) as illustrated in ﬁgure 9.1b. For convenience, let us denote this approximation generated by the Euler method by yE, x .

As already indicated, N will denote the number of steps taken. It must be chosen along with x to ensure that xN is the maximum value of x of interest. In theory, both N and the maximum value of x can be inﬁnite. In practice, they must be ﬁnite.

The precise steps of Euler’s method are outlined and illustrated in the next section.

9.2 Computing Via Euler’s Method (Illustrated)

Suppose we wish to ﬁnd a numerical solution to some ﬁrst-order differential equation with initial data y(x0) = y0 , say,

5 dy − y2 = −x2 with y(0) = 1 .

(9.3)

dx

(As it turns out, this differential equation is not easily solved by any of the methods already discussed. So if we want to ﬁnd the value of, say, y(3) , then a numerical method may be our only choice.)

To use Euler’s method to ﬁnd our numerical solution, we follow the steps given below. These steps are grouped into two parts: the main part in which the values of the xk’s and yk’s are iteratively computed, and the preliminary part in which the constants and formulas for those iterative computations are determined.

The Steps in Euler’s Method

Part I (Preliminaries)

1. Get the differential equation into derivative formula form, dy = f (x, y) . dx

For our example, solving for the derivative formula form yields dy = 1 y2 − x2 .

dx 5 2. Set x0 and y0 equal to the x and y values of the initial data.

194

Euler’s Numerical Method

In our example, the initial data is y(0) = 1 . So

x0 = 0 and y0 = 1 .

3. Pick a distance x for the step size, a positive integer N for the maximum number of steps, and a maximum value desired for x , xmax . These quantities should be chosen so that xmax = x0 + N x .

Of course, you only choose two of these values, and compute the third. Which two are chosen depends on the problem.

For no good reason whatsoever, let us pick

x = 1 and N = 6 .

2

Then

xmax = x0 + N x = 0 + 6 · 1 = 3 .

2

4. Write out the equations

xk+1 = xk + x

(9.4a)

and

yk+1 = yk + x · f (xk, yk)

(9.4b)

using the information from the previous steps. For our example,

f (x, y) = 1 y2 − x2

and

5

So, for our example, equation set (9.4) becomes

x=1 .

2

xk+1 = xk + 1 2

(9.4a ′)

and yk+1 = yk + 1 · 1 y2 − x 2

25

= yk + 1 yk 2 − xk 2 .

10

(9.4b ′)

Formula (9.4b) for yk+1 is based on approximation (9.2). According to that approximation, if y(x) is the solution to our initial-value problem and yk ≈ y(xk) , then

y(xk+1) = y(xk + x) ≈ yk + x · f (xk, yk ) = yk+1 .

Because of this, each yk generated by Euler’s method is an approximation of y(xk) .

Computing Via Euler’s Method (Illustrated)

195

Part II of Euler’s Method (Iterative Computations)

1. Compute x1 and y1 using equation set (9.4) with k = 0 and the values of x0 and y0 from the initial data. For our example, using equation set (9.4 ′) with k = 0 and the initial values x0 = 0 and y0 = 1 gives us

x1 = x0+1 = x0 +

x =0+ 1 = 1 ,

22

and y1 = y0+1 = y0 + x · f (x0, y0)

= y0 + 1 y02 − x02 10

= 1 + 1 12 − 02 = 11 .

10

10

2. Compute x2 and y2 using equation set (9.4) with k = 1 and the values of x1 and y1 from the previous step.

For our example, equation set (9.4 ′) with k = 1 and the above values for x1 and y1 yields

x2 = x1+1 = x1 +

x= 1+1 =1 ,

22

and y2 = y1+1 = y1 + x · f (x1, y1)

= y1 + 1 y12 − x12 10

= 11 + 1 10 10

11 2 1 2

290

−

=

.

10

2

250

3. Compute x3 and y3 using equation set (9.4) with k = 2 and the values of x2 and y2 from the previous step.

For our example, equation set (9.4 ′) with k = 2 and the above values for x2 and y2 yields

x3 = x2+1 = x2 +

x =1+ 1 = 3 ,

22

and y3 = y2+1 = y2 + 1 y22 − x22

10

= 29 + 1 250 10

29

2

− 12

= 774,401

.

250

625,000

For future convenience, note that y3 = 774,401 ≈ 1.2390 . 625,000

196

Euler’s Numerical Method

(d), (e), … In each subsequent step, increase k by 1 , and compute xk+1 and yk+1 using equation set (9.4) with the values of xk and yk from the previous step. Continue until xN and yN are computed.

For our example (omitting many computational details): With k + 1 = 4 ,

x4 = x3+1 = x3 +

x= 3+1 =2 ,

22

and y4 = y3+1 = y2 + 1 y32 − x32 = · · · ≈ 1.1676 .

10

With k + 1 = 5 ,

x5 = x4+1 = x4 +

x =2+ 1 = 5 ,

22

and y5 = y4+1 = y4 + 1 y42 − x42 = · · · ≈ 0.9039 .

10

With k + 1 = 6 ,

x6 = x5+1 = x5 +

x= 5+1 =6 ,

22

and y6 = y5+1 = y5 + 1 y52 − x52 = · · · ≈ 0.3606 .

10

Since we had earlier chosen N , the maximum number of steps, to be 6 , we can stop computing.

Using the Results of the Method

What you do with the results of your computations in depends on why you are doing these computations. If N is not too large, it is usually a good idea to write the obtained values of

{ x0 , x1 , x2 , x3 , . . . , xN } and { y0 , y1 , y2 , y3 , . . . , yN }

in a table for convenient reference (with a note that yk ≈ y(xk) for each k ) as done in ﬁgure 9.2a for our example. And, whatever the size of N , it is always enlightening to graph — as done in ﬁgure 9.2b for our example — the corresponding piecewise straight approximation y = yE, x (x) to the graph of y = y(x) by drawing straight lines between each (xk, yk) and (xk+1, yk+1) .

On Doing the Computations

The ﬁrst few times you use Euler’s method, attempt to do all the computations by hand. If the numbers become too awkward to handle, use a simple calculator and decimal approximations. This will help you understand and appreciate the method. It will also help you appreciate the tremendous value of programming a computer to do the calculations in the second part of the

What Can Go Wrong

197

k xk

yk

00

1

1 0.5 1.1000

2 1.0 1.1960

3 1.5 1.2390

4 2.0 1.1676

5 2.5 0.9039

6 3.0 0.3606

Y 1.0 0.5

0 0 0.5 1.0 1.5 2.0 2.5 3.0 X

(a)

(b)

Figure 9.2:

Results of Euler’s method to solve 5y′ − y2 = −x2 with y(0) = 1 using x = 1/2 and N = 6 : (a) The numerical solution in which yk ≈ y(xk) (for

k ≥ 3 , the values of yk are to the nearest 0.0001 ). (b) The graph of the corresponding approximate solution y = yE, x (x) .

method. That, of course, is how one should really carry out the computations in the second part of Euler’s method.

In fact, Euler’s method may already be one of the standard procedures in your favorite computer math package. Still, writing your own version is enlightening, and is highly recommended for the good of your soul.

9.3 What Can Go Wrong

Do not forget that Euler’s method does not yield exact answers. Instead, it yields values

{ x0 , x1 , x2 , x3 , . . . , xN } and { y0 , y1 , y2 , y3 , . . . , yN }

with yk ≈ y(xk) for k > 0 .

What’s more, each yk+1 is based on the approximation

y(xk + x) ≈ y(xk) + x · f (xk, y(xk))

with y(xk) being replaced with approximation yk when k > 0 . So we are computing approximations based on previous approximations.

Because of this, the accuracy of the approximation yk ≈ y(xk) , especially for larger values of k , is a serious issue. Consider the work done in the previous section: Just how well can we trust the approximation

y(3) ≈ 0.3606 obtained for the solution to initial-value problem (9.3)? In fact, it can be shown that

y(3) = −.23699 to the nearest 0.00001 .

198

Euler’s Numerical Method

Y

Y

4

4

3

approx. soln.

3

2

2

true soln.

1

1

0

0

0

1

2

3

4X

0

1

2

3

4X

−1

−1

(a)

(b)

Figure 9.3:

Catastrophic failure of Euler’s method in solving y′ = (y − 1)2 with y(0) = −1.3 : (a) Graphs of the true solution and the approximate solution. (b)

Same graphs with a slope ﬁeld, the graph of the equilibrium solution, and the graph of the true solution to y′ = (y − 1)2 with y(x1) = y1 .

So our approximation is not very good! To get an idea of how the errors can build up, look back at ﬁgure 9.1a on page 192. You can see

that, if the graphs of the true solutions to the differential equation are generally concave up (as in the ﬁgure), then the tangent line approximations used in Euler’s method lie below the true graphs, and yield underestimates for the approximations. Over several steps, these underestimates can build up so that the yk’s are signiﬁcantly below the actual values of the y(xk)’s .

Likewise, if the graphs of the true solutions are generally concave down, then the tangent line approximations used in Euler’s method lie above the true graphs, and yield overestimates for the approximations.

Also keep in mind that most of the tangent line approximations used in Euler’s method are not based on lines tangent to the true solution, but on lines tangent to solution curves passing through the (xk, yk)’s . This can lead to the “catastrophic failure” illustrated in ﬁgure 9.3a. In this ﬁgure, the true solution to

dy = (y − 1)2 dx

with y(0) = − 13 , 10

is graphed along with the graph of the approximate solution generated from Euler’s method with x = 1/2 . Exactly why the graphs appear so different becomes apparent when we superimpose

the slope ﬁeld in ﬁgure 9.3b. The differential equation has an unstable equilibrium solution y = 1 . If y(0) < 1 , as in the above initial-value problem, then the true solution y(x) should converge to 1 as x → ∞ . Here, however, one step of Euler’s method overestimated the value of y1 enough that (x1, y1) ended up above equilibrium and in the region where the solutions diverge away from the equilibrium. The tangent lines to these solutions led to higher and higher values for the subsequently computed yk’s . Thus, instead of correctly telling us that

lim y(x) = 1 ,

x →∞

Reducing the Error

199

Y y(xmax)

y = y(x)

Y y(xmax)

y = y(x)

yN yN

y0

x0 (a)

X xmax

y = yˆ(x) X

x0

xmax

(b)

Figure 9.4:

Two approximations yN of y(xmax) where y is the solution to y′ = f (x, y) with y(x0) = y0 : (a) Using Euler’s method with x equaling the distance from x0 to xmax . (b) Using Euler’s method with x equaling half the distance from x0 to xmax (Note: yˆ is the solution to y′ = f (x, y) with y(x1) = y1 .)

this application of Euler’s method suggests that

lim y(x) = ∞ .

x →∞

A few other situations where blindly applying Euler’s method can lead to misleading results are illustrated in the exercises (see exercises 9.6, 9.7, and 9.8, 9.9). And these sorts of problems are not unique to Euler’s method. Similar problems can occur with all numerical methods for solving differential equations. Because of this, it is highly recommended that Euler’s method (or any other numerical method) be used only as a last resort. Try the methods developed in the previous chapters ﬁrst. Use a numerical method only if the other methods fail to yield usable formulas or equations.

Unfortunately, the world is ﬁlled with ﬁrst-order differential equations for which numerical methods are the only practical choices. So be sure to skim the next section on improving the method. Also, if you must use Euler’s method (or any other numerical method), be sure to do a reality check. Graph the corresponding approximation on top of the slope ﬁeld for the differential equation, and ask yourself if the approximations are reasonable. In particular, watch out that your numerical solution does not “jump” over an unstable equilibrium solution.

9.4 Reducing the Error

Smaller Step Sizes

Suppose we are applying Euler’s method to a given initial-value problem over some interval [x0, xmax] . The one parameter we can adjust is the step size, x (or, equivalently, the number of steps, N , in going from x0 to xmax ). By shrinking x (increasing N ), at least two good things are typically accomplished:

1. The error in the underlying approximation

y(xk + x) ≈ y(xk) + x · f (xk, y(xk))

is reduced.

200

Euler’s Numerical Method

Y x =1

1.0

0.5

0 0

x=1 2

true solution

x=1 4

x=1 8

0.5

1.0

1.5

2.0

2.5

3.0 X

Figure 9.5: Graphs of the different piecewise straight line approximations of the solution to 5y′ − y2 = −x2 with y(0) = 1 obtained by using Euler’s method with

different values for the step size x = 1/2 . Also graphed is the true solution.

2. The slope in the piecewise straight approximation y = yE, x (x) is recomputed at more points, which means that this approximation can better match the bends in the slope ﬁeld for the differential equation.

Both of these are illustrated in ﬁgure 9.4. Accordingly, we should expect that shrinking the step size in Euler’s method will yield

numerical solutions that more accurately approximate the true solution. We can experimentally test this expectation by going back to our initial-value problem

5 dy − y2 = −x2 dx

with y(0) = 1 ,

computing (as you’ll be doing for exercise 9.5) the numerical solutions arising from Euler’s method using, say,

x=1 ,

x=1 , 2

x = 1 and 4

x=1 , 8

and then graphing the corresponding piecewise straight approximations over the interval [0, 3]

along with the graph of the true solution. Do this, and you will get the graphs in ﬁgure 9.5.1 As

expected, the graphs of the approximate solutions steadily approach the graph of the true solution

as x gets smaller. It’s even worth observing that the distance between the true value for y(3)

and the approximated value appears to be cut roughly in half each time x is cut in half.

In fact, our expectations can be rigorously conﬁrmed. In the next section, we will analyze

the error in using Euler’s method to approximate y(xmax) where y is the solution to a ﬁrst-order

initial-value problem

dy = f (x, y) dx

with y(x0) = y0 .

1 The graph of the “true solution” in ﬁgure 9.5 is actually the graph of a very accurate approximation. The difference between this graph and the graph of the true solution is less than the thickness of the curve used to sketch it.