Docsity

Lecture Notes on Numerical Computing | CSCI 4800, Study notes of Computer Science

Partial preview of the text

Download Lecture Notes on Numerical Computing | CSCI 4800 and more Study notes Computer Science in PDF only on Docsity!

NUMERICAL COMPUTING

Spring 2012

LESSON 5

This lesson is concerned with numerical algorithms for solving initial-value problems for ordinary differential equations. It should be treated as a supplement to Chapter 9 of the Text.

1 Introduction

In this lesson we shall consider first-order ODEs of the form

dy dt

= f (t, y). (1.1)

The discussion will focus on single ODEs, but the procedures developed here can apply equally well to a system of n ODEs, for which y and f are n -dimensional vectors.

0

1

2

3

4

5

6

2 4 6 8 10 t

Figure 1: Direction field and solution curves for y′(t) = y sin t.

Any function y(t) satisfying the ODE is a solution. Its graph in the ty -plane is called a trajectory or an integral curve. The ODE itself does not define a solution uniquely. At a point (t, y) it only provides the slope y′(t) of the integral curve that passes through that point. There is an infinite family of solution curves, in general; see figure 1 above. To select a particular member of the family one needs to specify a point on the curve. This is done via an initial condition,

y(a) = α.

Example 1.1. y′(t) = y sin t, y(0) = 1. The unique solution is y = exp(1 − cos t). The solution is valid for t ≥ 0. (Figure 1 above.)

Example 1.2. y′(t) = 1 + y^2 , y(0) = 0. The solution is y = tan t. It is unique, but valid only for 0 ≤ t < π/ 2.

Example 1.3. y′(t) = y^2 /^3 , y(0) = 0. There are two solutions, y(t) = 0 and y(t) = t^3 / 27.

With the exception of certain special forms of f (t, y), solutions to the ODE (1.1) cannot be determined analytically. Recourse to the computer to obtain approximate solutions is therefore necessary.

2 Theoretical Considerations

Before embarking on the development of numerical approximations, it is useful to examine certain aspects of the theory of the IVP dy dt

= f (t, y), y(a) = α. (2.1)

  • stable if given any  > 0 there exists a δ > 0 such that any other solution ˆy(t) which satisfies the ODE and the initial condition |y 0 − yˆ 0 | ≤ δ also satisfies |y(t) − yˆ(t)| ≤  for all t ≥ 0;
  • asymptotically stable if, in addition to being stable, |y(t) − ˆy(t)| → 0 as t → ∞.

We have already examined the linear, constant-coefficient ODE. For the linear, variable-coefficient case, f (t, y) is of the form yg(t). The sign of g(t) at a particular value of t gives an indication of stability but only in the short run, as a change in the sign of g will alter the behavior. In the nonlinear case one examines the sign of the jacobian, fy (t, y), but again, the behavior may change in t and now, also from one solution y to another.

Example 2.1. y′(t) = y(1 − y). The steady solutions are y = 0 and y = 1. The jacobian is A = 1 − 2 y, which is positive for y = 0 and negative for y = 1. We conclude that y = 0 is unstable and y = 1 is stable. Also, two solutions starting at, say, y = 0.05 and y = 0.1 have positive jacobians, and hence a tendency to move apart, until they pass y = 1/ 2 , when the sign of the jacobian changes and the solutions draw closer.

1

2

y

1 2 3 4 5 t

Figure 2: Trajectories of y′(t) = y(1 − y).

3 Numerical Solution of IVPs

Analytical solutions to ODEs are unavailable, except for special classes of ODEs. When available the analytical solution is in the form of a function y(t), or its graph (the solution curve). A computational solution produces discrete approximations (ti, wi) to points (ti, y(ti)) on the solution curve.

3.1 Euler’s Method

We begin with the basic IVP y′(t) = f (t, y), a ≤ t ≤ b, y(a) = α. (3.1)

The objective is to determine a numerical approximation to y(t) at the discrete set of points

a = t 0 < t 1 < t 2 · · · < tn− 1 < tn = b.

We adopt the notation that wi is the numerical approximation to yi = y(ti). For simplicity we take the points to be equally spaced, so that the step size is given by

