0% found this document useful (0 votes)
197 views23 pages

CE 206: Engineering Computation Sessional Ordinary Differential Equations (ODE)

This document discusses numerical methods for solving ordinary differential equations (ODEs) including Euler's method, Heun's method, the 4th order Runge-Kutta method, and MATLAB's built-in ODE solvers. It provides the mathematical formulations and MATLAB code implementations for these numerical ODE solving techniques. It also presents examples of applying these methods to solve physics problems involving a swinging pendulum, a bungee jumper, and predator-prey interactions.

Uploaded by

sazid alam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
197 views23 pages

CE 206: Engineering Computation Sessional Ordinary Differential Equations (ODE)

This document discusses numerical methods for solving ordinary differential equations (ODEs) including Euler's method, Heun's method, the 4th order Runge-Kutta method, and MATLAB's built-in ODE solvers. It provides the mathematical formulations and MATLAB code implementations for these numerical ODE solving techniques. It also presents examples of applying these methods to solve physics problems involving a swinging pendulum, a bungee jumper, and predator-prey interactions.

Uploaded by

sazid alam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

CE 206: Engineering

Computation Sessional

Ordinary Differential Equations


(ODE)
Why study Differential Equations?
Many physical phenomena are best formulated
mathematically in terms of their rate of change.

Motion of a swinging pendulum


d 2 g
2
+ sin  = 0
dt l
Bungee-jumper
Equation
dv cd 2
= g− v
dt m


CE 206: Engg. Computation Sessional Dr. Tanvir Ahmed
Euler’s 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 (t i , yi )
yi+1 = yi + f (t i , yi )h

CE 206: Engg. Computation Sessional Dr. Tanvir Ahmed


MATLAB Code: Euler’s Method
function [t,y] = eulode(dydt,tspan,y0,h)
% input: dydt = name of the M-file that evaluates the ODE
% tspan = [ti, tf], ti and tf limits of independent variable
% y0 = initial value of dependent variable h = step size
% output:y = solution vector

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
t(n+1) = tf; n = n+1; (
yi +1 = yi + f ti , yi h)
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

CE 206: Engg. Computation Sessional Dr. Tanvir Ahmed


Exercise: Euler’s method
Solve the bungee jumper problem using Euler’s method (use a
step size of 2 s). Compare the results with the analytical solution
dv cd 2 Given, g = 9.81 m/s2, cd = 0.25 kg/m, m = 68.1 kg
=g− v
dt m
60

50
Analytical solution:
gm  gcd 
v(t ) = tanh  t 
40 cd  m 
v (m/s)

30

20

10

0
0 2 4 6 8 10 12
t (sec)
CE 206: Engg. Computation Sessional Dr. Tanvir Ahmed
Heun’s Method

Predictor: yi0+1 = yi + f (ti , yi )h


f (ti , yi ) + f (ti +1 , yi0+1 )
Corrector: yi +1 = yi + h
2

Exercise: Modify eulode.m to implement Heun’s method.


Solve the same bungee-jumper problem.
CE 206: Engg. Computation Sessional Dr. Tanvir Ahmed
4th order Runge-Kutta method
1
yi+1 = yi + (k1 + 2k2 + 2k3 + k4 )h
6

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)

CE 206: Engg. Computation Sessional Dr. Tanvir Ahmed


MATLAB Code: Runge-Kutta Method
function [tp,yp] = rk4(dydt,tspan,y0,h)
% input: dydt = name of the M-file that evaluates the ODEs
% tspan = [ti, tf]; initial and final times
% y0 = initial values of dependent variables
% h = step size
% output: tp = vector of independent variable
% yp = vector of solution for dependent variables

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 tp(n)<tf
tp(n+1) = tf; n = n+1;
end

CE 206: Engg. Computation Sessional Dr. Tanvir Ahmed


MATLAB Code: Runge-Kutta Method

yp = y0*ones(n,1);%preallocate yp to improve efficiency

for i = 1:n-1 %implement RK method

hh = tp(i+1)-tp(i); tt = tp(i); yy = yp(i);

k1 = dydt(tt,yy);
k2 = dydt(tt + hh/2, yy + 0.5*k1*hh);
k3 = dydt(tt + hh/2, yy + 0.5*k2*hh);
k4 = dydt(tt + hh, yy + k3*hh);

yp(i+1) = yy + hh*(k1 + 2*k2 + 2*k3 + k4)/6;

end

CE 206: Engg. Computation Sessional Dr. Tanvir Ahmed


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) Euler’s method with h = 0.5 and h = 0.25
(c) Heun’s method with h = 0.5
(d) 4th order RK method with h = 0.5

CE 206: Engg. Computation Sessional Dr. Tanvir Ahmed


MATLAB’s common ODE solvers

MATLAB’s ode23 function uses second- and third-order


RK functions to solve the ODE and adjust step sizes.

MATLAB’s ode45 function uses fourth- and fifth-order RK


functions to solve the ODE and adjust step sizes. This is
recommended as the first function to use to solve a
problem.

MATLAB’s ode113 function is a multistep solver useful


for computationally intensive ODE functions.

CE 206: Engg. Computation Sessional Dr. Tanvir Ahmed


Example of ODE solver: ode45
The functions are generally called in the same way

[t, y] = ode45(odefun, tspan, y0)

y: solution array, where each column represents one of the


variables and each row corresponds to a time in the t vector
odefun: function returning a column vector of the right-
hand-sides of the ODEs
tspan: time over which to solve the system
If tspan has two entries, the results are reported for those times as
well as several intermediate times based on the steps taken by the
algorithm
If tspan has more than two entries, the results are reported only for
those specific times
y0: vector of initial values

