2.6 A Numerical Method

INTRODUCTION

In Section 2.1 we saw that we could glean qualitative information from a first-order DE about its solutions even before we attempted to solve the equation. In Sections 2.2–2.5 we examined first-order DEs analytically; that is, we developed procedures for actually obtaining explicit and implicit solutions. But many differential equations possess solutions and yet these solutions cannot be obtained analytically. In this case we “solve” the differential equation numerically; this means that the DE is used as the cornerstone of an algorithm for approximating the unknown solution. It is common practice to refer to the algorithm as a numerical method, the approximate solution as a numerical solution, and the graph of a numerical solution as a numerical solution curve.

In this section we are going to consider only the simplest of numerical methods. A more extensive treatment of this subject is found in Chapter 6.

Using the Tangent Line

Let us assume that the first-order initial-value problem

y′ = f(x, y),     y(x0) = y0(1)

possesses a solution. One of the simplest techniques for approximating this solution is to use tangent lines. For example, let y(x) denote the unknown solution of the first-order initial-value problem , y(2) = 4. The nonlinear differential equation cannot be solved directly by the methods considered in Sections 2.2, 2.4, and 2.5; nevertheless, we can still find approximate numerical values of the unknown y(x). Specifically, suppose we wish to know the value of y(2.5). The IVP has a solution, and, as the flow of the direction field in FIGURE 2.6.1(a) suggests, a solution curve must have a shape similar to the curve shown in blue.

The first illustration represents a direction field. The arrows on the horizontal axis point towards the right. The arrows in the second quadrant become less steep as x increases. The arrows in the first quadrant become more steep as x increases. The curve begins at approximately (negative 2.5, 0) and goes up and to the right with decreasing steepness up to approximately (0, 2.5), then goes up and to the right with increasing steepness through (2, 4). A circle is marked at the point (2, 4). In the second illustration, a solution curve goes up and to the right with increasing steepness through point (2, 4). An arrow pointing up and to the right is marked tangent to the curve at point (2, 4). The slope of the arrow is labeled, m equals 1.8.

FIGURE 2.6.1 Magnification of a neighborhood about the point (2, 4)

The direction field in Figure 2.6.1(a) was generated so that the lineal elements pass through points in a grid with integer coordinates. As the solution curve passes through the initial point (2, 4), the lineal element at this point is a tangent line with slope given by f(2, 4) = 0.1 + 0.4(2)2 = 1.8. As is apparent in Figure 2.6.1(a) and the “zoom in” in Figure 2.6.1(b), when x is close to 2 the points on the solution curve are close to the points on the tangent line (the lineal element). Using the point (2, 4), the slope f(2, 4) = 1.8, and the point-slope form of a line, we find that an equation of the tangent line is y = L(x), where . This last equation, called a linearization of y(x) at x = 2, can be used to approximate values y(x) within a small neighborhood of x = 2. If y1 = L(x1) denotes the value of the y-coordinate on the tangent line and y(x1) is the y-coordinate on the solution curve corresponding to an x-coordinate x1 that is close to x = 2, then y(x1) ≈ y1. If we choose, say, x1 = 2.1, then y1 = L(2.1) = 1.8(2.1) + 0.4 = 4.18, and so y(2.1) ≈ 4.18.

Euler’s Method

To generalize the procedure just illustrated, we use the linearization of the unknown solution y(x) of (1) at x = x0:

L(x) = f(x0, y0)(xx0) + y0.(2)

The graph of this linearization is a straight line tangent to the graph of y = y(x) at the point (x0, y0). We now let h be a positive increment of the x-axis, as shown in FIGURE 2.6.2. Then by replacing x by x1 = x0 + h in (2) we get

L(x1) = f(x0, y0)(x0 + hx0) + y0     or     y1 = y0 + hf(x0, y0),

where y1 = L(x1). The point (x1, y1) on the tangent line is an approximation to the point (x1, y(x1)) on the solution curve. Of course the accuracy of the approximation y1y(x1) depends heavily on the size of the increment h. Usually we must choose this step size to be “reasonably small.” We now repeat the process using a second “tangent line” at (x1, y1).* By replacing (x0, y0) in the above discussion with the new starting point (x1, y1), we obtain an approximation y2y(x2) corresponding to two steps of length h from x0, that is, x2 = x1 + h = x0 + 2h and

y(x2) = y(x0 + 2h) = y(x1 + h) ≈ y2 = y1 + hf(x1, y1).