h = b − a n

Then, ti = a + ih, i = 0, 1 , 2 , · · · , n.

We assume that y(t) has two continuous derivatives. It is instructive to derive the method in three different ways.

(i) Expand the exact solution y(t) in a Taylor series about t = ti to get

y(t) = yi + (t − ti)y′(ti) +

(t − ti)^2 y′′(ci),

where ci is an unknown quantity lying between t and ti. Upon replacement of y′(t) by f (t, y) and evaluation at t = ti+1 the above expression reads

yi+1 = yi + hf (ti, yi) +

h^2 y′′(ci),

where ci now lies between ti and ti+1. Euler’s method results upon dropping the last term on the right and repalcing the exact solution yi by the numerical approximation wi :

wi+1 = wi + hf (ti, wi), i = 0, 1 , · · · , n − 1; w 0 = α. (3.2)

(ii) Replace the derivative term in the ODE (3.1) by the forward difference approximation (recall discussion on numerical differentiation, section 5.1 of text)

y′(ti) =

yi+1 − yi h

hy”(c)

to get yi+1 − yi h

hy”(c) = f (ti, yi).

Omitting the error term again leads to (3.2).

(iii) Integrate the differential equation in (3.1) from t = ti to t = ti+1 to get

∫ (^) yi+

yi

dy =

∫ (^) ti+

ti

f (t, y(t)) dt.

The LHS yields yi+1 − yi. On the RHS, approximate the integrand by its value at the left end point ti. The result is hf (ti, yi), and one is again led to (3.2).

The pattern of the Euler approximation is clear. At each step we evaluate the slope f at the current approx- imate solution point and use it to advance to the next time step. Thus the solution curve is approximated locally by the tangent line segment passing through (ti, wi) and having slope f (ti, wi).

Example 3.1. y′^ = 1 − 2 y, y(0) = 1.

This is a linear problem with exact solution

y(t) =

e−^2 t. (3.3).

Euler’s method gives

wi+1 = wi + f (ti, wi) = wi + h(1 − 2 wi),

yexact=tt.(1+log(tt)); err=abs(yexact-s(i,2)); format2=’%5.3f %15.6e %15.6e %15.6e \n’; fprintf(format2,tt,s(i,2),yexact’,err’) end %%%%%%%%% function sol=euler1(fcn,a,b,alpha,N) % Generates Euler solution to y’=f(t,y) using N steps % Initial condition y(a)=alpha % Interval [a,b] h=(b-a)/N; t=a+(0:N)h; y(1)=alpha; for k=1:N y(k+1)=y(k)+h*feval(fcn,t(k),y(k)); end sol=[t’,y’]; %%%%%%%%% function f=ydot1(t,y) f=1+y./t; %%%%%%%%% OUTPUT t w y |y-w|

1.000 1.000000e+00 1.000000e+00 0.000000e+ 1.500 2.000000e+00 2.108198e+00 1.081977e- 2.000 3.166667e+00 3.386294e+00 2.196277e- 2.500 4.458333e+00 4.790727e+00 3.323935e- 3.000 5.850000e+00 6.295837e+00 4.458369e- 3.500 7.325000e+00 7.884670e+00 5.596704e- 4.000 8.871429e+00 9.545177e+00 6.737489e- 4.500 1.048036e+01 1.126835e+01 7.879911e- 5.000 1.214484e+01 1.304719e+01 9.023483e- 5.500 1.385933e+01 1.487611e+01 1.016789e+ 6.000 1.561926e+01 1.675056e+01 1.131293e+

The exact and the approximate solutions are plotted in Figure 3.

3.2 Local and global errors

Suppose that the solution wi is exact, i.e., yi = wi. Then a Taylor expansion about ti gives

yi+1 = yi + hf (ti, yi) + h^2 2

y′′(ci), ti ≤ ci ≤ ti+1.

Also,

wi+1 = wi + f (ti, wi) = yi + f (ti, yi).

Subtraction leads to

ei+1 ≡ yi+1 − wi+1 = h^2 2!

y′′(ci). (3.4)

