2.4 Numerical Approximation: Euler’s Method

It is the exception rather than the rule when a differential equation of the general form

dydx=f(x, y)

can be solved exactly and explicitly by elementary methods like those discussed in Chapter 1. For example, consider the simple equation

dydx=ex2.
(1)

A solution of Eq. (1) is simply an antiderivative of ex2. But it is known that every antiderivative of f(x)=ex2 is a nonelementary function—one that cannot be expressed as a finite combination of the familiar functions of elementary calculus. Hence no particular solution of Eq. (1) is finitely expressible in terms of elementary functions. Any attempt to use the symbolic techniques of Chapter 1 to find a simple explicit formula for a solution of (1) is therefore doomed to failure.

As a possible alternative, an old-fashioned computer plotter—one that uses an ink pen to draw curves mechanically—can be programmed to draw a solution curve that starts at the initial point (x0,y0) and attempts to thread its way through the slope field of a given differential equation y'=f(x,y). The procedure the plotter carries out can be described as follows.

  • The plotter pen starts at the initial point (x0,y0) and moves a tiny distance along the slope segment though (x0,y0). This takes it to the point (x1,y1).

  • At (x1,y1) the pen changes direction, and now moves a tiny distance along the slope segment through this new starting point (x1,y1). This takes it to the next starting point (x2,y2).

  • At (x2,y2) the pen changes direction again, and now moves a tiny distance along the slope segment through (x2,y2). This takes it to the next starting point (x3,y3).

Figure 2.4.1 illustrates the result of continuing in this fashion—by a sequence of discrete straight-line steps from one starting point to the next. In this figure we see a polygonal curve consisting of line segments that connect the successive points (x0,y0),(x1,y1),(x2,y2),(x3,y3), However, suppose that each “tiny distance” the pen travels along a slope segment—before the midcourse correction that sends it along a fresh new slope segment—is so very small that the naked eye cannot distinguish the individual line segments constituting the polygonal curve. Then the resulting polygonal curve looks like a smooth, continuously turning solution curve of the differential equation. Indeed, this is (in essence) how most of the solution curves shown in the figures of Chapter 1 were computer generated.

FIGURE 2.4.1.

The first few steps in approximating a solution curve.

Leonhard Euler—the great 18th-century mathematician for whom so many mathematical concepts, formulas, methods, and results are named—did not have a computer plotter, and his idea was to do all this numerically rather than graphically. In order to approximate the solution of the initial value problem

dydx=f(x, y),y(x0)=y0,
(2)

we first choose a fixed (horizontal) step size h to use in making each step from one point to the next. Suppose we’ve started at the initial point (x0,y0) and after n steps have reached the point (xn,yn). Then the step from (xn,yn) to the next point (xn+1,yn+1) is illustrated in Fig. 2.4.2. The slope of the direction segment through (xn,yn) is m=f(xn,yn). Hence a horizontal change of h from xn to xn+1 corresponds to a vertical change of mh=hf(xn,yn) from yn to yn+1. Therefore the coordinates of the new point (xn+1,yn+1) are given in terms of the old coordinates by

FIGURE 2.4.2.

The step from (xn, yn) to (xn+1, yn+1).

xn+1=xn+h,yn+1=yn+hf(xn, yn).

Given the initial value problem in (2), Euler’s method with step size h consists of starting with the initial point (x0,y0) and applying the formulas

x1x2x3===x0+hx1+hx2+hy1y2y3===y0+hf(x0, y0)y1+hf(x1, y1)y2+hf(x2, y2)

to calculate successive points (x1,y1), (x2,y2), (x3,y3),  on an approximate solution curve.

However, we ordinarily do not sketch the corresponding polygonal approximation. Instead, the numerical result of applying Euler’s method is the sequence of approximations

y1, y2, y3, , yn,

to the true values

y(x1), y(x2), y(x3), , y(xn),

at the points x1,x2,x3,,xn, of the exact (though unknown) solution y(x) of the initial value problem. These results typically are presented in the form of a table of approximate values of the desired solution.

The iterative formula in (3) tells us how to make the typical step from yn to yn+1 and is the heart of Euler’s method. Although the most important applications of Euler’s method are to nonlinear equations, we first illustrate the method with a simple initial value problem whose exact solution is available, just for the purpose of comparison of approximate and actual solutions.

