You are on page 1of 15
ORDINARY DIFFERENTIAL EQUATIONS Differential equations are mathematical descriptions of how the variables and their derivatives (rates of change) with respect fo one or more independent variable affect each other in a dynamical way. Their solutions show us how the dependent variable(s) will change with the independent variable(s). Many problems in natural sciences and engineering fields are formulated into a scalar differential equation or a vector differential equation—that is, a system of dif- ferential equations. In this chapter, we look into several methods of obtaining the numerical solu- tions to ordinary differential equations (ODEs) in which all dependent variables (x) depend on a single independent variable (1). First, the initial value problems (VPs) will be handled with several methods including Runge-Kutta method and predictor~corrector methods in Sections 6.1 to 6.5. The final section (Section 6.6) will introduce the shooting method and the finite difference method for solving the two-point boundary value problem (BVP). ODEs are called an IVP if the values .x(io) of dependent variables are given at the initial point f) of the inde~ pendent variable, while they are called a BVP if the values x(j)/ x(t) are given at the initial/inal points fy and ty 6.1 EULER'S METHOD When talking about the numerical solutions to ODEs, everyone starts with the Euler's method, since itis easy to understand and simple to program. Even though its low accuracy keeps it from being widely used for solving ODEs, it gives us a Applied Numerical Methods Using MATLAB. by Yang. Cao, Chung and Moris (Copyright 2005 John Wiley & Sons, Ic, ISBN O-171-69833-4 264 ORDINARY DFFERENTIAL EQUATIONS clue to the basic concept of numerical solution for a differential equation simply and clearly. Let's consider a first-order differential equation: y@+tayQ=r — with yO)=yo 1.0), It has the following form of analytical solution: 0 =(~ Shem 6.12) which can be obtained by using a conventional method or the Laplace trans- form technique (K-1, Chapter 5]. However, such a nice analytical solution does not exist for every differential equation; even if it exists, it is not easy 10 find even by using a computer equipped with the capability of symbolic com- pputation, That is why we should study the numerical solutions to differential ‘equations, ‘Then, how do we translate the differential equation into a form that can eas- ily be handled by computer? First of all, we have to replace the derivative y (0) = dy/dr in the differential equation by a numerical derivative (introduced in Chapter 5), where the step-size his determined based on the accuracy require- ments and the computation time constraints. Euler's method approximates the derivative in Eq. (6.1.1) with Eq. (5.1.2) as see — yO) Tt yeth) = +a yet 1 ah)y(t) + hr with y(O) = y» 6.13) ‘and solves this difference equation step-by-step with increasing 1 by h each time from 1 = 0. y(h) = (1 = ah)y() + hr yQh) = (= ahyy(h) + hr 1 ah)yy + hr 1 ah)*yy + (1 —ahjhr+hr (6.14) YGh) = (1 =ah)yQh) + hr = (1 = ayy + S296 1 ah)" hr ‘This is a numeric sequence {)(KH)}, which we call a numerical solution of Eq, (6.1.1). “To be specific et the parameters and the initial value of Bq, (6.1-1) be a = 1, r=, and yp = 0. Then, the analytical solution 6.1.2) becomes (6.15) EULERSMETHOD 265 ‘snn6i0: Euler nethod to solve a tt-order aitterential equation Beth rat; oso tt 3) TE [O:0.01:2"]; ye= 1 - expl-arty; Aq. (8.1.5): true analytical solution ploc(tiyt,"#"), noid on hiasts's (oa a}; nes tr.sklastss yO) = 05 for ite'= 1:3 with various step size h = 1/8,1/4,1/2 last = dlasts(itey; = RB(itr); ¥(1)=¥0; ye 4) = (1 + asnyey(ky hers. SEQ. (6.1.9): Bloe( he 1 kJemy[y(k) ylKe8) 1, By KER, YEKET) 0") inks 4, pause; ena and the numerical solution (6.1.4) with the step-size h = 0.5 and h = 0.25 are as listed in Table 6.1 and depicted in Fig. 6.1. We make a MATLAB program “nn 10.0”, which uses Euler's method for the differential equation (6.1.1), actu- ally solving the difference equation (6.1.3) and plots the graphs of the numerical solutions in Fig. 6.1. The graphs seem to tell us that a small step-size helps reduce the error so as to make the numerical solution closer to the (true) ana- lytical solution. But, as will be investigated thoroughly in Section 6.2, itis only partially true. In fact, a too small step-size not only makes the computation time longer (proportional as 1/h), but also results in rather larger errors due to the accumulated roundoff effect. This is why we should look for other methods to decrease the errors rather than simply reduce the step-size. Euler's method can also be applied for solving a first-order vector differential ‘equation YO =fiey) with ya Yo 6.1.6) which is equivalent to a high-order scalar differential equation, The algorithm ‘can be described by Yeu = Yet Mlle. Ye) with yC00) = Yo 1.7) ‘Table 6.1 ANumerical Solution ofthe Differential Equation (6.1.1) Obtained by the Euler's Method : n=05 025 025 025) = (1 —al)yo + hy = 1/4 = 0.25 050 | (0.50) =(1— abv +r = 1/2 3/4)y10.25) + 1k = 04875 075 8 /4}y10.30) + 1/4 = 05781 1.00 | y(.00) = (1/2905) + 1/2= 3/4 = 0.75 3 /4)910-75) + 1/4 = 0.6836 Las 3/4) 11.00) + 1/4 = 0.7627 150 | 90.30) = (1/299(0.04 1/2-= 7/8 = 0.875 3/4) (1.28) + 1/4 = 08220 266 ORDINARY DFFERENTIAL EQUATIONS os os 4 o4 the (rue) analytical solution 7 02 ° G 05 + ‘12 Figure 6.1 Examples of numerical solution obtained by using the Euler's method. and is cast into the MATLAB routine “ode_Euler()”. function (t,y] = ede_Euler(t tspan,y0,N) SEuler's mathod to salve vector differential equation y'(t) = f(tiy(t)) Stor tspan = (t0,t#] and with the initial value yo and N tine’ steps Sf nargines | N= 0, N= 100; end if nargins8, yO = 0;\end (tspan(2) ~ tspan(1)) /M; Astepsize ‘tspan(1)+{O:N]"*h; Stine vector yO(:)"; “always make the initial value a row vector 1 y(ke# 1y2) = ytkys) thefeval (#,t(h),y(he29)5 MEQ. (6.1.7) end 6.2 HEUN’S METHOD: TRAPEZOIDAL METHOD Another method of solving a first-order vector differential equation like Eq. (6.1.6) ‘comes from integrating both sides of the equation. yn =v) —vG) =f ti.yodr ve =yeyt fo fe,y)dr with yo) =y0 (621) If we assume that the value of the (derivative) function f(ry) is constant as f(f.y(G,)) within one time step [f,.f.,1), this becomes Eq. (6.1.7) (with ‘ku1 ~), amounting to Euler’s method. If we use the trapezoidal rule (5.5.3), it becomes h Yeu = Yet Slflte, Yo) + fla Yeo) 62.2) RUNGE-KUTTAMETHOD 267 function [t,y] = ode_Heun(f, tspan,y0,N) SHeun method to solve vector differential equation y'(t) = F(t.y(t) % for tspan = [t0,tf] and with the initial value yO and N time steps if nargin>abne CAP = -3/8 37/24 -59/24 55/24 cAC = 1/24 -5/24 19/24 3/8 ‘salem ‘% Pradictor/Corrector coefficients in Adams-Bashforth-Noulton method clear format rat (2,L} = Lagranp({-3 -2 -1 0},{0 00 0}); only coefficient polynomial L for = AL = polyint(Lim,:)}; ‘indefinite integral of polynomial cAP(a) = polyval(iL,1)-polyval(it,0); Mdefinite integral over [0,1] end CAP APredictor coefficients (2,L} = lagranp({-2 -1 0 1], {0 0 0 0}); Somly coefficient polynomial for = AL = polyint(Lim,:)}; indefinite integral of polynomial cAc(a) = polyval(iL,1) ~ polyval(it,0); Adefinite integral over (0,1) end ‘CAC Morrector coefficients format short Altematively, we write the Taylor series expansion of ycy1 about fy and that of Y about fee as W a ye + Ee + (64.20) Wea, hha Mew vs Sei + A — SA + k H: Tec Yor Sy bliin = Sh + A208 (6.420) and replace the first, second, and third derivatives by their difference approxi- mations, PREDICTOR-CORRECTOR METHOD 271 h 251 sew i Mlk a + SMa — 59h 1 + S56) + SONY + 48 5 pes + Sot (643) (cia tHe 3h Bn ee -) \ h 4 Wf fa +4 — She + Bist | Maia + (antl as Mage, 5...) Yeu =¥e + Mfg — Sf + fin ) 3 " Bm + 19 sp Sha + 198, + 9fe0) — OND, + ou 19 cp 2 eo IE, (64.3) ‘These derivations are supported by running the MATLAB program “ABMe1 ‘asl another way to get the ABU coefficients together with the error tere clear, forsat rat for 1'= 1:8, [cierri) = difapx(i,{-9 0)); 0(2,1) = e4) err(a) = err; end cAP = [0.0.0 t]+[1/2 1/6 1/24)*c, errp = -[1/2 1/6 1/24) *ere* + 1/120 eAC = [0.00 tJ+[-1/2 1/6 -1/24}*e, erce = -[-1/2 1/6 -1/24] tere’ + 1/120 format short From these equations and under te assumption that {9 ff? © K, we can write the predictor/corrector errors as 251 sgt a 251 gps Epes = Yer post © SoG = SEKI (6440) 19 spe Fossa = Yer ~ Seu % aap y ie (64.40) 272 ORDINARY DFFERENTIAL EQUATIONS ‘We still cannot use these formulas to estimate the predictor/corrector ertors, since K is unknown. But, from the difference between these two formulas 270, 270 270 East — Beast = test ~ Pen = FG KM = Fe Eran = Fy cast (45) ‘we can get the practical formulas for estimating the errors as 251 Epass = Yast ~ Pest = Faq (Geet — Pes) 4.60) 19 ous = Yer — Gen & aq (E41 — Pes) (6.4.66) ‘These formulas give us rough estimates of how close the predicted/corrected values are to the true value and so can be used to improve them as well as to adjust the step-size. 251 Pest > Pe + Fale — Pe) > Meo 47a) 19 Cet Gest — Say (eH — Pes) > Yess (64.7) ‘These modification formulas are expected to reward our efforts that we have made to derive them, ‘The Adams~Bashforth—Moulton (ABM) method with the modification formu- las can be described by Eqs. (6.4.1a), (6.4.1b), and (6.4.72), (6.4.7) summarized below and is cast into the MATLAB routine “ode_ABit()". This scheme needs only two function evaluations (calls) per iteration, while having truncation error of O(/5) and thus is expected to work better than the methods discussed so far. It is implemented by the MATLAB built-in routine “ode113()" with many ‘additional sophisticated techniques. |Adams~Bashforth-Moulton method with modification formulas) Predictor: pxys yt Boh 372 — 591 +558) (43a) 251 Modifier: misi = peri + S4(es ~ px) (64.8b) Corrector: es Yt Alla Shes +19 + 8Kasr md) (64.80) 19 Jeet = Coun — FAH lEeH — Pes) (48d) PREDICTOR-CORRECTOR METHOD 273 function (ty) = ode_ABN(t,tspan, y0,1,KC,varargin) ‘Adane-Gashtortn-lioulton method to solve vector de. y'(t) = f(tyyit)) ‘for tepan = [t0,tt] and with the initial value yo and\N tine steps ‘Using the moditier based on the error estinate depending on KE = 1/0 at nargin < 5, KC = 1} end ‘with modifier by default At margin < 4)| < *'0, N= 100; end Sderault maximum number of sterations yo = y0(:)"; snake it a'row vector = ctspan(2) ~ tepan(t))/N; sstep size tspand’= tspan(t)e(0 3]*hs (ty1 = eea(t, tspano, 0,3, varargin(:}); initialize by Runge-Kutta el tecnisy! ecayinitspan(yy tor k= t24, Fis) = feval(t,t(k)y (ky) varargin(:)); end Bi ylay2)i c= yldys); Rozz © Kov251/270, KEI2 = KO"8/270;, hea © hj2a: neat'= A26*t -5 19 9]; n249 - n20"[-9 37 -59 55]; Dt = ylkyt) m249*F; EQ. (6.4.88) met = ple" no22+(o-p); 369. (6.4- 80) et = yk) nakst (2,2); eval tek + 1) 01 va yi # tyr) = ot RCIZ"(et = pt); MEQ. (8.480) B= pt:' c= ct; update the predicted corrected values Fo Telaidyays teval(rstiies faye ty2) svarergin(:}) 15 P9in{:})15 SEQ. (8-480) 6.4.2 Hamming Method function (ty) = ode_Han(t, tspan,y0,1,KC, varargin) hanning nethoa to solve vector d.e. y(t) = f(ty¥(t)) ‘for tapan = (t0,t#] and with the initial value yO\and N tine steps ‘Using the moditier based on the error estinate depending on KE = 1/0 inargin'« 5, KC = 1} end with modifier by default Af nargin < 4)| N <0, N= 100; end Adetault naxinun number of iterations Af nargin < 3, yO 0;'end saerault anitial value yo = y0(:)"; end snake it a row vector hs ctspan(2)-tspan(1))/as step size tspand = tspan(t)={0 3]°h; [t5y1 = ode. Rea(,tspan0, 0,3, varargin(:}); Initialize by Runge-Kutta el te(nssy?t(a}nstepan(3)}"s for ks 2:4, Fk = 1,2) = fevad( ty tk) y (ky2) varangin(:)) end Be yldys)s c= yay!) had = n/Sha; REIT © RES T2/124; ROBY = KOv9/ 215, fsiz'= Sene(sn 24]! PE YK = 32) + nsAe(2e(R(Ty) + F(3,2)) ~ FR, 29); SEQ. (8.4.80) int = pte KCII*(@ - p)s “eg. (6.490) OF = Gy(k = 242) 6 88y thy 2) giz [(2i8,2); teval fst (ie + 4) m,varargin(:}) 12/8; s€9. (8.4.86) ylirtys) = ot KOBI(et - Bt) ‘eq. (6.4.90) B= pl; c= cl; supdate the predicted/corrected values Fo fr(ara,ay} fevad(tattk + tyay(k + yc] varargin(:) 1; 274 ORDINARY DFFERENTIAL EQUATIONS Hamming method with modification formulas} Predictor poor = yea + BO a — hs +2) (6498) Modifier: miss = peas + eles — pd) (6496) Comer: 4s = HON — yea + IM + 2 + Ag. my D649E) 9 Neer = Cun — Gap (EH — Pes) (649d) In this section, we introduce just the algorithm of the Hamming method [H-1] summarized in the box above and the corresponding routine “ode_Ham()”, which is another multistep predictor-corrector method like the Adams~Bashforth~ Moulton (ABM) method. This scheme also needs only two function evaluations (calls) per iteration, while having the error of O(h') and so is comparable with the ABM method discussed in the previous section, 6.4.3 Comparison of Methods ‘The major factors to be considered in evaluating/comparing different numeri- cal methods are the accuracy of the numerical solution and its computation time. In this section, we will compare the routines “ode_AK4()”, “ode_ABI()”, “ode_Ham()”, “ode23()”, “odess()", and “ode113()” by trying them out on the same differential equations, hopefully to make some conjectures about their performances. It is important to note that the evaluation/eomparison of numer- ical methods is not so simple because their performances may depend on the characteristic of the problem at hand. It should also be noted that there are other factors to be considered, such as stability, versatility, proof against run-time error, and so on. These points are being considered in most of the MATLAB builtin routines. ‘The first thing we are going to do is to validate the effectiveness of the mod- ifiers (Eqs. (6.4.8.4) and (6.4.9b.d)) in the ABM (Adams~Bashforth~Moulton) method and the Hamming method. For this job, we write and run the program “nmé43_1 nto get the results depicted in Fig. 6.3 for the differential equation YO=-yH+1 — with yO) =0 (4.10) ‘which was given at the beginning of this chapter. Fig. 6.3 shows us an interesting fact that, although the ABM method and the Hamming method, even without modifiers, are theoretically expected to have better accuracy than the RK4 (fourth- order Runge-Kutta) method, they turn out to work better than RK4 only with modifiers. OF course, it is not always the case, as illustrated in Fig. 6.4, which PREDICTOR-CORRECTOR METHOD 275 x10 . : fo ‘ - os /tcue analytical soluion yt) = 1~ & 2 ~ | savannas ° 0 (21) Numerical soluions without mestirs os nue analytical solution y0)= 1 ‘and numeral solusong 9 oi (22) Numerical solutions with modifi (@2) Relative erors with mods Figure 63 Numerical solutions and their errors forthe ctferential equation y Hythe 3 2 | tee analytical sotuson y10)= ‘nd numerical sokione rr rr (21) Numerical solutions without modifiers (bt) Relative erors without modifiers x10! x104 3 15 —RKt 2 1} -=2aem beeeed ‘rue analytical sluton 4) fener 6 oa (2) Numerical solutions with mcs (@2) Relative crore with macfirs 3 i 10 — — oder () 2 Seeds () ‘rue analytical sluton yt) =e" 08) ea den 13 (> ~ 1 ee and numerical soions ° _ ot (22) Numerical solutions by oe22, (2) Ther ative erors foded5, ode" Figure 6.4 Numerical solutions and their errors forthe dtferantil equation y(t) =yit) + 276 onowany FFERENTIA EQUATIONS ‘we obtained by applying the same routines to solve another differential equation yO=yQ+1 — with yO) =0 (64.11) ‘where the tru analytical solution is ye) 4.12) ‘son643_1: RK@/Adans/Hamning method to solve a differential eq clear, elt £0 = 0; tf = 10; yO = 0; Sstarting/final tine, initial value N= 50; ‘number of segments teas = inline('-ys1",'t', 'y'); Sdifferential equation to solve #643 = inline('t-exp(t)';'t')} Strue analytical solution for KC = 0:1 tic, [£1 yR] = ode_Rka(dF649,(t0 tf],y0,N); tR = to tic, [t1yA] = ode_AaM(ateaa, [to tf],0,N, Ko); tA = toc tic, [t1,yH] = ode Ham(at6aa, [to tf],y0,NKC); tH = toc ytt'> f643(t1); strue analytical solution to plot Subplot (221 + Ko*2) ‘plot analytical/nunerical solutions LOE (EH yt, Ky TYR, "Ky tTyyAy "kes, t1yyHy 2") ‘mp = abs(ytt)seps; 1_t1 ='zength(t1); eR = abs(yR - ytt)=/tip; © Renorm(eR) /2t1 eA = abs(yA - ytt)./tmp; @_A=norm(ea) /1t1 eH = abs(yH - ytt)=/tmp} e_H=norm(ek) /1t1 subplot (222 + Ko%2) ‘plot relative errors plot(tt,oR,"k', t1,eA,'k--', thy ely "k:") end ‘Snne49_2: 9de29() /ode45() /ode119() to solve a differential eq clear, elf 0; tf = 10; yO = 0; N= 50; Astarting/final tine, initial value inline('y + 1','t','y'); differential equation to solve inline(‘exp(t) | 17,8); strue analytical solution tic, (11 yA] = ode Ake (dFe4s, [20 tf] ,yO,N) time(1) = tocs tic, [11 yA] = ode ABM(dFe43, [20 tf},y0,N); time(2) = toc; yet =" #848001); ‘tmp = abs(yt1)+ eps; 1t1 = Lengeh(tt); abs(yR-ytt) [tap ere(1) = norm(of) (11; abs(yA-ytt) [tmp ere(2) = norm(oa)/1ot1; options = odeset("ReiTol' ,1¢-4); set the Tolerance of relative error Te, 123, yode23] = ode2s{aFe43, (20 tf],yO,options); time(3) = tec; tic) (245, yodeas] = odeds(ate43, (x0 tf], yOsoptions); time(s) = tec; tic, [t113,yodet13) = ode113(df64a,(t0 tf],yO,options); time(s) = tocs yt2a = ¥643(t25); tmp = abs(yt2s) + eps; ole23 = abs (yode2s-yt28)./tmp; err(3) = nora(eede23) /Lengtn(t23); ytas = f643(t45); tmp = abs(ytés) + eps; Godeas = abs(yode4s = ytas)./tap; err(4) yttis = f643(2113); tmp = abs(ytt13) + eps; e0de119 = abs(yodet!9 - yt113)./tmp; err(5) = norm(eodet1) /Length(t118); subplot(221), plot(t23,yode23,'k', t45,yodeds, 'b', t118,yodet13, 'r') subplot(222), plot(t23,e0de29,"k', t45,eoded5, 'b--', t113,e0de118, r nora eodees) /Length(t4s) ; VECTORDIFFERENTIAL EQUATIONS 277 Table 6.2. Results of Applying Several Routines to solve a Simple Differential Equation Readers are invited to supplement the program “nm643_2.n" in such a way that “ode_Ham()” is also used to solve Eq. (6.4.11). Running the program yields the results depicted in Fig. 64 and listed in Table 6.2. From Fig. 6.4, itis note- worthy that, without the modifiers, the ABM method seems to be better than the Hamming method; however, with the modifiers, itis the other way around or at least they run a neck-and-neck race. Anyone will see that the predictor-corrector methods such as the ABM method (ode_Asi()) and the Hamming method (ode_Man()) give us a better numerical solution with less error and shorter com- tation time than the MATLAB built-in routines “ode2a()”, “ode45()”", and ‘ode113()" as well as the RK4 method (ode_RK4()). as listed in Table 6.2. But, ‘a general conclusion should not be deduced just from one example. 6.5 VECTOR DIFFERENTIAL EQUATIONS 65.1 State Equation Although we have tried using the MATLAB routines only for scalar differential ‘equations, all the routines made by us or built inside MATLAB are ready to entertain first-order vector differential equations, called state equations, as below. VO = fil. 21(0), 220, ---) with 24(09) = 10 (= fat, x1(0), 2(8),--.) with x20) x) =AE,x0) with x) =x 5.1) For example, we can define the system of first-order differential equations WO =a) with x1(0) = 1 | 65.2) (= —n) +1 with 20) = in a file named “df651.n" and solve it by running the MATLAB program “am6S1_1.n", which uses the routines “ode Ham()"/ode4s()” to get the ‘numerical solutions and plots the results as depicted in Fig. 6.5. Note that the function given as the first input argument of “ode45()” must be fabricated to generate its value in a column yector or at least, in the same form of vector as the input argument *x’ so long as it is a vector-valued function.

You might also like