This is called the local truncation error, LTE. The LTE of an IVP algorithm is the error that results when a single step of the algorithm is performed with exact input data. A method has accuracy of order k if the

1 2 3 4 5 6 0

2

4

6

8

10

12

14

16

18

t

Solution of dy/dt = 1+y/t

Computed Exact

Figure 3: Exact solution and Euler approximation of the IVP y′^ = 1 + y/t, y(1) = 1, t ∈ [1, 6], h = 0. 5.

LTE is proportional to hk+1. Thus the Euler method is of order 1. One can visualize the LTE by observing that at time ti the solution point (ti, wi) lies on a certain solution curve of the ODE. The LTE causes the next computed solution point (ti+1, wi+1) to jump from this solution curve to a neigboring solution curve, with the LTE determining the magnitude of the jump. The next example illustrates this phenomenon.

Example 3.3. y′^ = ±y, y(0) = 1, t ∈ [0, 2].

(^00) 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2

1

2

3

4

5

6

7

8

(^00) 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2

1

Figure 4: Euler solution of y′^ = y (left) and y′^ = −y (right), y(0) = 1 with h = 0. 4.

The solutions are plotted in Figure 4. In each case a number of neighboring exact solutions are plotted along with the Euler approximation. We note that at every time step the tangent-line approximation representing the Euler approximation causes the solution to jump from one integral curve to another.

The global error at step i + 1 is defined as the difference between the exact and computed solution,

gi+1 = yi+1 − wi+1, (3.5)

Now,

|ei| ≤ zi

= hM 2 L

[(1 + hL)i^ − 1]

hM 2 L

[ehLi^ − 1]

= hM 2 L

[eL(ti−t^0 )^ − 1].

This result provides an upper bound for the global error and shows that it is of order h, thus becoming vanishingly small as h → 0. The primary importance of this result is not so much that it provides a bound on the global error, but the fact that it proves convergence of the Euler method. A convergent method is one for which the global error vanishes in the limit h → 0. Thus the method becomes increasingly accurate as the time step is reduced. For it to be useful a method needs to be convergent.

Example 3.4. y′^ = 1 + y/t, 1 ≤ t ≤ 6 , y(1) = 1.

We return to the IVP first considered in Example 3.2, but now examine its global error at the final time t = 6 as h is reduced from 1/2 to 1/ 256. The Matlab code and the computed results are given below. Only the script for the calling program is provided, the Euler routine and the function M-file are the same as in Example 3.2.

% script calling01.m % % Solves IVP y’(t)=f(t,y), y(a)=alpha at progressively % decreasing step size % % Input: % ODE particulars (f(t,y), a, b, alpha) % Number of subintervals n % % Output: % Step size % Exact solution y % Computed solution w % Global error |y(n) - w(n)| % Error ratio % % Calls Euler routine euler clc format=’ h |y-w| error ratio \n\n’; fprintf(format)

t0=1; T=6; % initial and final times alpha=1; % initial value yexact=T.*(1+log(T));

for k=1: n=10*2^(k-1); % number of subintervals h=(T-1)/n; % step size s=euler1(’ydot1’,t0,T,alpha,n); globalerr=abs(yexact-s(n+1,2)); if k== format=’%7.5f %15.6e \n’; fprintf(format,h,abs(yexact-s(n+1,2)));

else rate=globalerr0/globalerr; format=’%7.5f %15.6e %15.6e \n’; fprintf(format,h,abs(yexact-s(n+1,2)),rate) end globalerr0=globalerr; end %%%%%%%%%%%%%%% NUMERICAL OUTPUT

h |y-w| error ratio

0.50000 1.131293e+ 0.25000 5.948077e-01 1.901947e+ 0.12500 3.049166e-01 1.950723e+ 0.06250 1.543519e-01 1.975464e+ 0.03125 7.765033e-02 1.987782e+ 0.01562 3.894382e-02 1.993906e+ 0.00781 1.950158e-02 1.996957e+ 0.00391 9.758208e-03 1.998480e+