Example 1

Apply Euler’s method to approximate the solution of the initial value problem

dydx=x+15y,y(0)=3,
(4)
  1. first with step size h=1 on the interval [0, 5],

  2. then with step size h=0.2 on the interval [0, 1].

Solution

  1. With x0=0, y0=3, f(x,y)=x+15y, and h=1 the iterative formula in (3) yields the approximate values

    y1y2y3y4y5=====y0+h[x0+15y0]=(3)+(1)[0+15(3)]=3.6,y1+h[x1+15y1]=(3.6)+(1)[1+15(3.6)]=3.32,y2+h[x2+15y2]=(3.32)+(1)[2+15(3.32)]=1.984,y3+h[x3+15y3]=(1.984)+(1)[3+15(1.984)]=0.6192,andy4+h[x4+15y4]=(0.6912)+(1)[4+15(0.6912)]4.7430

    at the points x1=1, x2=2, x3=3, x4=4, and x5=5. Note how the result of each calculation feeds into the next one. The resulting table of approximate values is

    x 0 1 2 3 4 5
    Approx. y 3 3.6 3.32 1.984 0.6912 4.7430

    Figure 2.4.3 shows the graph of this approximation, together with the graphs of the Euler approximations obtained with step sizes h=0.2 and 0.05, as well as the graph of the exact solution

    y(x)=22ex/55x25

    that is readily found using the linear-equation technique of Section 1.5. We see that decreasing the step size increases the accuracy, but with any single approximation, the accuracy decreases with distance from the initial point.

    FIGURE 2.4.3.

    Graphs of Euler approximations with step sizes h=1, h=0.2, and h=0.05.

  2. Starting afresh with x0=0, y0=3, f(x,y)=x+15y, and h=0.2, we get the approximate values

    y1y2y3y4y5=====y0+h[x0+15y0]=(3)+(0.2)[0+15(3)]=3.12,y1+h[x1+15y1]=(3.12)+(0.2)[0.2+15(3.12)]3.205,y2+h[x2+15y2](3.205)+(0.2)[0.4+15(3.205)]3.253,y3+h[x3+15y3](3.253)+(0.2)[0.6+15(3.253)]3.263,y4+h[x4+15y4](3.263)+(0.2)[0.8+15(3.263)]3.234

    at the points x1=0.2, x2=0.4, x3=0.6, x4=0.8, and x5=1. The resulting table of approximate values is

    x 0 0.2 0.4 0.6 0.8 1
    Approx. y 3 3.12 3.205 3.253 3.263 3.234

High accuracy with Euler’s method usually requires a very small step size and hence a larger number of steps than can reasonably be carried out by hand. The application material for this section contains calculator and computer programs for automating Euler’s method. One of these programs was used to calculate the table entries shown in Fig. 2.4.4. We see that 500 Euler steps (with step size h=0.002) from x=0 to x=1 yield values that are accurate to within 0.001.

FIGURE 2.4.4.

Euler approximations with step sizes h=0.2,h=0.02, and h=0.002.

x Approx y with h=0.2 Approx y with h=0.02 Approx y with h=0.002 Actual value of y
0 3.000 3.000 3.000 3.000
0.2 3.120 3.104 3.102 3.102
0.4 3.205 3.172 3.168 3.168
0.6 3.253 3.201 3.196 3.195
0.8 3.263 3.191 3.184 3.183
1 3.234 3.140 3.130 3.129

Example 2

Falling baseball Suppose the baseball of Example 3 in Section 1.3 is simply dropped (instead of being thrown downward) from the helicopter. Then its velocity v(t) after t seconds satisfies the initial value problem

dvdt=320.16v,v(0)=0.
(5)

We use Euler’s method with h=1 to track the ball’s increasing velocity at 1-second intervals for the first 10 seconds of fall. With t0=0, v0=0, F(t,v)=320.16v, and h=1 the iterative formula in (3) yields the approximate values

