You are on page 1of 24
Numerical Solutions of Differential Equations A Tutorial Henrique Fleming 27-2-2002 1 Introduction ‘This is a tutorial on solving the ordinary differential equations of mechanies by numerical methods. I will be very straightful: we will learn how to solve second order ODE (ordinary differential equations) using a very good routine developed, years ago, by Shampine and Gordon. It is based on the Adams method, but we couldn't care less. Sulfice it to say that the routine is of excellent reliability. Tt is in Fortran. If you don't know Fortran, it’s OK. Fortran is the best language to ignore, as you can learn it in two days: I know, I learnt it in three days, and Tam really siow! Don't try to understand the routine: it is enormous and sophisticated. Tt does the following: you supply the differential equation, the initial conditions, the initial and end values of the independent variable (the time, in our ease) and the precision you want. The routine then calculates the integration step so that. the required precision is attained, and produces the position and velocity at the end point, (Actually, the step varies along the computation, being determined anew at each step). Tn case you want to know more about the numerical treatment of ODE'S, take a good Tok at Hamming, Numerical Methods for Scientists and Engincers, ‘This is a great book! Its chapter on diferential equations is illuminating, even if you don't care about numerical methods, 2 Basics of the method ‘Suppose the equation is dy Fa feud a ‘There are four things to be done: L.Write a Fortran routine informing the program which ODE is supposed to be integrated. Call it deries.f. say. 2.Fetch the Shampine-Gordon routine ade.f. 3.Write the Fortran program (what do you want to do with the equation). Call it main.f, say. 4.Compile everything, with a command such as this: 977 main.f ode.f derivs.f -o main.out The executable will be called main.out. This is, of course, the optimistic version, Probably error messages will crop up, as always. .. You'll have to debug, (I think it was Marvin Minsky who said “Programming is debugging a white sheet of paper!”) Let us proceed by parts. 2.41 derivs.f Suppose the equation is dy at Slu.t) @ with f(y.t) = yy. We have to write this equation in the way required by ode.f. This means writing a routine like this: subroutine deriva(c,a,b) integer nt parameter (nt=1) double precision a(nt),b(nt),c b(1) = a(t)*a(t) return end In this subroutine, y is represented by the array a(nt), and 4 by the array bint). In this ease , as nt=1, this seems ridiculous, but we will soon see that. the normal case requires ntz 1. $o, the line D(A) = a(t)eact) is tolling that 4 = y?, as it should Consider now the second order equation &y dy eS) @) ‘The best way to solve it is transforming it into a system of two first order equations. Suppose f(y, #0) = y?. The ttick is defining an auxiliary variable tHirough 4 y= v=4 @ which allows Eq.(3) to be written # ew 6) 2 = syw,p © so that, in our particular case, we would have Bow « 2 wv (8) Let's put this in the language required by ode.f. Here we will have two equations, so nt=2. The array a(2) will have two elements, a(l) uv (9) aQ) = w (ao) whereas the array b(2) will be given by ay = (uy 02) = (a2) a ‘The subroutine derivs.f will then be written; subroutine derivs (c,a,c) integer at parameter (at=2) double precision a(nt), bint), ¢ b(A)= a(2) (2) = a(1)*a(t) return end Let us now move to a more concrete example: a particle moving under the action of the gravitational field, We treat the problem as a 3-dimensional one for generality (though it is 2-dimensional, the orbits being plane curves). ‘The equationas are: cr mM mG =~ (a3) or, written in components, Cx mM x mit = gills (14) ey | _guMy 5 mae or cy @ mM = mp - ORF (16) So we lave thnee secomborder difforential cqnmations, In tertus of first-order cenes, we will have six. Sc, in this ease mt=6, and the arrays a(®) and bC) will rave six elements each. Withont more aco, we write the subroutine denn sf for this case. subroutine derive(c, a, b) integer ot parameter (nt sion a(nt), Bint), ¢ double precision G, r parameter (621,00) Tedsqrt (a (1)424a (3) *424a(5) 442) b()=a2) Ga (L/S where an arbitrary value was given to G. This is doubl what explains de dsqrt. whidh is the donblesprecision versicn of the square-root fimetict, Se mnch for the derivs,f rettine, 3 The main routine This is called adesf and dees all the dirty job. Den't try to hack itt as it it was so nach tested as to he considered the most reliahle routine available, being used to contro critical instraments in hospitals for instance, You cans it as it is subroutine cde(E ne implicit real+8(-h, yt tout ,relerr,abeerr, iflag work, iverk) on subroutine ordinary diftere ay(/at = £t,¥D) 7D... sylneqn)) yG) given att. subroutine integrates from t to tout . on return the ers in the call list are set for continuing the integration. ly to define a new value tout and call ode again. double pre the differential equations de, step, and intrp . arrays work and iwork and calls de. de is a supervisor which directs the cclution, it calls on the routines step and intrp to advance the integration and itput points. step uses a modified divided difference form cf the adams pece formulas and local extrapolation. it adjusts the order and st ‘the local error per unit step in a generalized normally each call to step advances the solution one step in the dir of tout . for reasons of effi: de integrat nd tout internally, though never beyond t10(tout-t), and calls intrp to interpolate the solution at tout , an option is provided to stop the integraticn at tout but jould be used only if it is impossible to continue the inteeration beyond tout . this code is completely explained and dccumented in the text, mputer solution of ordinary differential equations: the initial value problen by 1, £. shampine and m. k. gordon, ‘the parameters represent £ == doukle precision subroutine £(t,y.yp) to evaluate derivatives ypG=dy() /at © be im vector at t independent variab! point at which solution is desired relerr,abserr ~~ relative and abeclute error error test (real*), at each step the ot component. of the local errer and solution ve: indicates status of integration —_Cinteger*4) work() (real*) == arrays to hold information internal iworkG@} Gnteger*4) which is necessary for subsequent calls first call to the user must p in the call list, ytnegn}, worke(100#21neqn), iwork(5), declare f in an external statement, supply the double precision subroutine #(t.y,yp)_ to evaluate ay(D/at = ypG) = £7,975... .yCneq)) and initialize the parancter: nega == nunker of equaticns to be integrated initial conditions ide storage in his ling program for the arrays starting po: point at whi solution is desired c relerr,abserr =~ relative and absolute local error tolerances iflag -- t1y-1, indicator to initialize the code, normal input +1, the user should set iflag=1 only if it is impossib! beyond tout . all parameters neqn and tout may te altered by the code on output co must te variables in the calling progran output from cde == nega ~= unchanged yG@) -* soluticn att {== last point reached in integration, normal return has t = tout . tout “= unchanged r,akserr == normal return has tclerances unchanged. iflag=2 signals tolerances increaced iflag normal return, integration reached tout ation did net Teach tout because error small, relerr , 2b: appropriately for continuing ation did net Teach tout because more than ation did net reach tout because equations appear to be stiff = 6 -+ invalid input parameters (fatal error) the value of iflag is returned negative when the input value ig negative and the int ation does reach tout , Lees, 73, 4, “5 work(+) ,isork(+) + information generally of user tut necessary for suksequent calls. subsequent calls to od subroutine ode returns with all information need: ‘the integration, if the integration reached tout , the user need only define a new tout and call again. if the integration did not veach tout and the user wants te continue, he just calls again. ‘the output value of iflag subsequent calls. the only situation in whi is to stop the integraticn internally at the new tout , i. change output iflag=2 to input iflag=2 toleran: be changed by the user tefore continuing. all other parameter remain unchanged. to continue ld be altered Peete nnneeecnneee teen tentee aeeeeetceneentes (iteeee etteeetetteernennag subroutines de and step * contain machine dependent constants co be they are CH ieeien pone nonin bien oo sesion tasenieseno ners logical start phasel znornd dimension yneqn) swork(1) ,iwork(5) external f data ialpha,ibeta, isig,iv,iv.ig,iphase,ipsi,ix,ib,ihold,istart, dy idelen/1,13,25,28,50,62,75,75,88 89,0,31,92,2/ ip = vt + neqn aphi = iypout +n: if Gats (flag) 1 start = workGstart) .2t. 0.0d9 Phasel = work(iphace) nornd = iwork(2) ne, =1 4 call de(E neqn,y,t,tout relerr abeorr, iflag,work yy), 1 workCiwt} sworkCip) ,work Gyp) swork(ypout) sworkGrbi) » 2 work(ialpha) ywork beta) work sig) swork (iv) ,workCw) jwork(ie). 3. phaset ,work(ipsi) work (ix) ,work(ih) jworkGhold} start, 4 work(itold) ,work(idelen), iwork(1) snornd, ivork (3) ,iwork’ 5 iwork(S)) work(istart) = -1.0d0 if (start) work(iphase) = -1,040 if (phase!) work(iphase) = 1,049 awork(2) = =1 nd) iwork(2) = 1 tine de(£ nega, y,t,tout relerr akserr,iflag, 1 yyy yp.ypsypout ,phi alpha, beta,sig,v,W,g,phasel psi,x,hyhold, 2. Start ,told,delsgn,ns nornd,k,kold,isnold) inplicit real»8(a-h,o-2) ely allocates storage for enience of a long call list. thie computer solution of ordinary differential equations: the initial y explained and & value problen by 1, £. shampine and m. k. gor id logical stiff,crash,start phase! ,no dimension y(neqn) ,yy (neqn} ,wt(neqn) sphi (neqn, 16) ,p(negn> ,¥p(neqn) 1 ypout (nega) ,psi (12) ,alpha (12) ,beta(12) »sig13) ,¥(12) sw(12) 243) external f c Peeetrrnteeeteeeeee teen tentee inten teat eeenentineeee eeteeteeetenrennngd c+ ‘the only machine dependent constant is based cn the machine unit + c* roundoff error u which is the smallest positive number such that + ce 1.0 gt. 1.0. u must be calculated and fouru=4,0eu inserted + c in the following data statement before using de. the routine + c# machin calculates u. fourn and twou-2.ceu must also be * c in subrouti before calling de. * 2a-15/ ChbHeaao eb iobbipsrobiapt io iaoar ab iooiapiiaoniao dab tape aob rep ait c ‘the constant maxnum is the maximum number of steps allowed in ont 11 to de. the user my change this limit ty altering the following statement data maxnum/500/ test for improper parameters fouru = 4,0 * dimach¢4) if(neqn .1t. 1) 19 f(t eq. tout) go to 19 if(relerr .1t, 0.040 vor. abserr .1t. 0.040) go to 10 eps = daxl(relerr abserr) if(eps «le, 0,040) go to 19 ifGflag .eq. 0) go te 10 e en(1,iflaa) flag = iabe(iflaz) afGflag .eq. 1D go t f(t ne. told) go to 10 ifGflag ge. 2 and. iflag se. 5) go to 10 flag = 6 return ch call set interval of in steps, adjust input error toler: subroutine step ation and counter for number of define weight v for 20 del. = t + 10,000» afGen lt, 0 ter = 0 ifGflag .eq. 1) go to 20 ifGsnold .t. 0) go to 30 isgnedel sgt, 0,Cd0: go to 60 on start and restart also set work variatles x and direction of integration and initialize the ster size (GQ, store the 20 start = struc. xet do 40.1 = L.neqn 40 yy) = 7) delegn = dsign(1.0d0,de1) hh = daign dnaxt (abs (tout-x) ,fourutdats (0) ,tout-2) if already pact output point, interpolate and return 50 if (dats(eet) .1t, absdel) go to 60 call intrp(x,yy,tout,y,ypout eqn, kold phi ,psi) iflas = 2 t = tout told = t return if cannct go past output point and eufficiently close, extrapolate and return ifGen wet, 0 b= tout ~ x call £(2,y9,9p? de 70.1 = Linen 70 y(1) = yy} + beypQ) iflag t= tout told = t isnold = isn return r. dabe(toutex) .ge, fourwdabs(n)) go to 80 test for too many ste £0 if(mostep .1t, maxnum) go flag = isned ifGStiff) iflag = isms 61 = Lynegn y@) = w@ Limit step size, set veight vector and take a step 100 h = dsign(dnint abs n> ,dabe de 110 1 = Lyneqn 110 wt) = relepesdats( call step(tsyy,f snemsby 1 hold,k,kold,¢rash,Phi,p,ypspeiy 2 alpha,beta,sig,vw,g,phacel ns, end-x)) sh} test for tolerances too small ifGnet.crash) go to 130 flag = isnt3 120 ket + 1 dat. 4) if (led ‘ge. 50) subroutine step(x,y.f,neqnyh,eps,wt start, 1 hold,k,kold,crash Phir, yp.PeD) sh,Phi,P»JPsPSiy 2. algha,teta,sid,v,w,g,phacel jus, implicit real*6(sh,c-2) double pt -n subroutine integrates a systen of first order ordinary , Rormally from x to xh, using a difference form of the adans pece formulas. lecal used to improve absolute stability and jjusts ite order and step size per unit step in a generalized sense, special devices are included to control roundeff errer and to detect when the user is requesting ‘too much accuracy. thie code is completely explained and decumented in the text, conputer solution of ordinary differential equations: the initial value problen by 1, £. shampine and m. k. gor ‘the parameters represent independent variab’ solution vector at x (reals) p size for next step, normally determined by (real2) must be variable (reals8) criter: (reals8) » false. (owical=4) essful step (xeal+8) ietermined by code) ‘ul step then no step can be taken, phi, pei are required £ intrp. the array p is internal to t! the interpolation subroutine all are real+s input te step first storage in his driver program for all arrays in the call list, nanely dimension y(neqn) ,wt eqn) »phi cnegn, eqn) syptneqn) psi (2) the user must also declare start and crash logical variables and { an external subroutine, upply the subroutine £(3,y,yp) to evaluate ay(G) /ax = yp) = £60,y(1),y(2),..- ,y(nom)) I initial values of dependent variables equations to be integrated minal step size indicating directicn of integration and maximum size of step. must te variable variable the for his proble we) = 1.0 sp dabs(y@)) relative to the most recent value of the I-th component of the solution, dabs(yp(}) error relative to the most the I-th component of the derivative, = dnaxi (wt@) dabs(y@))) error relative to the lar magnitude of I-th component obtained dabs(y@) }arelerr/eps + abserr/eps 5 relative-absolute test error, atserr is abs amaxi (relerrabserr) « ris relative ror and eps subsequent calls -- designed so that all information needed egration, including the step size h and the order taith each step. with the the step and the weights, the paraneters should be altered. the array wt must te updated after each to maintain relative error tests Like tho: egration is continued just beyond the solution interpolated there with subrcutine intrp . impossible to integrate beyond the endpoint, the step size may reduced to hit the endpoint since the code will not take a step larger than the h input. changing the direction of intetration, é., the sign of h , requires the user set start = true, ‘ling step again, this is the only situation in which should te altered. output £ step successful s! ‘the subroutine returns after cach successful step with start and ach set false. . x roprecente the independent variatle advanced cne step of length hold from its value on input and y the soluticn vector at the new value of x. all other rarancters represent information corresponding to the nev x needed to continue the int tien the small for the machine precisicn, ‘the subroutine returns without taking a step and crash an appropriate step size and tolerance for continuing estimated and all other information is restored as upcn input before returning. to continue with the larger tolerance, the user just calle the code afain. a restart ie neither required nor desirabl: ‘ical start,crash,phase! znornd dimension y(neqn) ,wt (negn} ,phi (nean, 16) ,p(neqn) .yp(negn) 788 (12) dimension alpha (12) ,beta(i2) ,2ia(13) wA2) ,vC12) 813) 5 1 str (13) ,two12) external f Ceereeeneeteestrernsnreerent trernreerert irri enreetensTrerertterererrcy c+ the only machine dependent constants are based on the machine unit + c+ roundoff error u which is the smallest positive nunter such that ce 1.0f gt, LC. the user mst calculate u and insert c# ‘twous2,0eu and fourue4,0+1 in the data statement before calling c+ the code. the routine machin calculates u data tou, fouru/ 4444-15, 8884-15/ data two/2.0d0,4.0d0,8,040,16.0 1 512.030, 1024.040,2048.0d0 ,4096..040, 81% data gstt/0,50040,0.083340,0,.041740 50.0. 1 0,011440,0,00826d9,0,00729d0 ,0,0067840,0. 2 0,00as8a0/ data g() 82/1 245/,S3gC1)/1.9/ ‘tou = 2.0 * dimach(4) ee fouru = 2.0 + t a see begin bl check if step size cr error telerance i too emall for machine precision. if first step, initialize phi array and estimate a starting step size, if step size is t 5 if error tolerance mall, determine an acceptable one crash = .true, ifGats(h) .ge. fourutdabe(®)) go to 5 h = deign(fourusdabs (x) ,h) return pEeps sdO*eps 1S too small, increase it to an acceptable value initialize. compute appropriat size for first step call £(x.y.¥E) sum = 9,Cd 201 = Lyneqn phiG.1) = yp@) Phi@,2) = 0.009 sun = sum + (yp()/wt(a))**2 sun -t (sum) aksh = dabs(h) if (eps .1t, 18.0d0tsumeh¥h) abeh h = dsign (drat (absh, fourwedabs(x)) ,h) hold co BdOxdsqrt (epe/sun) strues gt. 109.Cd0*round) go to 99 false, Lnecn phi(1,15) = 0.00 99 ifail compute ents of formilas for this step, avoid computing ‘those quantities not changed when step size is not changed. 100 kp = keHL kp? = ke kml = kel bn = kB ns is the mmter of si with size hy including the cun one, when k.It.ns, no coefficients change if th ne, if(ng.le.kold) ns=nett nspi = ns+ if (k .It, ns) go to 199 compute those components of alpha(+) ,beta(+) ,psi(+) ,eie(#) which are chan beta(ns) = 1.030 realns alpha(ns) = 1,Cd0/realns tempi = herealn e(nsp1) = 1.040 ifGe 2b. napl) a do 105 i = nepl,k 2 110 pei Gm) beta(i) = beta(iml)~psi Giml) /temp2 tompl = tenp2 +b alpha) = h/templ reali = 4 195 sigGH1) = realivalpha(i)+sig(i) 110 psi) = tempt compute coefficients 24} initialize v(«) and set m(«). set in data statement if (as .gt. 1) go to 120 de 115 iq = Ak temp? = igeCigtt) v(ig) = 1.030/temp3 115 wig) = vGq) go te 140 cif orde was raised, update diagonal part of v(+) 120 4ftk «le. ) 130 tenp4 = keket end iftasm2 .1t. 1) & 25 j = Lyne 165 125 (i) = v(4) = alpha(j+1)40GH) 0 130 update w(«) and set w(*) 130 vGg) ~ temps+vCiget) 138 wGq) = vGq) g(nepL) = vA) © compute the g(@) in the work vector w(+) 140 nsp2 = ns + 2 iftkel .1t, nep2} go to 199 do 150 i = nep2,kpl Limit? = kp2 = 3 tempé = alpha(i-1) a MS ig = 1 init? wGg) = Cig) = tenpseaCigtt) a(i) = w(t) continue wee end block Le ewin block 20 as fict a solution p(+), evaluate derivatives using predicted solution, estimate local error at order k and errors at crders k, kel, k-2.as if constant step size were used. change phi to phi star 16 afk sIt. do 210 tynegn 205 phi (1, i} = tenplephi(2,i) 210 continue PQ) = pG) + temp2ephiGi) Phi(1,i} = phil,i) + phiC,ipt) continue if noma) 280 do 235 1 = Lyneqn tau = bep() - phils) p(1) = v1) + tau 295 phi@.16) = (pa) = ya)) - tau & 200 de 245 1 = Ljneqn 245 pd) xold = x rexth atch = dabe(h) call £6,p,7p) 1) + hepa) © estimate errors at orders k,keL,ke2 -qn 12605265 rian? + ((phi (1 ,kmt)+temp4)*temp2}++2 erknl + ((phi(1,)+temp4) +tenp3)++2 Be i erk = erk + ( if Cam2}.280,,275 orld = abchesig(kml)+getr (kn2)+deqrt (erkn2) abshesig(k)agstr Ganl )sdzqrt (erin!) sbehtdeqrt (cri) tonp5*(e(k)-a(kp!)) Basig(kpl)*gstr (k) pastenps) +42 test if order should 2 lowered if Gand) 26 £Camext ge to 20% 290 ifterkmt . 2 iOserk) 1a © test if step successful ifCerr «le, eps) go we end begin bi is unsuccessful. restore x, phi(«.#), Psi(*) « utive failure, set order to one. if step fails more ines, consider an optimal step size. double error tolerance and return if estimated step size is too small for machine ifk It. 2D do 315 i = 2k SIS psi(i-l) = psi’) - bh on third failure, size thereafter, use optinal st fail = ifail + 1 tonp2 ifGfail 225 if Peps knew = 1 b= tomp2eh k = knew ifGaks(h) .ge, fourusdabeGD) go to erach = .true, dsign (fourusdabs (0 sh) rect the predicted solution, evaluate using the corr: solution and update the determine best crder and step size for next step. rect and evaluate templ = heg(kpt) = templ+(yp@2) = phi(1,1)) - phi, 16) +r 408 = pad) = tho 410 418 yQ) = pC) + tempi+Gpa) = pria,1)) 420 call £(x,9,9P) update differences for next step do 425.1 = Ayneqn phil ket) = yp) - phi.) 428 phi(lkp2) = phi(,kpl) - phi(.,kp2) do 435 1 = Lyk do 430 1 = tynegn 430 phi(1,i) = phi(,i) + phi(,kp1) 435 continue in first phase when alway already decided to lover order, step siz constant = erkpl = 0.040 if(knew .eq. kml or. if(phaset) go to 45 iftknew .eq, kml} go to 455 iftkel vet, ng) go to 460 ao 44 ‘qn 440 erkpt = erkpl + (phi (1jkp23/ut (142 erkpl = absheastr (kpl)+dsqrt (erkp1) using estimated error at order tL, determine appropriat for next step if Ck gt. 1) go to 445 if(erkp1 .ge. 0.540+erk) go to 460 go to 450 445 if(erimt «le. dmint(erk,erkp1)) go to 455 if(erkp! .ge. erk .or. k .eq. 12) go to 460 her b in block 2, thus order is to be rai: rai: 450 k = K lower order 455 k = kat ork = erknt ith sr determine appropriate step size for next step 460 hnew = h +h if(Phasel) go to 465 LfGBepe ge. erleetwo(k+1)) go to 465 bnew = bh Sf (pSeps erk) 465 venp = k+l r= (pe fori’) +*(1.040/teme2) x thie code is completely explained and dccumented in the text, mputer solution of ordinary differential equations: the initial value problem by 1, £. shampine and m. k. gordon, input te intrp <= all floating point variables are double p the user provides storage in the calling proaram for the arrays in the call list ion y(neqn} ,yout (neq) ,ypout (neqn) yphi (neqn 16) psi (12) and defines xout the remaining parameters are defined in step and passed to intrp from that subroutine point at which solution is desired. output from intre = yout(*) =» solution at xout. ypout(+) =- derivative of colution at xcut ‘the remaining paraneters are returned unaltered from their input integration vith step may be continued. dimension (13) (43) ,rho(13) data g(1)/1,0d0/ ,rho(13/1,040/ hi tox kis kold+1 Kipl = ki +1 initialize w(+) for computing g() i= Lki 2 tenpl = 4 5 wi) term = 0.030 temp compute £(®) do 15 5 cml peij i= kipi 2 j eng? = 2G) temp3 = rho(i) 5 l= iyneq yout (1} = yout (2) + tempephi (1,3) C ap 4 The calling routine rely up to you. I describe here of the prowam that § te drawe a Kepler cabit, Now comes the p programm Tw1ct PROGRAM kepler calcula uma orbita INTEGER nneqn, i 2 PARAMETER (nneqn- DCUBLE FRECISION yy(nnoqn) .tt ttout srrelerr aabserr REAL# wwork(100+21*nnegn} INTEGER*4 iiflag,iivork() EXTERNAL f EN (unit=12, file="kepler.dat?) aiflag=t (deri % work, iivork) » nneqn, iiflas, ttout=tt+0. 10-1 arite(l2,*) y@).yyD 1d do CLOSE (unit=12) stop EXE Together with the comments in the subroutine ede, it should be sexplanatcrys Tuse the samme names ae the arguments of ade¢), cly doubling the finst Tetter The exception is the variable f. which [call derivs, For examples he areument y in the definition of cle is called. in the calling reutine, yye refer becomes reelerr, and s6 ot. 5 What to do? 1. Using copy and paste, cteate the files d keplersf. in the same directory, 2.Excente the commenck 977 hepler.f odef den 3,Esecute the command: krpler.out LEsccute the command: gnuplot 5.In grmplot, execute: plot “hepler.dat wsify clef atl in this case, <0 heplersont 23 Thats it. A beautiful cllipse shonld be exhibited, If you need to ficshen up your Fertran, there is a geod link in uny site 2

You might also like