The first column of the output above lists the values of h, which are being reduced by a factor of 2 from one row to the next. The second column is the global error at the final time t = 6. We note that it is decreasing with decreasing h. The third column is the ratio of the global error at two successive values of h. This ratio approaches 2, indicating that the global error is decreasing at the same rate as h, and is therefore proportional to h for the Euler method, as anticipated in the result of section 3.3.

3.4 Higher-order Method: Explicit Trapezoidal

We have seen that the Euler method is first-order accurate, i.e., its LTE is of order (h^2 ). We now present an example of a second-order accurate method. Starting with the basic IVP (3.1), we integrate the ODE (as was done in the third method of deriving the Euler method; see section 3.1):

∫ (^) yi+

yi

dy =

∫ (^) ti+

ti

f (t, y(t)) dy.

The LHS yields yi+1 − yi. On the RHS, approximate the integrand by the trapezoidal rule to get

yi+1 − yi ≈

h 2

[f (ti, yi) + f (ti+1, yi+1)].

On ignoring truncation errors, and upon replacing yi by wi one is led to the algorithm

wi+1 − wi = h 2

[f (ti, wi) + f (ti+1, wi+1)].

In general this is a nonlinear equation for wi+1, and its solution will require a root finder such as Newton’s method or bisection. Algorithms of this kind, in which the approximation wi+1 cannot be explicitly com- puted in terms of the approximation wi are said to be implicit. We shall have more to say about them later. It is possible to render the above algorithm explicit by replacing wi+1 on the RHS by its Euler-method approximation. The result is the Explicit Trapezoidal algorithm

wi+1 = wi +

h 2

[

f (ti, wi) + f

ti+1, wi + hf (ti, wi)

)]

Example 3.5. y′^ = 1 + y/t, 1 ≤ t ≤ 6 , y(1) = 1.

0.06250 3.677094e-03 3.871007e+ 0.03125 9.342982e-04 3.935675e+ 0.01562 2.354635e-04 3.967911e+ 0.00781 5.910261e-05 3.983978e+ 0.00391 1.480528e-05 3.991995e+

The second set of computational results shows the effect of reducing h on the global error. The first column of the output lists the values of h, which are being reduced by a factor of 2 from one row to the next. The second column is the global error at the final time t = 6. The third column is the ratio of the global error at two successive values of h. This ratio approaches 4, indicating that the global error is decreasing at the rate h^2 , rather than the slower rate h for the Euler method (Example 3.4).

3.5 Higher-order Taylor Methods

After having presented one example of a method that is more accurate than the Euler method, we now consider the problem of deriving higher-order methods more systematically. One approach is based on Taylor series, which can be used to advance the solution from time ti to ti+1. Assuming sufficient differentiability, one can obtain the Taylor expansion

yi+1 = yi + hy i′ +

h^2 2

y i′′ + · · · +

hk k!

y( ik )+ Rk,

where the remainder has the form

Rk =

hk+ (k + 1)! y(k+1)(ci), ti ≤ ci ≤ ti+1. (3.6)

We note that y′^ is f (t, y), and derivatives of y of order greater than one are computed by differentiating f (t, y). Thus,

y′(t) = f (t, y(t)), y′′(t) = f ′^ = ft(t, y(t)) + fy (t, y(t))y′^ = ft + f fy , y′′′(t) = f ′′^ = ftt + fty y′^ + (ft + f fy )fy + f (fyt + fyy y′), etc.

where the prime denotes total differentiation with respect to t. Upon dropping the remainder we obtain the approximation wi+1 to the solution yi+1 as

wi+1 = wi + hf (ti, wi) +

h^2 2!

f ′(ti, wi) + · · · +

hk k!

f (k−1)(ti, wi). (3.7)

We can write the above expression economically as

wi+1 = wi + hTk(ti, wi), (3.8)

where Tk(t, y) is defined as

Tk(t, y) = f (t, y) + h 2!

f ′(t, y) + · · · + hk−^1 k!

f (k−1)(t, y). (3.9)

The LTE of the method is defined by the remainder (3.6) above; we observe from this expression that the method is of order k. We note also that the Euler method corresponds to k = 1.

Example 3.6. 5 ty′^ + y^2 = 2, y(4) = 1. Using a Taylor-series method of order 2, take one step and compute y(4.1).

