Sstes of Ordnar Dfferental Equatons Aprl, Solvng Sstes of Ordnar Dfferental Equatons Larr Caretto Mecancal Engneerng 9 Nuercal Analss of Engneerng Sstes Aprl, Outlne Revew bascs of nuercal solutons of ordnar dfferental equatons Error control n sple etods dscussed prevousl Sstes of dfferental equatons Reducng ger-order equatons nto a sste of frst-order equatons Wrtng and usng software for solvng sstes of ordnar dfferental equatons Scedule for Aprl and Ma Monda Tuesda Wednesda Tursda Frda Sprng Brea 5 Quz 7 & PA5 7 5 8 PA 9 Quz 8 Ma 5 PA 7 7 Prog Ea Fnal 8 9 Revew Nuercal Approac Solve ntal value proble d/ = f(,) (nown equaton) wt ( ) = Use a fnte dfference grd: + = Replace dervatve b fnte-dfference approaton: d/ ( + ) / ( + ) = ( + ) / Derve a forula to copute f avg te average value of f(,) between and + Replace d/ = f(,) b ( + ) / = f avg Repeatedl copute + = + f avg Revew Notaton and Order s ndependent varable s nuercal soluton at = f s dervatve found fro : f = f(, ) ( ) s te eact value of at = f(,( )) s te eact dervatve e = ( ) = local truncaton error E = ( ) s global truncaton error If e s O( n ), ten E s O( n- ) 5 Revew Sple Metods Euler: + = + f = + f(, ) Huen s etod f (, ) f (, ) f (, ) f (, ) Modfed Euler etod (, ) f f (, ) ME 9 Nuercal Analss of Engneerng Sstes
Sstes of Ordnar Dfferental Equatons Aprl, Revew t Order Runge-Kutta Uses four dervatve evaluatons per step f (, ) f f f (,,, ) Error (at = ) versus step sze for ODE solvers Error.E+.E-.E-.E-.E-.E-5.E-.E-7.E-8.E-9.E- Error versus Step Sze for Sple ODE Solvers d e -- wt at Euler Huen Modfed Euler Runge-Kutta.E-.E-.E-.E-.E-5.E-.E-7.E-.E-5.E-.E-.E-.E- Step sze, 7 8 Error Error Propagaton n Solutons of ODEs.E+.E-.E-.E-.E-.E-5.E-.E-7.E-8.E-9.E-.E-.E-.E-.E-....8 Euler (N = ) Euler (N = ) Euler N =,) Huen (N =) Huen (N = ) Huen (N =,) RK (H = ) RK (N = ) 9 Error Control How do we coose to antan desred accurac? Want to obtan a result wt soe desred sall global error Can ust repeat calculatons wt saller untl two results are suffcentl close Can use algorts tat estate error and adust step sze durng te calculaton based on te error Runge-Kutta Error Control Tae step wt two forulas of dfferent orders usng te sae functon evaluatons n eac forula Use te dfference between te two forulas as an estate of te error Use te error estate to control te step sze based on a user-nput desred error Soe etods n fnal sldes not sown Dorand-Prnce Metod Uses fourt- and fft-order epressons to fnd error estate Modfes step sze b coparng error estate wt desred error Used n MATLAB solver ode5 wc s n net prograng assgnent Algort equatons sown on net two sldes v. of ode5 code at ttp://users.powernet.co.u/enzle/octave/ atcopat/scrpts/ode_v./ode5. ME 9 Nuercal Analss of Engneerng Sstes
Sstes of Ordnar Dfferental Equatons Aprl, Dorand-Prnce Equatons 5 5 5 + 9 8 + = 5 = 5 = 5 Dorand-Prnce Metod II e s desred error per step opt s optu step sze Tosnor Kura, On Dorand-Prnce Metod ttp://depa.fqu.una./ad/arcvero/dorandprnce_985.pdf Tosnor Kura, On Dorand-Prnce Metod ttp://depa.fqu.una./ad/arcvero/dorandprnce_985.pdf Net Steps We ave sown soe algorts for solvng te ntal value proble d/ = f(,) wt = at = Need to solve ger order equatons Can sow tan an n t -order equaton can be epressed as a sste of n frstorder equatons Ten ave to consder solvng sstes of frst order equatons 5 Basc Idea An n t order ODE can be wrtten as a sste of n frst-order ODEs For a sste of two or ore ODEs wt varng orders: Brea te t ODE wt order n nto n frstorder ODEs; repeat for all equatons Te result wll be a sste of frst order ODEs Te total nuber of ODEs wll be n + n + = te su of te orders of eac orgnal ODE Hger order equatons Loo at sprng-ass-daper equaton as eaple: d / + Cd/ + = F F s appled force wc a be a nown functon of t, and d/ (a ave F = ) Defne a veloct, v = d/ dv/ = d / Our second order equaton becoes dv/ + Cv + = F gvng two ODEs dv C F - v - fvt,, v d v f t,, v 7 dv f Hger Order Equatons II d t,, v f t, v v, d General Notaton d ft, f t, Te two frst order equatons we found loo le our ntal value proble Ts assues tat we ave an ntal conon for veloct, v Wll cover stuatons were ts s not true (nown as boundar value probles) later We can appl all algorts developed for sngle frst-order equatons to an sste of sultaneous frst-order equatons 8 N ME 9 Nuercal Analss of Engneerng Sstes
Sstes of Ordnar Dfferental Equatons Aprl, Solvng Sultaneous ODEs Appl sae algorts used for sngle ODEs Must appl eac part of eac algort step to all equatons n sste before gong on to net step Ke s avng consstent and values n deternaton of f (,) All values n ust be avalable at te sae pont wen coputng te f E.g., n Runge-Kutta we ust evaluate Runge-Kutta for ODE Sste (n) s vector of dependent varables at = n (), (), (), and (), are vectors contanng nteredate Runge-Kutta results f s a vector contanng te dervatves () = f = f( n, (n) ) () = f( n + /, (n) + () /) () = f( n + /, (n) + () /) () = f( n +, (n) + () ) (n+) = ( () + () + () + () )/ for all equatons before fndng 9 ODE Sste b RK d/ = + z and dz/ = z wt () = and z() = - wt =. Detals of frst step fro to () = [- + z] =.[- + (-)] = -. ()z = [ - z] =.[ - (-)] =. () = [-(+ () /) + z + ()z /] =.[ -( + -./) + (- +./)] = -.8 ()z = [(+ () /) (z + ()z /)] =.[( + -.)/ - (- +./)] =.8 ODE Sste b RK II () = [-(+ () /) + z + ()z /] =.[ -( + -.8/) + (- +.8/)] = -.8 ()z = [(+ () /) (z + ()z /)] =.[( + -.8)/ - (- +.8/)] =.8 () = [-(+ () ) + z + ()z ] =.[ -( + -.8) + (- +.8)] = -. ()z = [(+ () ) (z + ()z )] =.[ ( + -.8) - (- +.8)] =. ODE Sste b RK III + = + ( () + () + () + () )/ = + [ (.) + (.8) + (.8) + (.)]/ =.887 (ere = ) z + = z + ( ()z + ()z + ()z + ()z )/ = + [(.) + (.8) + (.8) + (.)]/ =.887 Contnue n ts fason untl desred fnal value s reaced Note all coputed before an + No dependence for f n ts eaple Nuercal Software for ODEs Usuall wrtten to solve a sste of N equatons, but wll wor for N = User as to code a subroutne or functon to copute te f arra Input varables are and ; f s output Soe codes ave one densonal paraeter arra to pass adonal nforaton fro an progra nto te functon tat coputes dervatves ME 9 Nuercal Analss of Engneerng Sstes
Sstes of Ordnar Dfferental Equatons Aprl, Dervatve Subroutne Eaple Vsual Basc sub code for sste of ODEs at rgt s sown below d - d - - e - Sub fsub( as Double, () as Double, _ f() as Double ) f() = -() + Sqr(()) - ()*Ep(*) f() = - * ()^ f() = - * () * () End Sub 5 d Dervatve Functon Eaple d Vsual Basc - - e functon code for d d sste of ODEs - - Functon ff( As Double, () As Double, _ N As Long) As Varant D fd() As Double; red fd( to N) fd() = -() + Sqr(()) - ()*Ep(*) fd() = - * ()^ fd() = - * () * () : ff = fd End Functon In an progra use f = ff(,,n) were f s an arra of sae sze Dervatve Subroutne Eaple MATLAB functon for sste of ODEs at rgt s sown below d - d - - e - functon f = test(, ) f = zeros(,); f() =-() +Sqrt(()) - ()*Ep(*); f() = - * ()^; f() = - * () * (); end %secolons prevent output d 7 Dervatve Subroutne Eaple C++ code for sste of ODEs at rgt s sown below d - d - - e - vod fsub(double, double [], double f[]) { f[] = -[] + sqrt([]) - []*ep(*); f[] = - * [] * []; f[] = - * [] * []; } 8 d Dervatve Subroutne Eaple Fortran code for sste of ODEs at rgt s sown below d - d - - e - subroutne fsub(,, f ) real(kind=8), (:), f(:) f() = -() + sqrt(()) -()*ep(*) f() = - * ()** f() = - * () * () end subroutne fsub d 9 Assgnent Seven and Last Wrte routne n MATLAB and n VBA to solve general sste of N ODEs wt followng for: d / = f (t,), =, N Appl to N = sste wt nown eact solutons sae as on prevous sldes d - d - - e d - e e -t e -t -t ME 9 Nuercal Analss of Engneerng Sstes 5
Sstes of Ordnar Dfferental Equatons Aprl, Assgnent Seven Algorts Code Huen algort n MATLAB Modfed Euler algort n VBA f ( t, ) t t f ( t, ) f ( t, ) f ( t, t t Use MATLAB functon ode5 for solvng sae set of equatons and copare wor for sae accurac ) Runge-Kutta Error Control Followng sldes not sown n class Present oter approac to error control n Runge-Kutta algorts Older etod: Copare results for two steps at step sze wt one step of Requres dervatve functon evaluatons over bot steps Runge-Kutta-Felberg: earl etod to use algorts of dfferent order wt sae functon evaluatons Runge-Kutta Error Control Control error b dong ntegraton wt and all along ntegraton Integraton wt step requres adonal functon evaluatons per steps Analze local truncaton error, wc s O( 5 ), for bot steps 5 ( ) A B ( ) - A 5 B 5 5 - A O( ) Runge-Kutta Error Control II = D s easure of truncaton error User specfes D D, te desred error Man was to specf ts, sngle value, relatve values, relatve to ncreents for n one step Snce error scales as 5, we can adust step sze suc tat new = old D D /D /5 Tpcall use safet factor to avod ang new too large Runge-Kutta-Felberg Uses two equatons to copute n+, one as O( 5 ), te oter O( ) error Requres s dervatve evaluatons per step (sae evaluatons used for bot equatons) Te error estate can be used for step sze control based on an overall 5 t order error Cas-Karp verson and Runge-Kutta- Verner use sae dea 5 Runge-Kutta-Felberg II See page n Rao tet for algort Tpcal forula coponents below n+ = n + ( /5 + 5 /85 = f( n + /8, n + / + 9 /) Error = / - 8 /75 new = old old E Desred /Error / E Desred s set b user RKF5 code b Watts and Sapne ME 9 Nuercal Analss of Engneerng Sstes
Sstes of Ordnar Dfferental Equatons Aprl, Increents,, for RKF f, f, 9 f, 8 9-7 79 f, 97 9 8 85 5 f, -8-5 8 5 859 5 f, - - - 7 55 7 Fnal RKF Step Results Proposed value of + s 5 85 95-5 85 5 55 Estated error s 5 85 95 e - 5 85 5 55 If e n < e + < e a accept + ; eep If e + < e n accept + and get new If e + > e a get new and redo step new = old (ε* old / e + )¼ ε* = desred error 8 ME 9 Nuercal Analss of Engneerng Sstes 7