v1v2v3v4v5=====v0+h[320.16v0]=(0)+(1)[320.16(0)]=32,v1+h[320.16v1]=(32)+(1)[320.16(32)]=58.88,v2+h[320.16v2]=(58.88)+(1)[320.16(58.88)]81.46,v3+h[320.16v3]=(81.46)+(1)[320.16(81.46)]100.43,andv4+h[320.16v4]=(100.43)+(1)[320.16(100.43)]116.36.

Continuing in this fashion, we complete the h=1 column of v-values shown in the table of Fig. 2.4.5—where we have rounded off velocity entries to the nearest foot per second. The values corresponding to h=0.1 were calculated using a computer, and we see that they are accurate to within about 1 ft/s. Note also that after 10 seconds the falling ball has attained about 80% of its limiting velocity of 200 ft/s.

Local and Cumulative Errors

There are several sources of error in Euler’s method that may make the approximation yn to y(xn) unreliable for large values of n, those for which xn is not sufficiently close to x0. The error in the linear approximation formula

y(xn+1)yn+hf(xn,yn)=yn+1
(6)

FIGURE 2.4.5.

Euler approximations in Example 2 with step sizes h=1 and h=0.1.

t Approx v with h=1 Approx v with h=0.1 Actual value of v
1 32 30 30
2 59 55 55
3 81 77 76
4 100 95 95
5 116 111 110
6 130 124 123
7 141 135 135
8 150 145 144
9 158 153 153
10 165 160 160

is the amount by which the tangent line at (xn,yn) departs from the solution curve through (xn,yn), as illustrated in Fig. 2.4.6. This error, introduced at each step in the process, is called the local error in Euler’s method.

FIGURE 2.4.6.

The local error in Euler’s method.

The local error indicated in Fig. 2.4.6 would be the total error in yn+1 if the starting point yn in (6) were an exact value, rather than merely an approximation to the actual value y(xn). But yn itself suffers from the accumulated effects of all the local errors introduced at the previous steps. Thus the tangent line in Fig. 2.4.6 is tangent to the “wrong” solution curve—the one through (xn,yn) rather than the actual solution curve through the initial point (x0,y0). Figure 2.4.7 illustrates this cumulative error in Euler’s method; it is the amount by which the polygonal stepwise path from (x0,y0) departs from the actual solution curve through (x0,y0).

FIGURE 2.4.7.

The cumulative error in Euler’s method.

FIGURE 2.4.8.

Approximating the solution of dy/dx=x+y, y(0)=1 with successively smaller step sizes.

x y with h=0.1 y with h=0.02 y with h=0.005 y with h=0.001 Actual y
0.1 1.1000 1.1082 1.1098 1.1102 1.1103
0.2 1.2200 1.2380 1.2416 1.2426 1.2428
0.3 1.3620 1.3917 1.3977 1.3993 1.3997
0.4 1.5282 1.5719 1.5807 1.5831 1.5836
0.5 1.7210 1.7812 1.7933 1.7966 1.7974
0.6 1.9431 2.0227 2.0388 2.0431 2.0442
0.7 2.1974 2.2998 2.3205 2.3261 2.3275
0.8 2.4872 2.6161 2.6422 2.6493 2.6511
0.9 2.8159 2.9757 3.0082 3.0170 3.0192
1.0 3.1875 3.3832 3.4230 3.4338 3.4366

The usual way of attempting to reduce the cumulative error in Euler’s method is to decrease the step size h. The table in Fig. 2.4.8 shows the results obtained in approximating the exact solution y(x)=2exx1 of the initial value problem

dydx=x+y,y(0)=1,

using the successively smaller step sizes h=0.1, h=0.02, h=0.005, and h=0.001. We show computed values only at intervals of Δx=0.1. For instance, with h=0.001, the computation required 1000 Euler steps, but the value yn is shown only when n is a multiple of 100, so that xn is an integral multiple of 0.1.

By scanning the columns in Fig. 2.4.8 we observe that, for each fixed step size h, the error yactualyapprox increases as x gets farther from the starting point x0=0. But by scanning the rows of the table we see that for each fixed x, the error decreases as the step size h is reduced. The percentage errors at the final point x=1 range from 7.25% with h=0.1 down to only 0.08% with h=0.001. Thus the smaller the step size, the more slowly does the error grow with increasing distance from the starting point.

