E7 Ordinary Differential Equation New - 0
E7 Ordinary Differential Equation New - 0
E7 Ordinary Differential Equation New - 0
Laboratory Module
EXPERIMENT 7
SOLVING ORDINARY DIFFERENTIAL EQUATION
1.0 OBJECTIVES
1.1. To understand fundamentals of ordinary differential equations
1.2. To apply various method for solving ordinary differential equations numerically using
MATLAB software
2.0. EQUIPMENT
Computers and Matlab program in the Mechanical Design.
x2
d 2 y dy
and
d3y
d2y
dy
a
by
c sin x - 3rd-order equation
3
2
dx
dx
dx
where x is the independent variable; y is the dependent variable. Ordinary differential
equations can be classified as linear and nonlinear equations. A differential
equation is linear if it can be written in form
a n ( x)
dny
d n 1 y
dy
a
(
x
)
... a1 ( x)
a0 ( x) y f ( x)
n 1
n
n 1
dx
dx
dx
Laboratory Module
The standard MATLAB package has built-in functions for solving ODEs. The standard
ODE solvers include two functions to implement the adaptive step size RungeKutta method. These are ode23, which uses second- and third-order formula to
attain medium accuracy, and ode45, which uses forth- and fifth-order formulas to
attain higher accuracy.
The full syntax of these functions:
Independent variable
Solver
ode23 or ode45
your_function
tspan
Laboratory Module
y0
M-file
Inline function
Example 7.1
Find the solution of the problem
dy
t
; y (0) 1 and exact solution is y (t )
dt
y
t 2 1
in interval 0 t 2 with step size is 0.2, using MATLAB functions ode23 and ode45.
Solution
To define an inline function, by typing:
>> f=inline('t./y')
The other way is via a function M-file, by typing
function z = func(t,y)
z = t./y
To calculate and plot the approximate solution y a (t ) on the interval [0, 2]. Type
>> ode45(f,[0 2],1) - if f is an inline function
>>ode45(@func,[0 2],1) if func.m is an M-file
There is still another possibility, namely to put the definition of the anonymous function
directly into ode45 command like this:
>>ode45(@(t,y) t./y,[0 2],1)
Plot a family of approximate solutions by using a vector of initial values as the third
argument to ode45 where y (0) 1,1.2,1.4,....,3, type
>>ode45(f,[0 2],1:0.2:3)
Laboratory Module
1.00000000000000 1.00000000000000
Laboratory Module
h 2 ''
y (ci ); xi ci xi 1 ,
2
(7.1)
where the third term on the right-hand side of Eq.(7.1) denotes the error or remainder
dy
'
( xi ).
term, y i y ( x i ), y i 1 y ( x i 1 ), and y i
dx
By Substituting
y ' i f ( xi , y i )
y i 1 y i hf ( xi , y i )
h 2 ''
y (ci )
2
(7.2)
(7.3)
Laboratory Module
Flowchart Euler
Start
x(1)=x0, y(1)=y0
steps size, h
No
Yes
STOP
i=1
Stabilit
y
i>N
i =i 1= +N
yi 1 y i hf ( xi , y i )
xi 1 xi h
Laboratory Module
Example 7.2
Use Eulers method to integrate y ' 4e 0.8t 0.5 y from t=0 to 4 with step size of 1. The
initial condition at t=0 is y=2. Note that the exact solution can be determined analytically as
4
(e 0.8t e 0.5t ) 2e 0.5t
1.3
Procedures-MATLAB Program
1. Start a new MatLab file by clicking on File, New and M-file that opens an empty file in
the Editor/Debugger window.
2. Write the program given below in M-file
Laboratory Module
y i f xi , y i
'
(7.2)
y i01 y i f xi , y i h
(7.3)
For the standard Euler method we would stop at this point. However, in Heuns method the
y i01 calculated in Eq.(7.3) is not the final answer, but an intermediate prediction. This is
why we have distinguished it with a superscript 0. Equation(7.3) is called a predictor
equation. It provides an estimate of y i 1 that allows the calculation of an estimated slope
at the end of the interval:
y i' 1 f xi 1 , y i01
(7.4)
Thus, the two slopes [Eqs.(7.2) and (7.4)] can be combined to obtain an average slope for
the interval:
y'
2
2
This average slope is then used to extrapolate linearly from y i to y i 1 using Eulers
method:
y i 1
f xi , y i f xi 1 , y i01
yi
h
2
Laboratory Module
as
y i 1 yi hf xi , y i
0
4.
y i 1
f xi , yi f xi 1 , y i 1 0
yi h
2
-without iteration
or
f xi , y i f xi 1 , yi k1
2
yik11 yi h
5.
y ik11 y ik1
s -with iteration
y ik11
6.
Laboratory Module
Start
x(1)=x0, y(1)=y0
Number of steps N
No
Yes
STOP
i=0
Stabilit
y
i>M
i = 1+ i
yi 1 yi hf xi , yi
k=0
k=N
k = 1+ k
f xi , y i f xi 1 , y i k1
y ik11 y i h
10
Laboratory Module
y i k11 y i k1
s
k 1
y i 1
No
Yes
xi 1 xi h
Example 7.3
Use Huens method to integrate y ' 4e 0.8t 0.5 y from t=0 to 4 with step size of 1. The
initial condition at t=0 is y=2. Note that the exact solution can be determined analytically as
4
(e 0.8t e 0.5t ) 2e 0.5t
1.3
Procedures-MATLAB Program
1. Start a new MatLab file by clicking on File, New and M-file that opens an empty file in
the Editor/Debugger window.
2. Write the program given below in M-file
11
Laboratory Module
error=zeros(M,1);
y_huen(1)=ya;
y_exact(1)=ya;
t(1)=ta;
for k=1:M
t(k+1)=t(k)+dt;
y_exact(k+1)=feval(f,t(k+1));
y_huen(k+1)=y_huen(k)+feval(df,t(k),y_huen(k))*dt;
%y_huen(k+1)=y_huen(k)+(dt/2*(feval(df,t(k),y_huen(k))
+feval(df,t(k+1),y_huen(k+1))));
for i=1:N
y_huen(k+1)=y_huen(k)+(dt/2*(feval(df,t(k),y_huen(k))
+feval(df,t(k+1),y_huen(k+1))));
error1(k+1)=abs((y_huen(k+1)-y_huen(k))/y_huen(k+1))*100;
if error1 < esp
break
end
y_huen(k+1)=y_huen(k+1)
end
end
error(k+1)=abs((y_exact(k+1)-y_huen(k+1))/y_exact(k+1))*100
(7.5)
( xi , y i , h) c1 k1 c 2 k 2 ... c n k n
12
Laboratory Module
where n denotes the order of the Runge-Kuttas method; c1 , c 2 ,...c n are constants; and
k1 , k 2 ,..., k n are recurrence relations given by
k1 f ( xi , y i ),
k 2 f ( xi p 2 h, y i a 21 k1 ),
k 3 f ( xi p3 h, y i a31 hk1 a32 hk 2 ),
.
.
.
and
k n f ( xi p n h, y i a n1 hk1 a n 2 hk 2 ... a n , n 1 hk n 1 ).
(7.6)
(7.7)
with
k1 f ( x i , y i )
k 2 f ( xi
h
h
, y i k1 )
2
2
Heuns Method
y i 1 y i
1
h ( k1 k 2 )
2
(7.8)
with
k1 f ( x i , y i )
k2 f ( xi h, yi hk1 )
Ralstons Method
y i 1 y i
1
h ( k1 2 k 2 )
3
(7.9)
13
Laboratory Module
with
k1 f ( x i , y i )
3
3
k2 f ( xi h, yi hk1 )
4
4
Method,
x(1)=x0, y(1)=y0
Steps size, h
No
STOP
Yes
i=1
Stabilit
y
i>N
i=N
k1 f ( x i , y i )
k 2 f ( xi h, y i hk1 )
FlowChart Second-Order Runge-Kutta (Huens Method)
1
y i 1 y i h(k1 k 2 )
2
14
xi 1 xi h
Laboratory Module
i = i +1
Example 7.3
Use Eulers method to integrate y ' 4e 0.8t 0.5 y from t=0 to 4 with step size of 1. The
initial condition at t=0 is y=2. Note that the exact solution can be determined analytically as
15
Laboratory Module
4
(e 0.8t e 0.5t ) 2e 0.5t
1.3
Procedures-MATLAB Program
1.
Start a new MatLab file by clicking on File, New and M-file that opens an empty
file in the Editor/Debugger window.
2. Write the program given below in M-file
16
Laboratory Module
k 2 f ( xi
1
1
h, y i k1 h)
2
2
k 3 f ( xi
1
1
h, y i k 2 h )
2
2
and
k 4 f ( xi h, y i k 3 h)
Start
17
Laboratory Module
x(1)=x0, y(1)=y0
steps size, h
No
Yes
STOP
i=1
i>M
Stabilit
y
i = i +1
k1 f ( x i , y i )
1
1
h, y i hk1 )
2
2
1
1
k 3 f ( xi h, y i hk 2 )
2
2
k 4 f ( xi h, y i hk 3 )
k 2 f ( xi
y i 1 y i
h
( k1 2 k 2 2 k 3 k 4 )
6
xi 1 xi h
Example 7.4
Use Eulers method to integrate y ' 4e 0.8t 0.5 y from t=0 to 4 with step size of 1. The
initial condition at t=0 is y=2. Note that the exact solution can be determined analytically as
18
Laboratory Module
4
(e 0.8t e 0.5t ) 2e 0.5t
1.3
Procedures-MATLAB Program
1 Start a new MatLab file by clicking on File, New and M-file that opens an empty file in
the Editor/Debugger window.
2 Write the program given below in M-file
19
y i 1 y i
Laboratory Module
h
(7 k1 32k 3 12k 4 32k 5 7 k 6 )
90
(7.10)
where
k1 f ( x i , y i )
k 2 f ( xi
1
1
h, y i hk1 )
4
4
k 3 f ( xi
1
1
1
h, y i hk1 hk 2 )
4
8
8
k 4 f ( xi
1
1
h, y i hk1 hk 3 )
2
2
k 5 f ( xi
3
3
9
h, y i hk1 hk 4 )
4
16
16
and
k 6 f ( xi h, y i
3
2
12
12
8
hk1 hk 2 hk 3 hk 4 hk 5 )
7
7
7
7
8
Start
x(1)=x0, y(1)=y0
20
Laboratory Module
step size, h
No
Yes
STOP
i=1
i>M
Stabilit
y
i = i +1
k1 f ( x i , y i )
1
1
h, y i k1 )
4
4
1
1
1
k 3 f ( x i h, y i k 1 h k 2 h )
4
8
8
1
1
k 4 f ( xi h, y i k 2 h k 3 h)
2
2
3
3
9
k 5 f x i h, y i
k1 h
k4h
4
16
16
k 2 f ( xi
3
2
12
12
8
k 6 f x i h , y i k1 h k 2 h
k3h k 4 h k5 h
7
7
7
7
7
y i 1 y i
h
(7 k1 32k 3 12k 4 32k 5 7 k 6 )
90
xi 1 xi h
Example 7.5
Use Eulers method to integrate y ' 4e 0.8t 0.5 y from t=0 to 4 with step size of 1. The
initial condition at t=0 is y=2. Note that the exact solution can be determined analytically as
4
(e 0.8t e 0.5t ) 2e 0.5t
1.3
Procedures-MATLAB Program
21
Laboratory Module
1. Start a new MatLab file by clicking on File, New and M-file that opens an empty
file in the Editor/Debugger window.
2. Write the program given below in
22
4.0
Laboratory Module
LAB ASSIGNMENT
Assignment
The free-fall velocity of a parachutist can be simulated as
c
dv
g d v2
dt
m
where v = velocity(m/s), t = time (s), g = acceleration due to gravity (9.81 m/s 2), cd = drag
coefficient (kg/m), and m = mass (kg). For a 80-kg parachutist, solve this equation using
23
a)
b)
c)
Laboratory Module
Eulers Method
Huens Method(Improvement of Eulers Method)
Higher Order Runge Kuttas Method
from t = 0 to 30 s given that v(0) = 0 with step size, h = 2. During free fall, cd = 0.25 kg/m.
Data Analysis
1. Fill in Table 1, 2 & 3.
2. Plot a graph for results comparison in Figure 1.
5.0
Lab Name
: ..
Pc Number : ..
Folder Name : ..
Mathematical Model
24
Laboratory Module
v (initial guest)
v (Euler)
m/s
v (exact)
m/s
True Percent
Relative Error
(%)
v (Huen)
m/s
v (exact)
m/s
True Percent
Relative Error
(%)
1
.
.
.
.
.
.
.
.
.
.
n
v (initial guest)
1
.
.
.
.
25
Laboratory Module
.
.
.
.
.
.
n
v (initial guest)
v (Higher Order)
m/s
v (exact)
m/s
True Percent
Relative Error
(%)
1
.
.
.
.
.
.
.
.
.
.
n
26
6.0
DISCUSSION
7.0
CONCLUSION
Laboratory Module
27
Laboratory Module
28