Beruflich Dokumente
Kultur Dokumente
KULLIYYAH OF ENGINEERING
DEPARTMENT OF BIOCHEMICAL-BIOTECHNOLOGY ENGINEERING
BTE 4225
COMPUTER SIMULATION IN BIOLOGICAL SYSTEM
MATLAB REPORT
AND
ASSIGNMENT TWO
MATLAB ODE.
MATLABs built-in numerical solvers to find approximate solutions to almost any system of differential equation which
are ode45, ode23, ode113, ode15s, ode23s and so on.
x' = F(t, x)
M(t, x) x' = F(t, x)
where
t is a scalar independent variable, typically representing time.
y is a vector of dependent variables.
F is a function of t and x returning a column vector the same length as x.
M(t, x) is a time-and-state-dependent mass matrix
[t, x] = ode45(filename, [t0,tf], x0)
Where
filename is a string variable containing the name of the m-file for the derivatives.
t0
is the initial time
tf
is the final time
x0
is the initial condition vector for the state variables
t
a (column) vector of time
x
an array of state variables as a function of time
Create function file;
function output=function_name (input1, input2)
The function file generates the value of outputs every time it called upon with certain sets of inputs of
dependent and independent variables.
For instance the cstr.m file generates the value of output (dx), every time it is called upon with inputs of
independent variable time (t) and dependent variable (x)
For example;
The ODE is
We use ode45 to obtain the numeric solution. We have to define a MATLAB function equal to the right side of
the equation, which we can do with an anonymous function.
syms t
f = @(t,y) 2.*y -1
f =
@(t,y)2.*y-1
To solve and plot the approximate solution ya on the interval [0,1], we give the command
ode45(f, [0,1], 1)
Other example:
>> syms t
>> f = @(t,y) 5.*y -4
f=
@(t,y)5.*y-4
>> ode45(f, [0,5], 3)
>>
10
16
x 10
14
12
10
8
6
4
2
0
0.5
1.5
2.5
3.5
4.5
The second argument is the interval and the last one is the value of the solution at the left end of the interval.
We can make a table of values of the approximate and exact solutions. We don't want an enormous table, so
we only calculate at t=0,0.1,0.2,...,1. The first column in the table is t, the second the value of the approximate
solution, and the third the value of the exact solution.
[t,ya] = ode45(f,0:0.1:1,1);
y = 1./2+1./2.*exp(2.*t);
format long
[t,ya,y]
ans =
0
1.000000000000000
1.000000000000000
0.100000000000000
1.110701386666667
1.110701379080085
0.200000000000000
1.245912367353179
1.245912348820635
0.300000000000000
1.411059434148805
1.411059400195255
0.400000000000000
1.612770519540847
1.612770464246234
0.500000000000000
1.859140998650765
1.859140914229523
0.600000000000000
2.160058585103081
2.160058461368274
0.700000000000000
2.527600159740712
2.527599983422337
0.800000000000000
2.976516458318416
2.976516212197557
0.900000000000000
3.524824070395758
3.524823732206473
1.000000000000000
4.194528508426800
4.194528049465325
Generally, all of the solvers of the ODE Suite can solve M(t, x) x' = F(t, x). However, ode23s are exceptions
that they only able to solve problems with constant mass matrices. The ode15s and ode23t solvers can solve
some differential-algebraic equations (DAEs) of the form.
M(t) x' = F(t, x).
In order to set up the computatuin, we need to specify an entire initial value problem (IVP) within the ODE Mfile by eliminating the need to supply time and initial value vectors at the command line.
In my previous example we use ode45 which is based on an explicit Runge-Kutta formula, the DormandPrince pair. It is a one-step solver - in computing y(tn), it needs only the solution at the immediately preceding
time point, y(tn-1). In general, ode45 is the best function to apply for most problems.
Example 1:
An example of a nonstiff system is the system of equations describing the motion of a rigid body without
external forces:
y1 = y2 y3
y1 (0) = 0
y2 = y1 y3
y2 (0) = 1
y3 = 0.51y1 y2
y3 (0) = 0
To simulate this system, a function M-file rigid containing the equations has to be created.
%TUTORIAL OF ODE
%Example 1
function rigid_run
options = odeset('RelTol', 1e-4, 'AbsTol', [1e-4 1e-4 1e-5]);
[t,y]=ode45('rigid',[0 12],[0 1 1], options);
plot(t,y(:,1),'-',t,y(:,2),'-.',t,y(:,3),'.');
function dy=rigid(t,y)
dy=zeros(3,1);
%a column vector
dy(1)=y(2)*y(3),
dy(2)=-y(1)*y(3),
dy(3)=-0.51*y(1)*y(2);
1.5
0.5
-0.5
-1
-1.5
10
12
In this example the error tolerances with the odeset command was changed and the problem was solved on a
time interval of [0 12] with initial condition vector [0 1 1] at time 0.
The function is also used to solve a problem on a time interval of [0 5] with initial condition vector [1 3 2] at
time 0. During this time, the dy value is suppressed by placing semicolon. So, only the graph will appear and
the function is executed.
%TUTORIAL OF ODE
%Example 1
function rigid_run
options = odeset('RelTol', 1e-4, 'AbsTol', [1e-4 1e-4 1e-5]);
[t,y]=ode45('rigid',[0 5],[1 3 2], options);
plot(t,y(:,1),'-',t,y(:,2),'-.',t,y(:,3),'.')
function dy=rigid(t,y)
dy=zeros(3,1);
%a column vector
dy(1)=y(2)*y(3),
dy(2)=-y(1)*y(3),
dy(3)=-0.51*y(1)*y(2);
-1
-2
-3
0
0.5
1.5
2.5
3.5
4.5
Example 2:
An example of a stiff system is provided by the van der Pol equations governing relaxation oscillation. The
limit cycle has portions where the solution components change slowly and the problem is quite stiff,
alternating
with
regions
of
very
sharp
change
where
it
is
not
stiff.
y1 = y2
y2 = 1000(1 y1 ) y2 y1
2
y 2 (0 ) = 1
%Example 2:
%First Trial
function dy = vdp1000(t,y);
dy = zeros(2,1); % a column vector
dy(1) = y(2);
dy(2) = 1000*(1 - y(1)^2)*y(2) - y(1);
[t,y] = ode15s('vdp1000',[0 3000],[2 0]);
plot(t,y(:,1),'-o');
y1 (0) = 1
Usually, I will face this kind of problem which some variables/parameters are undefined. So that in command
window, I cannot execute the function.
Therefore, I made some changes to the MATLAB command in editor window of function: vdp1000.
%Example 2:
function vdp1000_run
[t,y] = ode15s('vdp1000',[0 500],[2 0]);
plot(t,y(:,1),'-o');
function dy = vdp1000(t,y)
dy = zeros(2,1); % a column vector
dy(1) = y(2);
dy(2) = 1000*(1 - y(1)^2)*y(2) - y(1);
However, I could not solve the problem. I have some difficulties in understanding the problem regarding
recursion limit.
So, then I try to call the function form command window
%Example 2:
function dy = vdp1000(t,y);
dy = zeros(2,1); % a column vector
dy(1) = y(2),
dy(2) = 1000*(1 - y(1)^2)*y(2) - y(1),
dy =
1.0e-003 *
0.7118
0.0044
>> plot(t,y(:,1),'-o');
>>
-1
-2
-3
500
1000
1500
2000
y2 = (1 y1 ) y2 y1
2
2500
3000
-1
-2
-3
10
12
14
16
18
20
-1
-2
-3
5
10
15
20
25
30
35
40
45
50
To specify the entire initial value problem (IVP) within the M-file, we make the vdp1 function as follows:
I make a new M-file and rename the function as vdp1_flag.
-1
-2
-3
10
15
20
25
30
35
40
45
50
ASSIGNMENT 2
Solving ODE problem in Biotechnology Engineering field.
Process design and operating conditions are characterized by the following four parameter groups:
1. a = 0.05 oC min/kcal
The nominal values of Q and Ti are:
1. Q = 5000 kcal/min
2. Ti = 100oC
Questions
a) Calculate the nominal steady state temperature, T
b) Assume that the process is initially at the steady state determined in part(a). Calculate the response,
T(t) to a sudden change in Q from 5000 to 5400 kcal/min using equation:
3T T = 100
Plot the temperature response.
%ASSIGNMENT 2
%SOLVING ODE PROBLEM IN BIOTECHNOLOGY FIELD
function sstate_run
%define variables
a= 0.05;
Q=5000;
Ti=100;
T=Ti+(a*Q)
a)
T=
350
>>
b) After substituting the parameters, we obtained:
3T T= 100
T = (100 + T)/3
>> syms t
>> f = @(t,T) (100+T)/3
f=
@(t,T)(100+T)/3
>> ode45(f, [0, 10], 3)
>>
3000
2500
2000
1500
1000
500
10