The column of data for h=0.1 in Fig. 2.4.8 requires only 10 steps, so Euler’s method can be carried out with a hand-held calculator. But 50 steps are required to reach x=1 with h=0.02, 200 steps with h=0.005, and 1000 steps with h=0.001. A computer is almost always used to implement Euler’s method when more than 10 or 20 steps are required. Once an appropriate computer program has been written, one step size is—in principle—just as convenient as another; after all, the computer hardly cares how many steps it is asked to carry out.

Why, then, do we not simply choose an exceedingly small step size (such as h=1012), with the expectation that very great accuracy will result? There are two reasons for not doing so. The first is obvious: the time required for the computation. For example, the data in Fig. 2.4.8 were obtained using a hand-held calculator that carried out nine Euler steps per second. Thus it required slightly over one second to approximate y(1) with h=0.1 and about 1 min 50 s with h=0.001. But with h=1012 it would require over 3000 years!

The second reason is more subtle. In addition to the local and cumulative errors discussed previously, the computer itself will contribute roundoff error at each stage because only finitely many significant digits can be used in each calculation. An Euler’s method computation with h=0.0001 will introduce roundoff errors 1000 times as often as one with h=0.1. Hence with certain differential equations, h=0.1 might actually produce more accurate results than those obtained with h=0.0001, because the cumulative effect of roundoff error in the latter case might exceed combined cumulative and roundoff error in the case h=0.1.

The “best” choice of h is difficult to determine in practice as well as in theory. It depends on the nature of the function f(x, y) in the initial value problem in (2), on the exact code in which the program is written, and on the specific computer used. With a step size that is too large, the approximations inherent in Euler’s method may not be sufficiently accurate, whereas if h is too small, then roundoff errors may accumulate to an unacceptable degree or the program may require too much time to be practical. The subject of error propagation in numerical algorithms is treated in numerical analysis courses and textbooks.

The computations in Fig. 2.4.8 illustrate the common strategy of applying a numerical algorithm, such as Euler’s method, several times in succession, beginning with a selected number n of subintervals for the first application, then doubling n for each succeeding application of the method. Visual comparison of successive results often can provide an “intuitive feel” for their accuracy. In the next two examples we present graphically the results of successive applications of Euler’s method.

Example 3

Approximate logistic solution The exact solution of the logistic initial value problem

dydx=13y(8y),y(0)=1

is y(x)=8/(1+7e8x/3). Figure 2.4.9 shows both the exact solution curve and approximate solution curves obtained by applying Euler’s method on the interval 0x5 with n=5, n=10, and n=20 subintervals. Each of these “curves” actually consists of line segments joining successive points (xn,yn) and (xn+1,yn+1). The Euler approximation with 5 subintervals is poor, and the approximation with 10 subintervals also overshoots the limiting value y=8 of the solution before leveling off, but with 20 subintervals we obtain fairly good qualitative agreement with the actual behavior of the solution.

FIGURE 2.4.9.

Approximating a logistic solution using Euler’s method with n=5, n=10, and n=20 subintervals.

FIGURE 2.4.10.

Approximating the exact solution y=esinx using Euler’s method with 50, 100, 200, and 400 subintervals.

Example 4

The exact solution of the initial value problem

dydx=ycosx,y(0)=1

is the periodic function y(x)=esin x. Figure 2.4.10 shows both the exact solution curve and approximate solution curves obtained by applying Euler’s method on the interval 0x6π with n=50, n=100, n=200, and n=400 subintervals. Even with this many subintervals, Euler’s method evidently has considerable difficulty keeping up with the oscillations in the actual solution. Consequently, the more accurate methods discussed in succeeding sections are needed for serious numerical investigations.

A Word of Caution

The data shown in Fig. 2.4.8 indicate that Euler’s method works well in approximating the solution of dy/dx=x+y, y(0)=1 on the interval [0, 1]. That is, for each fixed x it appears that the approximate values approach the actual value of y(x) as the step size h is decreased. For instance, the approximate values in the rows corresponding to x=0.3 and x=0.5 suggest that y(0.3)1.40 and y(0.5)1.80, in accord with the actual values shown in the final column of the table.

Example 5, in contrast, shows that some initial value problems are not so well behaved.

