Chapter 7 Ordinary Differential Equations Differential equations are an extremely important tool in various science and engineering disciplines. Laws of nature are most often expressed as different equations. Just as it is often difficult to evaluate an integral analytically, it is often difficult to solve an Ordinary Differential Equation ODE) analytically. Simple examples in which analytical methods e.g., separation of variables) are successful are illustrated below. Example 7.1 Consider the equation Rearranging the terms, Taking the integral for both sides, which gives where C is a constant. Therefore, dt = y. y = dt. y = dt, log y = t + C, y = ±e C e t. Example 7.2 Here is a somewhat more complicated equation dt = 1 y y. Rearranging the equation and taking the integral, 1 y y = we obtain Case 1: Assume y > 0; therefore, Computing the integral, we have dt, 1 y y = t + C, 1 y 2 = t + C. tanh 1 y = t + C, y < 1 coth 1 y = t + C y > 1
Chap. 7. Ordinary Differential Equations CS414 Class Notes 103 which gives the following solution y = { tanht + C), y < 1 cotht + C), y > 1 Case 2: Assume y < 0; therefore, which can be simplified to giving the solution 1 + y 2 = t + C, tan 1 y = t + C, y = tant + C) Example 7.3 Solve the equation y t) = tyt) with the condition y0) = 1. Again, rearranging and integrating, y = tdt, which gives the solution log e y = t2 2 + C. Now, we enforce the condition y0) = 1 to get log e 1 = 0 + C C = 0 The solution can now be written as yt) = e t2 2 7.1 The Initial-Value Problem for a Single ODE The general form of the initial value problem for a single ODE is y t) = ft, yt)), ya) = η The first part, y t) = ft, yt)) gives the slope of the function yt) at t. This condition determines a family of curves in the t y plane that satisfy the given equation. To specify a particular curve, we need an additional condition. The second part, ya) = η specifies the function value at η. Such a condition is called the initial condition, and determines a unique solution. Suppose y t) = ft) dt = ft) Therefore, and integrating both sides, we have t a dt = ft), = t a ft)dt.
Chap. 7. Ordinary Differential Equations CS414 Class Notes 104 z η z=yt) [solution] a t Figure 7.1: The initial value problem for a single ODE. Along with the initial condition, ya) = η, we get the following solution yt) = η + t a fs)ds Example 7.4 Suppose that an ODE has as its solution yt) = 3 + Ce 2t where C is an arbitrary constant 1. Show that there exists a solution through any point t 0, y 0 ) in the t y plane. 2. Show that this is not true if instead yt) = 3 + C t. Solution 1. We know that 3+C e 2t0 = y 0, which implies that C = y 0 3)e 2t0. Therefore, the following function yt) passes through t 0, y 0 ): yt) = 3 + y 0 3)e 2t t0) 2. If 3 + C t 0 = y 0, then C = y 0 3)/t 0. For t 0 = 0 and y 0 3, we cannot satisfy 3 + C t 0 = y 0. Example 7.5 Obtain the general solution of y t) = yt)/t, and also the particular solution for the initial condition y1) = 1 2. Solution Rearranging the equation, Integrating both sides, and thus, y = dt t. log e y = log e t + C 1, y = e C1+log e t = e C1 t.
Chap. 7. Ordinary Differential Equations CS414 Class Notes 105 Since y1) = 1 2, we have e C1 = 1 2, and therefore, yt) = 0.5t Example 7.6 Solve for the initial condition y0) = 1/2. Solution Rearranging the equation, The integral of the left hand side is given as y1 y) = y + y t) = yt)[1 yt)] y1 y) = dt 1 y = log e y log e 1 y). Thus, or log e y log e 1 y) = t + C, log e y 1 y = t + C. At t = 0, y0) = 1 2, which implies that C = 0. This gives the solution or yt) 1 yt) = et yt) = et 1 + e t. Example 7.7 Solve given that y1) = η. Solution Rearranging, and integrating, The initial condition implies y t) = y2 t), t 1, t y 2 = dt t, y 1 = log e t + C. 1 η = log1 e +C C = 1 η. Thus, yt) = η 1 η log e t.
Chap. 7. Ordinary Differential Equations CS414 Class Notes 106 yt) y= γ e t εe t initial value ε t Figure 7.2: Unstable family of solutions. y= γ e -t yt) initial value ε εe -t t Figure 7.3: A stable family of solutions. 7.2 Stability The single ODE y t) = y has the family of solutions yt) = γe t. Note that the error ɛ at t = 0 grows by a factor of e t at time t. On the other hand, for y t) = yt), we have the family of solutions yt) = γe t, and the error ɛ at t = 0, decreases as t increases by a factor of e t. Figs.?? and?? represent this fact graphically. 7.3 Euler s Method In order to solve the equation, = ft, y), dt let us consider the simple finite difference approximation in time y k+1 y k h = ft k, y k ) k = 0, 1, 2,..., N where we have chosen a uniform mesh for the time axis. The time instance t k is given by
Chap. 7. Ordinary Differential Equations CS414 Class Notes 107 h a=t 0 t 3 t 1 t 2 t N-1 t =b N Figure 7.4: A uniform mesh for the time axis. t k = k h + a where a is the starting instance and b is the final time. Thus, h = b a N y k+1 = y k + h ft k, y k ). y k solution through t, y ) k-1 k-1 y k-1 t k-1 h t k Figure 7.5: Euler s method. Example 7.8 Consider y t) = 1 y 2 t) with the initial condition y0) = 0. Let h = 0.1; since y0) = 0, we have y 1 = y 0 + 0.1)[1 y 2 0] = 0.1, y 2 = y 1 + 0.1)1 y1 2 ) = 0.1 + 0.1)1.01) = 0.199. Similarly, y 3 = 0.295,..., etc. 7.3.1 Local and Global Error in Euler s Method We need a measure of the error by which the exact solution of y = ft, y) fails to satisfy the difference equation y k+1 = y k + hft k, y k ). This is known as the local truncation error, or discretization error and is define by... At time t k we are at the point t k, y k ). Most likely it is not on the true solution but rather on some nearby solution Ut), i.e., y k = Ut k ), U t) = ft, Ut)). We call Ut) the local solution. Therefore, the local error is given by d k+1 = y k+1 Ut k+1 ). But, y k+1 = Ut k ) + hu t k )
Chap. 7. Ordinary Differential Equations CS414 Class Notes 108 y k+1 d k+1 Ut) y k t h t k k+1 Figure 7.6: Local error. and since Ut k+1 ) = Ut k ) + hu t k ) + h2 2 U τ k+1 ), where the last term on the right hand side is the error. This is obvious since and thus, Ut k+1 ) = y k+1 + h2 2 U τ k+1 ) d k+1 = h2 2 U τ k+1 ) The important error here however, is the global error, e k = y k yt k ). which is the total error accrued over N steps. In fact, d 3 e 3 d 2 d 1 yt) t 0 t 1 t 2 t 3 Figure 7.7: Global error. e N = N 1 k=1 G N,k d k + d N
Chap. 7. Ordinary Differential Equations CS414 Class Notes 109 where G ij are the magnification factors. Example 7.9 Give a closed-form expression for y N obtained by Euler s method with step-size 1 N y t) = yt), y0) = 1. for Solution where h = 1 N. Therefore, y 1 = y 2 = y k = y k+1 = y k + hft k, y k ) = y k + hy t k ) = y k + hy k = 1 + h)y k, 1 + 1 ) y 0 N 1 + 1 ) y 1 = 1 + 1 ) 2 y 0,... N N 1 + 1 ) k y 0. N Since y 0 = 1, we have y k = 1 + 1 ) k N Example 7.10 For the differential equation y t) = yt), a local error introduced into the numerical solution tn tn) at mesh point t n will have an effect on the numerical solution at some later mesh point t N that is e times greater. Suppose we have a mesh 0 < 0.1 < 0.3 < 0.6 < 1 with local errors 0.02, 0.01, 0.05, 0.04 introduced at the end of each of the four steps in the order given. What is the global error at t = 1.0? Solution We know that the solution of is given by Therefore, the global error at t = 1.0 is 7.4 Systems of ODE s y t) = yt) yt) = γe t. e 4 = 0.02 e 1 0.1) + 0.01 e 1 0.3) + 0.05 e 1 0.6) + 0.04 = 0.02e 0.9 + 0.01e 0.7 + 0.05e 0.4 + 0.04 = 0.183921. A general system of n ODE s can be written as d dt y1) t) = f 1 t, y 1) t), y 2) t),..., y n) t)), y 1) a) = η 1 d dt y2) t) = f 2 t, y 1) t), y 2) t),..., y n) t)), y 2) a) = η 2.... d dt yn) t) = f n t, y 1) t), y 2) t),..., y n) t)), y n) a) = η n..
Chap. 7. Ordinary Differential Equations CS414 Class Notes 110 Alternately, this can be expressed as the following system of ODE s η 1 y η 2 t) = ft, y), ya) =.. η n Euler s method for this system is given by For instance, consider the following system for n = 2 Euler s method is given by y k+1 = y k + hft k, y k ). x t) = ft, xt), yt)), y t) = gt, xt), yt)). x k+1 = x k + hft k, x k, y k ), y k+1 = y k + hgt k, x k, y k ). Example 7.11 Consider the equations r t) = 2rt).01rt)ft), f t) = ft) +.01rt)ft), which represent the rate of change of the populations of rabbits and foxes predator-prey relationship). 1. Is there a particular solution to this problem for which the rabbit and fox population stay constant? 2. How many foxes must there be to bring about a decrease in the rabbit population? 3. What happens to the rabbit population if there are no foxes? Solution 1. For the populations to stay constant which gives the solution f t) = r t) = 0 rt) = 100, ft) = 200 2. A decrease in rabbit population corresponds to the condition r t) < 0, which implies rt)[2.01ft)] < 0 2.01ft) < 0 ft) > 200. 3. If ft) = 0, then which implies exponential growth! r t) = 2rt) rt) = γ e 2t
Chap. 7. Ordinary Differential Equations CS414 Class Notes 111 Note: This is realistic for small population r. For large r, however, there are limiting factors such as availability of food and space. Example 7.12 Determine t n, r n, f n, r n, f n for n = 0, 1 for Euler s method applied to with r0) = 300, f0) = 150 and h =.001. Solution 7.5 Taylor-series Methods r t) = 2rt) 0.1rt)ft), f t) = ft) + 0.1rt)ft), r 0 = 600 0.1300)150) = 3900, f 0 = 150 + 0.1)300)150) = 4350, r 1 = r 0 + hr 0 = 300.001)3900) = 296.1, f 1 = f 0 + hf 0 = 150 +.001)4350) = 154.35, r 1 = 2r 1 0.1r 1 f 1 = 3978.1, f 1 = f 1 + 0.1r 1 f 1 = 4415.95. Euler s method can be improved using the Taylor-series expansion of yt). We know that where yt) yt k ) + t t k )y t k ) + t t k) 2 y t k ), 2 y t k ) = ft k, yt k )), is provided to us. The last term, y t k ), can be obtained simply by differentiating the ODE and evaluating the resulting expression at t = t k. The following example illustrates the concept. Example 7.13 Consider the ODE Differentiating w.r.t. t, we get At time t k, and y = t 2 + y 2. y = 2t + 2yy. y k = t 2 k + y 2 k, y k = 2t k + 2y k y k. Using these equations in the Taylor-series approximation, For t k = 2, y k = 1, h = 0.1, we get and y k+1 = 1.57. The local truncation error is given by y k+1 = y k + hy k + h2 2 y k, y k = 5, y k = 14, d k+1 = h3 y τ k+1 ), 3!
Chap. 7. Ordinary Differential Equations CS414 Class Notes 112 with t k < τ k+1 < t k+1. An error like this is committed at every step, and after a number of steps proportional to 1 h, it accumulates to give a global error of Oh2 ). Hence we have a method whose order of accuracy is 2, which we call 2 nd -order Taylor Method. Example 7.14 For the equation where y2) = 1, determine the value of y 2). Solution From the equation, we have Thus, y2) = 1 y = ty 2 y = y 2 + 2tyy y t) = ty 2 t), y = 2yy + 2yy + 2ty ) 2 + 2tyy. y 2) = 2 1 = 2 y 2) = 1 + 41)2) = 9 y 2) = 2 1 2 + 2 1 2 + 2 2 2) 2 + 2 2 1 9 = 4 + 4 + 16 + 36 = 60. Example 7.15 Given y t) = ty t) + y 2 t), and y1) = 1, y 1) = 2, determine y 1). Solution Differentiating the above equation, and therefore, y t) = y t) + ty t) + 2yt)y t), y 1) = 2 + 2 + 1) + 2 1 2 = 9 Example 7.16 Obtain the equations for one step of the 3 rd order Taylor-series method applied to y t) = tyt). Solution The Taylor-series expansion is given as Therefore, the method is For y = ty, we have Ut) = Ut k ) + t t k )U t k ) + t t k) 2 2! y k+1 = y k + hy k + h2 = y k + h [ y k + h 2 U t k ) + t t k) 3 U t k ) + 3! 2! y k + h3 3! y k y k + h 3 y k )] y k = t k y k, y k = y k + t k y k, y k = 2y k + t ky k
Chap. 7. Ordinary Differential Equations CS414 Class Notes 113 7.6 Runge-Kutta Methods Runge-Kutta methods are a class of methods that were devised to avoid computing analytic derivatives of y without sacrificing the accuracy of Taylor methods. 7.6.1 Runge s Midpoint Method The simplest method in this class of methods can be derived using the observation that yt k + h) yt k ) = tk +h t k y t)dt, where the right hand side requires computing the average value of y t) over the interval [t k, t k + h]. The integral can be approximated in a number of different ways. Euler s method uses the approximation tk +h t k y t)dt hy t k ). A better approximation can be obtained using the value of y at the midpoint of the interval [t k, t k + h] tk +h t k y t)dt hy t k + h/2). y y k+h/2 y k+h y k h/2 h/2 Figure 7.8: Approximating t k +h t k y t)dt by the midpoint quadrature rule. Although it is not possible to compute y t k + h/2), we can approximate it with the value obtained using Euler s method with step size h/2, i.e., where yt k + h/2) is given by Runge s midpoint method y t k + h/2) ft k + h/2, yt k + h/2)) yt k + h/2) = yt k ) + h 2 ft k, y k ). y k+ 1 2 g 1 = ft k, y k ) = y k + h 2 g 1 g 2 = f t k + h ) 2, y k+ 1 2 y k+1 = y k + hg 2
Chap. 7. Ordinary Differential Equations CS414 Class Notes 114 slope = g 2 slope = g 1 y k+1 d k+1 y k t k h/2 t +h/2 h/2 k Figure 7.9: Runge s midpoint method. t +h k 7.6.2 Runge s Trapezoid Method Another way to approximate the integral of y t) on the interval [t k, t k + h] uses the trapezoid rule. The y y t +h) k y t ) k algorithm is stated below. Runge s Trapezoid method h Figure 7.10: Approximating t k +h t k y t)dt by the trapezoid rule. g 1 = ft k, y k ) g 2 = ft k + h, y k + hg 1 ) y k+1 = y k + h 2 g 1 + g 2 ) Example 7.17 Suppose t k = 2, y k = 1, and h = 0.1. Then, g 1 = ft k, y k ) = t 2 k + y2 k = 4 + 1 = 5
Chap. 7. Ordinary Differential Equations CS414 Class Notes 115 slope = g 2 slope = g 1 y k+1 d k+1 y k slope = g + g 1 2 2 t k h t +h Figure 7.11: Runge s trapezoid method. k g 2 = ft k + h, y k + hg 1 ) = t k + h) 2 + y k + hg 1 ) 2 = 2.1) 2 + 1 + 0.1 5) 2 = 6.66 y k+1 = y k + h 2 g 1 + g 2 ) = 1 + 0.1 5 + 6.66) 2 = 1 + 1.166 = 1 + 0.583 = 1583. 2 7.6.3 General 2 nd order 2-stage Runge-Kutta Methods The two methods described above are special cases of general 2-stage Runge-Kutta methods which are 2 nd order accurate. The general form can be rewritten as g 1 = ft k, y k ) g 2 = ft k + αh, y k + αhg 1 ) [ y k+1 = y k + h 1 1 ) g 1 + 1 ] 2α 2α g 2 Clearly, when α = 1/2, we obtain Runge s midpoint method, where g 1 = ft k, y k ) g 2 = ft k + 1 2 h, y k + 1 2 hg 1) y k+1 = y k + hg 2
Chap. 7. Ordinary Differential Equations CS414 Class Notes 116 while α = 1 gives Runge s trapezoid rule g 1 = ft k, y k ) g 2 = ft k + h, y k + hg 1 ) y k+1 = y k + h 2 [g 1 + g 2 ] Observe that the general form can be written as y k+1 = y k + 1 1 ) hy k 2α + h 2α ft k + αh, y k + αhy k ) Also, note that lim α 0 = y k + hy k + h2 2 [ ftk + αh, y k + αhy k ) ft k, y k ) [ ftk + αh, y k + αhy k ) ft ] k, y k ) = f t k, y k ) = y t k ). αh Therefore, as α 0, the method approaches the 2 nd order Taylor s method. 7.6.4 Classical 4 th Order Runge-Kutta Method A popular 4 th order accurate method, commonly known as the Runge-Kutta method, uses the slopes at four specially selected points, to compute the function at the next step, αh g 1 = ft k, y k ), g 2 = f t k + h 2, y k + h ) 2 g 1, g 3 = f t k + h 2, y k + h ) 2 g 2, g 4 = ft k + h, y k + hg 3 ), y k+1 = y k + h This method is also called Kutta-Simpson s method. Example 7.18 Given [ 1 6 g 1 + 1 3 g 2 + 1 3 g 3 + 1 ] 6 g 4. y = 1 + y 2, y0) = 0, calculate yπ/4) with step size h = π/4 using Kutta-Simpson s method. ]. g 1 = ft 0, y 0 ) = 1 + 0 = 1 h g 2 = f 2, y 0 + h ) 2 g 2 = 1 + y 0 + h ) 2 2 g 1 = 1 + 0 + π ) 2 8 1 = 1.1542 h g 3 = f 2, y 0 + h ) 2 g 2 ) h g 4 = f 2, y 0 + hg 3 = 1 + 0 + π ) 2 8 1.1542 = 1.2054 = 1 + 0 + π ) 2 4 1.2054 = 1.8963
Chap. 7. Ordinary Differential Equations CS414 Class Notes 117 Therefore, [ 1 y 1 = y 0 + h 6 g 1 + 1 3 g 2 + 1 3 g 3 + 1 ] 6 g 4 = 0 + π [ 1 4 6 + 1 3 1.1542 + 1.2054) + 1 ] 6 1.8963) = π 4 1 [2.8963 + 4.7192] = 0.99687 6 Here, y 1 has a relative error of 0.3%. In contrast, Euler s method with the step size π/16 gives the solution y Euler) 4 = 0.90188 for the same time instant. This has a relative error of 9.8%. This is due to the fact that Kutta-Simpson s method is 4 th order with Oh 4 ) error whereas Euler s method is only 1 st order with Oh) error. Thus, a reduction in the step size from h to h/2 improves the error in Kutta-Simpson s method by a factor of 16, but only by a factor of 2 in Euler s method.