A solution curve is graphed. It enters the second quadrant, goes up and to the right, and intersects the positive vertical axis. It then continues into the first quadrant and goes up and to the right to a high point, then goes down and to the right. Points (x subscript zero, y subscript zero) and (x subscript one, y of x subscript one) are marked on the curve. x subscript one equals x subscript zero plus h. A line L of x goes up and to the right. It is tangent to the curve at point (x subscript zero, y subscript zero). The slope of the tangent is f of (x subscript zero, y subscript zero). A point on the tangent is marked, corresponding to x subscript one. The vertical distance between this point and point (x subscript one, y subscript one) is labeled, error.

FIGURE 2.6.2 Approximating y(x1) using a tangent line

Continuing in this manner, we see that y1, y2, y3, . . ., can be defined recursively by the general formula

(3)

where xn = x0 + nh, n = 0, 1, 2, . . . . This procedure of using successive “tangent lines” is called Euler’s method.

EXAMPLE 1 Euler’s Method

Consider the initial-value problem y′ = 0.1 + 0.4x2, y(2) = 4. Use Euler’s method to obtain an approximation to y(2.5) using first h = 0.1 and then h = 0.05.

SOLUTION

With the identification f(x, y) = 0.1 + 0.4x2, (3) becomes

Then for h = 0.1, x0 = 2, y0 = 4, and n = 0, we find

which, as we have already seen, is an estimate to the value of y(2.1). However, if we use the smaller step size h = 0.05, it takes two steps to reach x = 2.1. From

we have y1y(2.05) and y2y(2.1). The remainder of the calculations were carried out using software; the results are summarized in Tables 2.6.1 and 2.6.2. We see in Tables 2.6.1 and 2.6.2 that it takes five steps with h = 0.1 and ten steps with h = 0.05, respectively, to get to x = 2.5. Also, each entry has been rounded to four decimal places.

TABLE 2.6.1 h = 0.1
xn yn
2.00 4.0000
2.10 4.1800
2.20 4.3768
2.30 4.5914
2.40 4.8244
2.50 5.0768
TABLE 2.6.2 h = 0.05
xn yn
2.00 4.0000
2.05 4.0900
2.10 4.1842
2.15 4.2826
2.20 4.3854
2.25 4.4927
2.30 4.6045
2.35 4.7210
2.40 4.8423
2.45 4.9686
2.50 5.0997

In Example 2 we apply Euler’s method to a differential equation for which we have already found a solution. We do this to compare the values of the approximations yn at each step with the true values of the solution y(xn) of the initial-value problem.

EXAMPLE 2 Comparison of Approximate and Exact Values

Consider the initial-value problem y′ = 0.2xy, y(1) = 1. Use Euler’s method to obtain an approximation to y(1.5) using first h = 0.1 and then h = 0.05.

SOLUTION

With the identification f(x, y) = 0.2xy, (3) becomes

yn + 1 = yn + h(0.2xn yn),

where x0 = 1 and y0 = 1. Again with the aid of computer software we obtain the values in Tables 2.6.3 and 2.6.4.

TABLE 2.6.3 h = 0.1
A table has six rows and five columns. The column headers are x subscript n, y subscript n, actual value, absolute error, and percentage of relative error. The data in the table are as follows: Row 1. x subscript n: 1.00, y subscript n: 1.0000, actual value: 1.0000, absolute error: 0.0000, and percentage of relative error: 0.00. Row 2. x subscript n: 1.10, y subscript n: 1.0200, actual value: 1.0212, absolute error: 0.0012, and percentage of relative error: 0.12. Row 3. x subscript n: 1.20, y subscript n: 1.0424, actual value: 1.0450, absolute error: 0.0025, and percentage of relative error: 0.24. Row 4. x subscript n: 1.30, y subscript n: 1.0675, actual value: 1.0714, absolute error: 0.0040, and percentage of relative error: 0.37. Row 5. x subscript n: 1.40, y subscript n: 1.0952, actual value: 1.1008, absolute error: 0.0055, and percentage of relative error: 0.50. Row 6. x subscript n: 1.50, y subscript n: 1.1259, actual value: 1.1331, absolute error: 0.0073, and percentage of relative error: 0.64.
TABLE 2.6.4 h = 0.05
A table has 11 rows and five columns. The column headers are x subscript n, y subscript n, actual value, absolute error, and percentage of relative error. The data in the table are as follows. Row 1. x subscript n: 1.00, y subscript n: 1.0000, actual value: 1.0000, absolute error: 0.0000, and percentage of relative error: 0.00. Row 2. x subscript n: 1.05, y subscript n: 1.0100, actual value: 1.0103, absolute error: 0.0003, and percentage of relative error: 0.03. Row 3. x subscript n: 1.10, y subscript n: 1.0206, actual value: 1.0212, absolute error: 0.0006, and percentage of relative error: 0.06. Row 4. x subscript n: 1.15, y subscript n: 1.0318, actual value: 1.0328, absolute error: 0.0009, and percentage of relative error: 0.09. Row 5. x subscript n: 1.20, y subscript n: 1.0437, actual value: 1.0450, absolute error: 0.0013, and percentage of relative error: 0.12. Row 6. x subscript n: 1.25, y subscript n: 1.0562, actual value: 1.0579, absolute error: 0.0016, and percentage of relative error: 0.16. Row 7. x subscript n: 1.30, y subscript n: 1.0694, actual value: 1.0714, absolute error: 0.0020, and percentage of relative error: 0.19. Row 8. x subscript n: 1.35, y subscript n: 1.0833, actual value: 1.0857, absolute error: 0.0024, and percentage of relative error: 0.22. Row 9. x subscript n: 1.40, y subscript n: 1.0980, actual value: 1.1008, absolute error: 0.0028, and percentage of relative error: 0.25. Row 10. x subscript n: 1.45, y subscript n: 1.1133, actual value: 1.1166, absolute error: 0.0032, and percentage of relative error: 0.29. Row 11. x subscript n: 1.50, y subscript n: 1.1295, actual value: 1.1331, absolute error: 0.0037, and percentage of relative error: 0.32.