Example 5

Cautionary example Use Euler’s method to approximate the solution of the initial value problem

dydx=x2+y2,y(0)=1
(7)

on the interval [0, 1].

Solution

Here f(x,y)=x2+y2, so the iterative formula of Euler’s method is

yn+1=yn+h(x2n+y2n).
(8)

With step size h=0.1 we obtain

y1y2y3===1+(0.1)[(0)2+(1)2]=1.1,1.1+(0.1)[(0.1)2+(1.1)2]=1.222,1.222+(0.1)[(0.2)2+(1.222)2]1.3753,

and so forth. Rounded to four decimal places, the first ten values obtained in this manner are

y1y2y3y4y5=====1.10001.22201.37531.57351.8371y6y7y8y9y10=====2.19952.71933.50784.80237.1895

But instead of naively accepting these results as accurate approximations, we decided to use a computer to repeat the computations with smaller values of h. The table in Fig. 2.4.11 shows the results obtained with step sizes h=0.1, h=0.02, and h=0.005. Observe that now the “stability” of the procedure in Example 1 is missing. Indeed, it seems obvious that something is going wrong near x=1.

Figure 2.4.12 provides a graphical clue to the difficulty. It shows a slope field for dy/dx=x2+y2, together with a solution curve through (0, 1) plotted using one of the more accurate approximation methods of the following two sections. It appears that this solution curve may have a vertical asymptote near x=0.97. Indeed, an exact solution using Bessel functions (see Problem 34 in Section 11.4) can be used to show that y(x)+ as x0.969811 (approximately). Although Euler’s method gives values (albeit spurious ones) at x=1, the actual solution does not exist on the entire interval [0, 1]. Moreover, Euler’s method is unable to “keep up” with the rapid changes in y(x) that occur as x approaches the infinite discontinuity near 0.969811.

The moral of Example 5 is that there are pitfalls in the numerical solution of certain initial value problems. Certainly it’s pointless to attempt to approximate a solution on an interval where it doesn’t even exist (or where it is not unique, in which case there’s no general way to predict which way the numerical approximations will branch at a point of nonuniqueness). One should never accept as accurate the results of applying Euler’s method with a single fixed step size h. A second “run” with smaller step size (h/2, say, or h/5, or h/10) may give seemingly consistent results, thereby suggesting their accuracy, or it may—as in Example 5—reveal the presence of some hidden difficulty in the problem. Many problems simply require the more accurate and powerful methods that are discussed in the final two sections of this chapter.

FIGURE 2.4.11.

Attempting to approximate the solution of dy/dx=x2+y2, y(0)=1.

x y with h=0.1 y with h=0.02 y with h=0.005
0.1 1.1000 1.1088 1.1108
0.2 1.2220 1.2458 1.2512
0.3 1.3753 1.4243 1.4357
0.4 1.5735 1.6658 1.6882
0.5 1.8371 2.0074 2.0512
0.6 2.1995 2.5201 2.6104
0.7 2.7193 3.3612 3.5706
0.8 3.5078 4.9601 5.5763
0.9 4.8023 9.0000 12.2061
1.0 7.1895 30.9167 1502.2090

FIGURE 2.4.12.

Solution of dy/dx=x2+y2, y(0)=1.

2.4 Problems

In Problems 1 through 10, an initial value problem and its exact solution y(x) are given. Apply Euler’s method twice to approximate to this solution on the interval [0,12], first with step size h=0.25, then with step size h=0.1. Compare the three-decimal-place values of the two approximations at x=12 with the value y(12) of the actual solution.

  1. y'=y, y(0)=2; y(x)=2ex

     

  2. y'=2y, y(0)=12; y(x)=12e2x

     

  3. y'=y+1, y(0)=1; y(x)=2ex1

     

  4. y'=xy, y(0)=1; y(x)=2ex+x1

     

  5. y'=yx1, y(0)=1; y(x)=2+xex

     

  6. y'=2xy, y(0)=2; y(x)=2ex2

     

  7. y'=3x2y, y(0)=3; y(x)=3ex3

     

  8. y'=ey, y(0)=0; y(x)=ln (x+1)

     

  9. y'=14(1+y2), y(0)=1; y(x)=tan14(x+π)

     

  10. y'=2xy2, y(0)=1; y(x)=11x2