For k = 2 the algorithm (3.8) can be written as

wi+1 = wi + hT 2 (ti, wi)

where

T 2 (t, y) = f (t, y) + h 2

[ft(t, y) + f (t, y)fy (t, y).]

Here the subscripts t and y denote the corresponding partial derivatives. We are given that

f (t, y) =

2 − y^2 5 t

Then,

ft = −

2 − y^2 5 t^2

and fy = −

2 y 5 t

The algorithm then reduces to

wi+1 = wi + h

2 − w^2 i 5 ti

h^2 2

[

2 − w^2 i 5 t^2 i

2 − w^2 i 5 ti

2 wi 5 ti

]

Upon substituting h = 0.1 and the initial condition w 0 = 1 at t 0 = 4 we find that

w 1 = 1 +

3.6 Runge-Kutta Methods

Taylor-series methods, especially those of higher orders, require substantial derivative computation. Runge- Kutta methods imitate Taylor-series methods in accuracy but avoid this difficulty by using diffrerence ap- proximations for the derivatives. The methods can be derived in a variety of ways, including use of quadrature rules and use of Taylor series.

3.6.1 Runge-Kutta method of order 2

Consider the derivation of the Taylor-series method of order 2.

y(t + h) = y(t) + hy′(t) +

h^2 2

y′′(t) + O(h^3 )

= y(t) + hf +

h^2 (ft + f fy ) + O(h^3 )

= y(t) +

hf +

h[f + hft + hf fy ] + O(h^3 ).

By using Taylor series for two variables we obtain

f (t + h, y + hf ) = f (t, y) + h[ft + f fy ] + O(h^2 ).

Then we can write

y(t + h) = y(t) +

h 2

f (t, y) +

h 2

f

t + h, y + hf (t, y)

  • O(h^3 ).

Upon dropping the error term we get the Runge-Kutta method of order 2, which can also be written as

wi+1 = wi +

h 2

[k 1 + k 2 ],

where k 1 = f (ti, wi), k 2 = f

ti+1, wi + hk 1

This method can be thought of as an Euler-like method in which the slope used to advance the solution is computed as an average of the initial slope and the slope at the provisional next step computed by the Euler method. A moment’s inspection reveals that this method is the same as the explicit trapezoidal method introduced earlier. It also goes under the name Heun’s method.

The exact solution is y(t) = eλt. Recall that the solution is asymptotically stable for λ < 0 , or if λ is complex, for Re(λ) < 0. Consider the numerical solution given by the Euler method:

wi+1 = wi + hλwi = (1 + hλ)wi, w 0 = α.

This difference equation has the solution

wi = (1 + λh)iw 0.

where 1 + hλ is the growth factor. For λ < 0 the exact solution decays. The numerical solution will decay for |1 + hλ| < 1 , requiring that h, if it is real, be restricted to the range

h <

−λ

(For complex λ, 1 + hλ = ρeiα^ with ρ < 1 , i.e., hλ lies inside a circle of radius 1 centered at -1 in the complex hλ -plane. ) Thus, stability of the numerical method constrains the step size. The region of the complex hλ -plane for which the numerical solution decays is called the region of absolute stability.

Example 3.7. We consider the case λ = − 1 , α = 1, previously considered in Example 3.3. In this case the absolute stability restriction on h is

h <

−λ

For h = 0. 4 , which satisfies the above criterion of absolute stability, the computed solution is displayed in Figure 4 (right) and exhibits the expected decay behavior. For h = 20/ 9 , which violates the stability criterion, the solution, plotted in Figure 6, displays an instability in the form of an oscillatory growth.

-8 0 2 4 6 8 10 12 14 16 18 20

0

2

4

6

Figure 6: Euler solution of y′^ = −y, y(0) = 1 with h = 20/ 9.

5 Stiff Problems

Let us begin with an example.

Example 3.8. Consider the IVP

y′(t) = − 100 y + 100t + 101, y(0) = 1 + C,

where C is a specified constant. It has the general solution

y(t) = 1 + t + Ce−^100 t

0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0. 0

1

2

t

