Numerical Metods for Civil Engineers Lecture Ordinar Differential Equations -Basic Ideas -Euler s Metod -Higer Order One-step Metods -Predictor-Corrector Approac -Runge-Kutta Metods -Adaptive Stepsize Algoritms S U R A N A R E E UNIVERSITY OF TECHNOLOGY Mongkol JIRAVACHARADET INSTITUTE OF ENGINEERING SCHOOL OF CIVIL ENGINEERING Ordinar Differential Equations Rate of cange of one variable wit respect to anoter. were d dt = t = independent variable =dependent variable f(, t ) Initial Condition (t )= k Example: Mass-spring sstem c m d x dx ma + cv + kx = m c kx dt + dt + = x
EULER S S METHOD From Talor series: ( t t) t () = t ( ) + ( t t) ( t) + ( t) +L! Retaining onl first derivative: t () = + f( t, ) were =(t -t ), = (t ),and f(t, )= (t ) t t Predict True New value =Old value +slope x step size t error = + f( t, ) = + f ( t, ) = + f( t, ) i i i i Example: Manual Calculation wit Euler smetod d t, () dt = = 5 4 t Exact solution: = ( t + e ) Set =. t i f (t i-, i- ) Euler i = i- + f (t i-, i- ) Exact Error...4.6 NA -() =-..-(.6) =-..4-(.4) =-.4 initial cond. =..+.(-.) =.6.6+.(-.) =.4.4+.(-.4) =.3..6879.57.465 -.879 -.7 -.65
Implementing Euler s Metod odeeuler.m function [t,] = odeeuler(diffeq,tn,,) % Input: diffeq = (string) name of m-file tat % evaluate rigt and side of ODE % tn = stopping value of independent variable % = stepsize % = initial condition at t= t = (::tn) ; n = lengt(t); = *ones(n,); for i=:n (i) = (i-)+*feval(diffeq,t(i-),(i-)); end d Example: t, (),. dt = = = rs.m function ddt = rs(t,) ddt = t-*; >> [t,] = odeeuler( rs,.6,.,.) t = =..4.6..6.4.3
Exact Solution : >> = (/4)*(*t-ones(size(t))+5*exp(-*t)) =..6879.57.465 >> t=:.:.6; >> = (/4)*(*t-ones(size(t))+5*exp(-*t)); >> plot(t,,t,) Improvements of Euler s Metod Heun s Metod: estimate slope from derivatives at te beginning and at te end of te interval. Euler: i = i- + k slope: k = f(t i, i) slope: k = f(t i-, i- ) t i- t i t
Average slope k + k f ( ti, i ) + f ( ti, = = i ) i = i k + k + Use slope: (k + k )/ t i- t i t Heun s Metod d f(, t ) k = f( t, ) i i dt = i k = f( t +, + k ) i i k = + i i + k Euler Heun True i- t i- t i
Predictor-Corrector Approac Heun smetod: k = f( t, ) i i k = f( t +, + k ) i i k = + i i + k Euler smetod: = + f( t, ) i i i i Predictor: i = i + f( ti, i ) Corrector: f t f t i = i + ( i, i ) + ( i, i ) Iterating te Corrector of Heun s Metod To improve te estimation i f t f t i + ( i, i ) + ( i, i )
Example: Use Heun smetod to integrate From x =to 4,step size = = e =.8x 4.5, () 4 = e e + e.3 Analtical solution: ( ) Predictor, i = i + f( ti, i ) = + (4e.5()) = 5.8 x.5 x.5 x Corrector, = + i i f t f t ( i, i ) + ( i, i ).8() (4e.5()) + (4e.5(5)) = + = 6.7 4.8().5().5() = e e + e = 6.946.3 True value: ( ) 6.946 6.7 Relative error, E t = % = 8.8% 6.946 nd Corrector, = + rd 3 Corrector,.8() (4e.5()) + (4e.5(6.7)) = 6.758, E =.3% t + = + = 6.38, E = 3.3% Error ma increases for large step sizes..8() (4e.5()) (4e.5(6.758)) t
Iteration using up-arrow in MATLAB >> = +*(4*exp()-.5*) % Predictor of = 5 >> = +(/)*((4*exp()-.5*)+(4*exp(.8*)-.5*)) = 6.7 % st Corrector of Press and Enter = 6.7 % nd Corrector of = 6.758 = 6.38 = 6.3555 % 3rd Corrector of % 4t Corrector of % 5t Corrector of = 6.369 % until no cange MATLAB s Implementation func.m function ddx = func(x,) ddx = 4*exp(.8*x)-.5*; odeheun.m function [x,] = odeheun(diffeq,xn,,,iter) % Input: iter = number of corrector iterations x = (::xn) ; n = lengt(x); = *ones(n,); for i=:n (i) = (i-)+*feval(diffeq,x(i-),(i-)); for j=:iter end end (i) = (i-)+*(feval(diffeq,x(i-),(i-))... + feval(diffeq,x(i),(i)))/;
For x =to 4,step size =, () =,Iteration =5 >> [x,] = odeheun( func,4,,,5) x = = 3 4. 6.369 5.3 34.7433 77.735 _true =. 6.946 4.8439 33.677 75.339 Et =..68 3.9 3.7 3.8 Compare wit Euler smetod: >> [x,] = odeeuler( func,4,,) x = = 3 4. 5..4 5.53 56.8493 Et =. 9.8 3.9 4.4 4.54
Comparison of True Solution wit Euler sand Heun s Metod >> xtrue = (:.:4) ; >> true =(4/.3)*(exp(.8*xtrue)-exp(-.5*xtrue))... + *exp(-.5*xtrue); >> [xeuler,euler] = odeeuler( func,4,,); >> [xeun,eun] = odeheun( func,4,,,5); >> plot(xtrue,true,xeuler,euler, o,xeun,eun, + ) 8 6 True Euler Heun 5 4 3 4 x RUNGE-KUTTA (RK) METHODS General Formula using Weigted Average of slopes m γ k = + i i l l l= For Heun smetod γ = γ =.5 Te weigts must satisf m l= γ l = Second-Order RK (Ralston s)metod: i = + k + k 3 3 k = f( x, ) i i i 3 3 k = f xi +, i + k 4 4
Tird-Order Runge-Kutta Metods were i = + k + k + k 6 k = f( x, ) i i ( 4 ) i 3 k = f xi +, i + k k = f x + k+ k (, ) 3 i i Fout-Order Runge-Kutta Metods Te most popular RK metods Classical 4t-order RK: were k = f( x, ) i i k = f xi +, i + k k3 = f xi +, i + k k = f x + + k (, ) 4 i i 3 i = + k + k + k + k 6 ( ) i 3 4
Example: Use 4t-order RK metod to integrate = e = From x =to 4,step size = Step : x =, =.8x 4.5, () k k k k 3 4 = e = 4.5() 3 = + = = + = = e + =.8(.5) 4e.5( 3 ) 4.73.8(.5) 4e.5( 4.73 ) 3.93.8() 4.5( 3.93 ) 5.9457 = + 3+ 4.73+ 3.93+ 5.9457 6 = 6. ( ) 4.8().5().5() = e e + e = 6.946.3 True value: ( ) 6.946 6. Relative error, E t = % =.38% 6.946
MATLAB s Implementation oderk4.m function [x,] = oderk4(diffeq,xn,,) x = (::xn) ; n = lengt(x); = *ones(n,); for i=:n k = feval(diffeq,x(i-),(i-)); k = feval(diffeq,x(i-)+/,(i-)+k*/); k3 = feval(diffeq,x(i-)+/,(i-)+k*/); k4 = feval(diffeq,x(i-)+,(i-)+k3*); (i) = (i-)+/6*(k+*k+*k3+k4); end For x =to 4,step size =, () =,Iteration =5 >> [x,] = oderk4( func,4,,) x = = 3 4. 6. 4.865 33.73 75.439 _true =. 6.946 4.8439 33.677 75.339 Et(%) =..38.5.39.38
Adaptive Stepsize Algoritms MATLAB s ode3 and ode45 Routines ode3 use second-and tird-order RK simultaneousl ode45 use fourt-and fift-order RK simultaneousl Example: For x =to 4, ()=.8x = 4e.5, () = func.m >> x = ; xn = 4; = ; >> [x45,45] = ode45( func,[x,xn],); >> xtrue = (:.:4) ; >> true =(4/.3)*(exp(.8*xtrue)... - exp(-.5*xtrue))+ *exp(-.5*xtrue); >> plot(xtrue,true,x45,45)