CE 206: Engg. Computation Sessional Dr. Tanvir Ahmed


Solving a system of ODE using ode45
Predator-prey problem
dy1 dy2
Solve = 1.2y1 − 0.6y1 y2 = 0.8y2 + 0.3y1 y2
dt dt
with y1(0)=2 and y2(0)=1 for 20 seconds


function yp = predprey(t, y)
yp = [1.2*y(1)-0.6*y(1)*y(2);… predprey.m file
-0.8*y(2)+0.3*y(1)*y(2)];

tspan = [0 20];
y0 = [2, 1];
[t, y] = ode45(@predprey, tspan, y0);
figure(1); plot(t,y);
figure(2); plot(y(:,1),y(:,2));

CE 206: Engg. Computation Sessional Dr. Tanvir Ahmed


Example using ode45
Predator-prey problem
dy1 dy2
Solve = 1.2y1 − 0.6y1 y2 = 0.8y2 + 0.3y1 y2
dt dt
with y1(0)=2 and y2(0)=1 for 20 seconds

CE 206: Engg. Computation Sessional Dr. Tanvir Ahmed


Practice Problem: motion of a pendulum
d 2 g
2
+ sin  = 0
dt l
Analytical solution (assuming θ = sinθ):
 g 
 (t ) =  0 cos 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) Euler’s method with h = 0.05
(b) Using ode45 function with h = 0.05
(c) Plot your results and compare with the analytical solution

d @t = 0, θ0 = π/4
=v
d g
2
dt
2
+ sin  = 0 dv g
dt l = − sin  @t = 0, v = 0
dt l
CE 206: Engg. Computation Sessional Dr. Tanvir Ahmed
Boundary value problem: the shooting method
the boundary-value problem is converted into an
equivalent initial-value problem.
 dT
d 2T  dx = z
+ h(T − T ) = 0  
= − h(T − T )
dx 2 dz

 dx
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.
CE 206: Engg. Comp. Sessional Dr. Tanvir Ahmed
Problem: shooting method

Solve d 2T
dx 2
+ h (T − T ) +  (T 
4
− T 4
)=0

with σ’=2.7x10-9 K-3 m-2, L=10 m, h’=0.05 m-2, T=200 K, T(0) =


300 K, and T(10) = 400 K.

First - break into two equations:
 dT
 dx = z
( )
2
d T
+ h (T − T ) +   T 4
− T 4
=0
( )

= −0.05(200 − T ) − 2.7  10 −9 1.6  109 − T
dx 2  dz

 dx

CE 206: Engg. Computation Sessional Dr. Tanvir Ahmed


Problem: shooting method
Code for derivatives:
function dy=dydxn(x,y)
dy=[y(2);…
-0.05*(200-y(1))-2.7e-9*(1.6e9-y(1)^4)];

Code for residual:


function r=res(za)
[x,y]=ode45(@dydxn, [0 10], [300 za]);
r=y(length(x),1)-400;

Code for finding root of residual:


fzero(@res, -50)

Code for solving system:


[x,y]=ode45(@dydxn, [0 10], [300 fzero(@res, -50) ]);
CE 206: Engg. Computation Sessional Dr. Tanvir Ahmed
Practice Problem: shooting method

The basic differential equation of the elastic curve for a uniformly 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
of the beam using the shooting method and compare the numerical
results with the analytical solution

wLx 3 wx 4 wL3 x
y= − −
12 EI 24 EI 24 EI
CE 206: Engg. Computation Sessional Dr. Tanvir Ahmed
Numerical solution: finite difference
finite differences are substituted for the derivatives
in the original equation
d 2T
2
+ h(T − T ) = 0
dx

d 2T Ti−1 − 2Ti + Ti+1


=
dx 2
x 2
Ti−1 − 2Ti + Ti+1
+ h(T − Ti ) = 0
x 2

−Ti−1 + (2 + hx 2 )Ti − Ti+1 = hx 2T


CE 206: Engg. Comp. Sessional Dr. Tanvir Ahmed
Numerical solution: finite difference
The linear differential equation is transformed into a
set of simultaneous algebraic equations.

Since T0 and Tn are known (Drichlet boundary


conditions), they will be on the right-hand-side of the
linear algebra system

2 + hx 2 −1  T  hx 2T + T0 


  1    2 
−1 2 + hx 2 −1 T2 h x T 
   =  
    
 −1 2 + hx 2 Tn−1  hx 2T + Tn 

CE 206: Engg. Comp. Sessional Dr. Tanvir Ahmed


Numerical solution: finite difference
If there is a derivative boundary condition
(Neumann B.C.), the centered difference equation is
solved at the point and rewriting the system
equation accordingly.
For a Neumann condition at T0 point:

dT T1 − T−1  dT 
=  T−1 = T1 − 2x 
dx 0 2x  dx 0

−T−1 + (2 + hx 2 )T0 − T1 = hx 2T


  dT 
−T1 − 2x  + (2 + hx )T0 − T1 = hx T
2 2

  dx 0 
 dT 
(2 + hx )T0 − 2T1 = hx T − 2x dx 
2 2

0
CE 206: Engg. Comp. Sessional Dr. Tanvir Ahmed
Practice Problem: Finite difference
Solve the nondimensionalized ODE using finite difference
methods that describe the temperature distribution in a
circular rod with internal heat source S
d 2T 1 dT
2
+ +S =0
dr r dr
Over the range 0 ≤ r ≤ 1, with boundary conditions
dT
T (r = 1) = 1 =0
dr r =0

For S = 1, 10 and 20 K/m2 plot the temperature versus radius

CE 206: Engg. Computation Sessional Dr. Tanvir Ahmed

You might also like