Plots of 1+t+Cexp(−100t) C= C= C=−

Figure 7: Analytical solution of y′^ = − 100 y + 100t + 101, y(0) = 1 + C.

For C = 0 the solution is the straight line 1 + t. For C 6 = 0 the solution has an exponential component that decays very rapidly to zero, and one is again left with the straight line 1 + t as the asymptote. One can say that for C 6 = 0 the solution has two characteristic time scales, a fast time on which the initial transient decays, and a slow time on which the portion 1 + t of the solution evolves. Problems exhibiting such disparate time scales are said to be stiff and a straightforward numerical approach experiences difficulties.

To explore the issue let us try to solve this problem by the Euler method. The algorithm leads to

wi+1 = wi + hf (ti, wi) = wi + h(− 100 wi + 100ti + 101), w 0 = 1 + C.

This linear, nonhomogeneous difference equation is of the type encountered before in this lesson, and the solution is found to be wi = C(1 − 100 h)i^ + 1 + ti.

Stability requires that the first term on the RHS be a decaying term, which leads to the restriction

− 1 < 1 − 100 h < 1 ,

or 0 < h < 0. 02.

Let us solve the IVP by the Euler method for C = 0, 0 .01 and − 0 .01 for h = 0. 05 , a value outside the stability regime. The corresponding solutions are plotted in Figure 8. While the solution corresponding to C = 0 is computed accurately, those for C 6 = 0 illustrate dramatically the instability of the algorithm.

Thus we find that even though the rapidly-varying component of the solution decays rapidly, the algorithm is forced to employ a very short time step for the entire time span of the computation. This makes the method extremely expensive if computations are to be carried out over long times. A remedy is provided by implicit methods.

5.1 Implicit Approach: Backward Euler Method

Consider a backward Taylor expansion where the solution at time ti+1 is expanded about time ti as follows.

y(ti) = y(ti+1 − h) = y(ti+1) − hy′(ti+1) + O(h^2 ) = y(ti+1) − hf (ti+1, y(ti+1)) + O(h^2 ).

Example 3.9. For the linear problem considered in Example 3.8 the above method leads to

wi+1 =

1 + 100h wi +

h[101 + 100(ti + h)] 1 + 100h , w 0 = 1 + C.

The corresponding numerical solution, and the exact solution 1 + t for C = 0, are shown in Figure 9 for h = 0. 4. We note that unlike the unstable Forward Euler solutions (for h = 0.05 ) shown in Figure 8, the Backward Euler solution for the much larger value h = 0.4 is stable and approaches the asymptotic solution 1 + t as anticipated.

6 IVP Tools in MATLAB

MATLAB has a variety of ODE solvers using diverse methods. The most popular of these is the solver ODE45, which is an adaptive procedure based on 4th and 5th order Runge-Kutta methods. The sample code below illustrates the use of ODE45 in solving the IVP

y′^ = −y − 5 e−t^ sin 5t, y(0) = 1, 0 ≤ t ≤ 6.

The ODE is supplied in a separate m-file, while the calling program supplies the initial condition and the span of t over which the solution is desired. The exact and computed solutions are compared in Figure 10.

0 1 2 3 4 5 6 −0.

−0.

−0.

0

1

Figure 10: ODE45 solution of y′^ = −y − 5 e−t^ sin 5t, y(0) = 1, 0 ≤ t ≤ 6.

% script calling3.m % Calls ode45 and solves dy/dt = f(t,y), y(a)=alpha % Plots numerical approximation and the exact solution % Supplies initial value and span of t % Requires f(y,t) supplied in an auxiliary m-file clc

tspan=[0 6]; y0=1; % initial value [t y]=ode45(@odetest_slope,tspan,y0); figure plot(t,y,’’,’linewidth’,2) hold on tt=linspace(0,6); yy=exp(-tt).cos(5tt); plot(tt,yy,’r’,’linewidth’,2) zoom on max(abs(y-exp(-t).cos(5t))) %%%%%%%%%%%%%%%%%%%%%%% % function file odetest_slope.m function yprime=odetest_slope(t,y) yprime=-y-5exp(-t)sin(5t);