Note: The application following this problem set lists illustrative calculator/computer programs that can be used in the remaining problems.

A programmable calculator or a computer will be useful for Problems 11 through 16. In each problem find the exact solution of the given initial value problem. Then apply Euler’s method twice to approximate (to four decimal places) this solution on the given interval, first with step size h=0.01, then with step size h=0.005. Make a table showing the approximate values and the actual value, together with the percentage error in the more accurate approximation, for x an integral multiple of 0.2. Throughout, primes denote derivatives with respect to x.

  1. y'=y2, y(0)=1;0x1

     

  2. y'=12(y1)2, y(0)=2;0x1

     

  3. yy'=2x3, y(1)=3;1x2

     

  4. xy'=y2, y(1)=1;1x2

     

  5. xy'=3x2y, y(2)=3;2x3

     

  6. y2y'=2x5, y(2)=3;2x3

A computer with a printer is required for Problems 17 through 24. In these initial value problems, use Euler’s method with step sizes h=0.1, 0.02, 0.004, and 0.0008 to approximate to four decimal places the values of the solution at ten equally spaced points of the given interval. Print the results in tabular form with appropriate headings to make it easy to gauge the effect of varying the step size h. Throughout, primes denote derivatives with respect to x.

  1. y'=x2+y2, y(0)=0; 0x1

     

  2. y'=x2y2, y(0)=1; 0x2

     

  3. y'=x+y, y(0)=1; 0x2

     

  4. y'=x+y3, y(0)=1; 0x2

     

  5. y'=ln y, y(1)=2; 1x2

     

  6. y'=x2/3+y2/3, y(0)=1; 0x2

     

  7. y'=sin x+cos y, y(0)=0; 0x1

     

  8. y'=x1+y2, y(1)=1; 1x1

     

  9. Falling parachutist You bail out of the helicopter of Example 2 and immediately pull the ripcord of your parachute. Now k=1.6 in Eq. (5), so your downward velocity satisfies the initial value problem

    dvdt=321.6v,v(0)=0

    (with t in seconds and v in ft/sec). Use Euler’s method with a programmable calculator or computer to approximate the solution for 0t2, first with step size h=0.01 and then with h=0.005, rounding off approximate v-values to one decimal place. What percentage of the limiting velocity 20 ft/sec has been attained after 1 second? After 2 seconds?

  10. Deer population Suppose the deer population P(t) in a small forest initially numbers 25 and satisfies the logistic equation

    dPdt=0.0225P0.0003P2

    (with t in months). Use Euler’s method with a programmable calculator or computer to approximate the solution for 10 years, first with step size h=1 and then with h=0.5, rounding off approximate P-values to integral numbers of deer. What percentage of the limiting population of 75 deer has been attained after 5 years? After 10 years?

Use Euler’s method with a computer system to find the desired solution values in Problems 27 and 28. Start with step size h=0.1, and then use successively smaller step sizes until successive approximate solution values at x=2 agree rounded off to two decimal places.

  1. y'=x2+y21, y(0)=0; y(2)=?

     

  2. y'=x+12y2, y(2)=0; y(2)=?

Problems 29 through 31 illustrate the unreliability of Euler’s method near a discontinuity of the solution.

  1. Consider the initial value problem

    7xdydx+y=0,y(1)=1.

    (a) Solve this problem for the exact solution

    y(x)=1x1/7,

    which has an infinite discontinuity at x=0. (b) Apply Euler’s method with step size h=0.15 to approximate this solution on the interval 1x0.5. Note that, from these data alone, you might not suspect any difficulty near x=0. The reason is that the numerical approximation “jumps across the discontinuity” to another solution of 7xy'+y=0 for x>0. (c) Finally, apply Euler’s method with step sizes h=0.03 and h=0.006, but still printing results only at the original points x=1.00, 0.85, 0.70, , 1.20, 1.35. and 1.50. Would you now suspect a discontinuity in the exact solution?

  2. Apply Euler’s method with successively smaller step sizes on the interval [0, 2] to verify empirically that the solution of the initial value problem

    dydx=x2+y2,y(0)=0

    has a vertical asymptote near x=2.003147. (Contrast this with Example 2, in which y(0)=1.)

  3. The general solution of the equation

    dydx=(1+y2)cosx

    is y(x)=tan(C+sin x). With the initial condition y(0)=0 the solution y(x)=tan(sin x) is well behaved. But with y(0)=1 the solution y(x)=tan(14π+sin x) has a vertical asymptote at x=sin1(π/4)0.90334. Use Euler’s method to verify this fact empirically.