In Example 1, the true values were calculated from the known solution (verify). Also, the absolute error is defined to be

The relative error and percentage relative error are, in turn,

    and    

By comparing the last two columns in Tables 2.6.3 and 2.6.4, it is clear that the accuracy of the approximations improve as the step size h decreases. Also, we see that even though the percentage relative error is growing with each step, it does not appear to be that bad. But you should not be deceived by one example. If we simply change the coefficient of the right side of the DE in Example 2 from 0.2 to 2, then at xn = 1.5 the percentage relative errors increase dramatically. See Problem 4 in Exercises 2.6.

A Caveat

Euler’s method is just one of many different ways a solution of a differential equation can be approximated. Although attractive for its simplicity, Euler’s method is seldom used in serious calculations. We have introduced this topic simply to give you a first taste of numerical methods. We will go into greater detail and discuss methods that give significantly greater accuracy, notably the fourth-order Runge–Kutta method, in Chapter 6. We shall refer to this important numerical method as the RK4 method.

Numerical Solvers

Regardless of whether we can actually find an explicit or implicit solution, if a solution of a differential equation exists, it represents a smooth curve in the Cartesian plane. The basic idea behind any numerical method for ordinary differential equations is to somehow approximate the y-values of a solution for preselected values of x. We start at a specified initial point (x0, y0) on a solution curve and proceed to calculate in a step-by-step fashion a sequence of points (x1, y1), (x2, y2), . . ., (xn, yn) whose y-coordinates yi approximate the y-coordinates y(xi) of points (x1, y(x1)), (x2, y(x2)), . . ., (xn, y(xn)) that lie on the graph of the usually unknown solution y(x). By taking the x-coordinates close together (that is, for small values of h) and by joining the points (x1, y1), (x2, y2), . . ., (xn, yn) with short line segments, we obtain a polygonal curve that appears smooth and whose qualitative characteristics we hope are close to those of an actual solution curve. Drawing curves is something well suited to a computer. A computer program written to either implement a numerical method or to render a visual representation of an approximate solution curve fitting the numerical data produced by this method is referred to as a numerical solver. There are many different numerical solvers commercially available, either embedded in a larger software package such as a computer algebra system or as a stand-alone package. Some software packages simply plot the generated numerical approximations, whereas others generate both hard numerical data as well as the corresponding approximate or numerical solution curves. As an illustration of the connect-the-dots nature of the graphs produced by a numerical solver, the two red polygonal graphs in FIGURE 2.6.3 are numerical solution curves for the initial-value problem y′ = 0.2xy, y(0) = 1, on the interval [0, 4] obtained from Euler’s method and the RK4 method using the step size h = 1. The blue smooth curve is the graph of the exact solution of the IVP. Notice in Figure 2.6.3 that even with the ridiculously large step size of h = 1, the RK4 method produces the more believable “solution curve.” The numerical solution curve obtained from the RK4 method is indistinguishable from the actual solution curve on the interval [0, 4] when a more typical step size of h = 0.1 is used.

A graph consists of a curve and 2 broken-line graphs. The curve labeled exact solution starts from (0, 1), goes up and to the right and passes through the approximate points (1, 1.1), (2, 1.5), (3, 2.5), and (4, 5). The broken-line graph labeled Euler’s method starts from (0, 1), and goes up and to the right through the approximate points (1, 1), (2, 1.2), (3, 1.7), and (4, 2.7). The broken-line graph labeled Runge-Kutta method starts from (0, 1), and goes up and to the right through the same points as the solution curve: (1, 1.1), (2, 1.5), (3, 2.5), and (4, 5).

