Ce206 Ode
Ce206 Ode
dv cd 2 = g v g dt m
Eulers Method
The first derivative provides a direct estimate of the slope at ti:
dy = f (t i , yi ) dt ti
Euler method uses that estimate as the increment function: f
= f (t i , yi )
yi+1 = yi + f (t i , yi )h
if nargin<3,error(less than 3 input arguments'),end ti = tspan(1); tf = tspan(2); t = (ti:h:tf)'; n = length(t); % if necessary, add an additional value of t % so that range goes from t = ti to tf if t(n)<tf yi +1 = yi + f ti , yi h t(n+1) = tf; n = n+1; end y = y0*ones(n,1); %preallocate y to improve efficiency for i = 1:n-1 %implement Euler's method y(i+1) = y(i) + dydt(t(i),y(i))*(t(i+1)-t(i)); end
cd 2 dv =g v dt m
60
50
Analytical solution:
v(t ) = gcd gm tanh m t cd
40
v (m/s)
30
20
10
10
12
t (sec)
CE 206: Engg. Computation Sessional Dr. Tanvir Ahmed
Heuns Method
Predictor:
yi0+1 = yi + f (ti , yi )h
Exercise: Modify eulode.m to implement Heuns method. y p Solve the same bungee-jumper problem.
CE 206: Engg. Computation Sessional Dr. Tanvir Ahmed
where
k1 = f (t i , yi ) 1 1 k2 = f t i + h, yi + k1h 2 2 1 1 k3 = f t i + h, yi + k2 h 2 2 k4 = f (t i + h yi + k3h) h,
if nargin<3,error(3 input arguments required'), end ti = tspan(1); tf = tspan(2); tp = (ti:h:tf)'; n = length(tp); % if necessary, add an additional value of t % so that range goes from tp = ti to tf if t ( )<tf tp(n)<tf tp(n+1) = tf; n = n+1; end
Practice Problem
Solve the following initial value problem over the interval from t = 0 to 2 where y(0) = 1. Display all your results in the same graph
dy = yt 3 1.5 y dt
(a) Analytically (b) Eulers method with h = 0.5 and h = 0.25 Euler s 05 0 25 (c) Heuns method with h = 0.5 (d) 4th order RK method with h = 0.5
p p y predprey.m file
tspan = [0 20]; y0 = [2 1] 0 [2, 1]; [t, y] = ode45(@predprey, tspan, y0); figure(1); plot(t,y); figure(2); plot(y(: 1) y(: 2)); plot(y(:,1),y(:,2));
CE 206: Engg. Computation Sessional Dr. Tanvir Ahmed
(t ) = 0 cos
g t l
If l = 2 ft, g = 32.2 ft/s2 and 0 = /4, Solve for from t = 0 to 1.6 s using (a) Eulers method with h = 0 05 Euler s 0.05 (b) Using ode45 function with h = 0.05 (c) Plot your results and compare with the analytical solution
d =v dt dv g = sin dt l
d g + sin = 0 2 dt l
2
@t = 0, 0 = /4 @t = 0, v = 0
Dr. Tanvir Ahmed
Generally, the equivalent system will not have sufficient initial conditions
-A guess is made for any undefined values. - The guesses are changed until the final solution satisfies all the B.C.
For linear ODEs, only two shots are required - the proper initial condition can be obtained as a linear interpolation of the two guesses. i l i f h
CE206:Engg.Comp.Sessional Dr.Tanvir Ahmed
with =2 7x10-9 K-3 m-2, L=10 m h=0 05 m-2 T=200 K T(0) = 2.7x10 L 10 m, h 0.05 m 2, 200 K, 300 K, and T(10) = 400 K.
The basic differential equation of the elastic curve f a uniformly l d d Th b i diff i l i f h l i for if l loaded beam is given as
d 2 y wLx wx 2 EI 2 = dx 2 2
If E = 30000 ksi, I = 800 in4, w = 1 kip/ft, L = 10 ft, solve for the deflection p/ of the beam using the shooting method and compare the numerical results with the analytical solution
L L wLx 3 wx 4 wL3 x y= 12 EI 24 EI 24 EI
CE 206: Engg. Computation Sessional Dr. Tanvir Ahmed
dT dT T1 T1 = T1 = T1 2x 2x dx 0 dx 0 T1 + (2 + hx 2 ) 0 T1 = hx 2T T dT 2 2 T1 2x T + (2 + hx ) 0 T1 = hx T dx 0 dT 2 2 (2 + hx )T0 2T1 = hx T 2x dx 0
CE 206: Engg. Comp. Sessional Dr. Tanvir Ahmed