2.4 Application Implementing Euler’s Method

Construction of a calculator or computer program to implement a numerical algorithm can sharpen one’s understanding of the algorithm. Figure 2.4.13 lists TI-84 Plus and Python programs implementing Euler’s method to approximate the solution of the initial value problem

dydx=x+y,y(0)=1

considered in this section. The comments provided in the final column should make these programs intelligible even if you have little familiarity with the Python and TI calculator programming languages. Python—freely available on the Internet—is a general-purpose computing language widely used in education and industry. In particular, its many add-on packages make Python a powerful scientific computing platform, with numerical, graphic, and symbolic capabilities approaching those of commercial systems such as Matlab, Mathematica, and Maple. Further, Python code was designed with an emphasis on readability, making it well-suited to expressing basic mathematical algorithms.

FIGURE 2.4.13.

TI-84 Plus and Python Euler’s method programs.

TI-84 Plus Python Comment
PROGRAM: EULER # Program EULER Program title
:10→N N = 10 Number of steps
:0→X X = 0.0 Initial x
:1→Y Y = 1.0 Initial y
:1→T X1 = 1.0 Final x
:(T-X)/N→H H = (X1-X)/N Step size
:For(I,1,N) for I in range(N): Begin loop
:X+Y→F F = X + Y Function value
:Y+H*F→Y Y = Y + H*F Euler iteration
:X+H→X X = X + H New x
:Disp X,Y print (X,Y) Display results
:End # END End loop

To increase the number of steps (and thereby decrease the step size) you need only change the value of N specified in the first line of the program. To apply Euler’s method to a different equation dy/dx=f(x,y), you need change only the single line that calculates the function value F.

Any other procedural programming language (such as FORTRAN or C++) would follow the pattern illustrated by the parallel lines of TI-84 Plus and Python code in Fig. 2.4.13. Some of the modern functional programming languages mirror standard mathematical notation even more closely. Figure 2.4.14 shows a Matlab implementation of Euler’s method. The euler function takes as input the initial value x, the initial value y, the final value x1 of x, and the desired number of subintervals.

function yp = f(x,y)
yp = x + y;                % yp = y’
function [X,Y] = euler(x,y,x1,n)
h = (x1 - x)/n;            % step size
X = x;                     % initial x
Y = y;                     % initial y
for i = 1:n                % begin loop
    y = y + h*f(x,y);      % Euler iteration
    x = x + h;             % new x
    X = [X; x];            % update x-column
    Y = [Y; y];            % update y-column
    end                    % end loop

FIGURE 2.4.14.

Matlab implementation of Euler’s method.

For instance, the Matlab command

[X, Y] = euler(0, 1, 1, 10)

then generates the xn- and yn-data shown in the first two columns of the table of Fig. 2.4.8.

You should begin this project by implementing Euler’s method with your own calculator or computer system. Test your program by first applying it to the initial value problem in Example 1, then to some of the problems for this section.

Famous Numbers Investigation

The following problems describe the numbers e2.71828, ln 20.69315, and π3.14159 as specific values of solutions of certain initial value problems. In each case, apply Euler’s method with n=50, 100, 200,  subintervals (doubling n each time). How many subintervals are needed to obtain—twice in succession—the correct value of the target number rounded to three decimal places?

  1. The number e=y(1), where y(x) is the solution of the initial value problem dy/dx=y, y(0)=1.

  2. The number ln 2=y(2), where y(x) is the solution of the initial value problem dy/dx=1/x, y(1)=0.

  3. The number π=y(1), where y(x) is the solution of the initial value problem dy/dx=4/(1+x2), y(0)=0.

Also explain in each problem what the point is—why the indicated famous number is the expected numerical result.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.54.199