FIGURE 2.6.3 Comparison of numerical methods

Using a Numerical Solver

Knowledge of the various numerical methods is not necessary in order to use a numerical solver. A solver usually requires that the differential equation be expressed in normal form dy/dx = f(x, y). Numerical solvers that generate only curves usually require that you supply f(x, y) and the initial data x0 and y0 and specify the desired numerical method. If the idea is to approximate the numerical value of y(a), then a solver may additionally require that you state a value for h, or, equivalently, require the number of steps that you want to take to get from x = x0 to x = a. For example, if we want to approximate y(4) for the IVP illustrated in Figure 2.6.3, then, starting at x = 0, it takes four steps to reach x = 4 with a step size of h = 1; 40 steps is equivalent to a step size of h = 0.1. Although it is not our intention here to delve into the many problems that one can encounter when attempting to approximate mathematical quantities, you should be at least aware of the fact that a numerical solver may break down near certain points or give an incomplete or misleading picture when applied to some first-order differential equations in the normal form. FIGURE 2.6.4 illustrates the numerical solution curve obtained by applying Euler’s method to a certain first-order initial value problem dy/dx = f(x, y), y(0) = 1. Equivalent results were obtained using three different commercial numerical solvers, yet the graph is hardly a plausible solution curve. (Why?) There are several avenues of recourse when a numerical solver has difficulties; three of the more obvious are decrease the step size, use another numerical method, or try a different numerical solver.

A graph consists of a series of line segments that alternate going up and to the right and down and to the right. The graph oscillates through (negative 2.5, 2), (negative 2.25, negative 1), (negative 2, 2), (negative 1.75, negative 1), (negative 1.5, 2), (negative 1.25, negative 1), (negative 1, 2), (negative 0.75, negative 1), (negative 0.5, 2), and (negative 0.25, negative 1). It then goes up and to the right through (0, 1) to (0.5, 6), and oscillates through (0.75, 3), (1, 6), (1.25, 3), (1.5, 6), (1.75, 3), (2, 6), (2.25, 3), (2.5, 6), (2.75, 3), (3, 6), (3.25, 3), (3.5, 6), (3.75, 3), (4, 6), (4.25, 3), (4.5, 6), (4.75, 3) and (5, 6).

FIGURE 2.6.4 A not very helpful numerical solution curve

2.6 Exercises Answers to selected odd-numbered problems begin on page ANS-3.

In Problems 1 and 2, use Euler’s method to obtain a four-decimal approximation of the indicated value. Carry out the recursion of (3) by hand, first using h = 0.1 and then using h = 0.05.

  1. y′ = 2x − 3y + 1,     y(1) = 5; y(1.2)
  2. y′ = x + y2,     y(0) = 0; y(0.2)

In Problems 3 and 4, use Euler’s method to obtain a four-decimal approximation of the indicated value. First use h = 0.1 and then use h = 0.05. Find an explicit solution for each initial-value problem and then construct tables similar to Tables 2.6.3 and 2.6.4.

  1. y′ = y,     y(0) = 1; y(1.0)
  2. y′ = 2xy,     y(1) = 1; y(1.5)

In Problems 5–10, use a numerical solver and Euler’s method to obtain a four-decimal approximation of the indicated value. First use h = 0.1 and then use h = 0.05.

  1. y′ = e-y,     y(0) = 0; y(0.5)
  2. y′ = x2 + y2,     y(0) = 1; y(0.5)
  3. y′ = (xy)2,     y(0) = 0.5; y(0.5)
  4. y′ = xy +      y(0) = 1; y(0.5)
  5. y′ = xy2,     y(1) = 1; y(1.5)
  6. y′ = yy2,     y(0) = 0.5; y(0.5)

In Problems 11 and 12, use a numerical solver to obtain a numerical solution curve for the given initial-value problem. First use Euler’s method and then the RK4 method. Use h = 0.25 in each case. Superimpose both solution curves on the same coordinate axes. If possible, use a different color for each curve. Repeat, using h = 0.1 and h = 0.05.

  1. y′ = 2(cos x)y,     y(0) = 1
  2. y′ = y(10 − 2y),     y(0) = 1

Discussion Problem

  1. Use a numerical solver and Euler’s method to approximate y(1.0), where y(x) is the solution to y′ = 2xy2, y(0) = 1. First use h = 0.1 and then h = 0.05. Repeat using the RK4 method. Discuss what might cause the approximations of y(1.0) to differ so greatly.

 

*This is not an actual tangent line since (x1, y1) lies on the first tangent and not on the solution curve.