Vous êtes sur la page 1sur 12

INTERNATIONAL ISLAMIC UNIVERSITY MALAYSIA

KULLIYYAH OF ENGINEERING
DEPARTMENT OF BIOCHEMICAL-BIOTECHNOLOGY ENGINEERING

BTE 4225
COMPUTER SIMULATION IN BIOLOGICAL SYSTEM

MATLAB REPORT
AND
ASSIGNMENT TWO

RICCARAHMAN BINTI NASARUDDIN


0611866
SECTION ONE
3RD AUGUST 2009

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.

This equation is linear, so it is easy to solve symbolically.


dsolve('Dy = 2*y-1, y(0)=1','t')
%solve the differential equation with the condition of y=1 when t=0
ans =
exp(2*t)/2 + 1/2

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);

Based on above command, the results are as follows:


dy =
In
command
window,
1
MATLAB
will
define
all
0
possible
dy
value
based
on
0
given ODE.
dy =
1
0
0
dy =
1
0
0
..(cont.)

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

??? Input argument "y" is undefined.

Error in ==> vdp1000 at 6


dy(1) = y(2);

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);

??? Maximum recursion limit of 500


reached. Use set(0,'RecursionLimit',N)
to change the limit. Be aware that
exceeding your available stack space
can
crash MATLAB and/or your computer.
Error in ==> ode15s

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),

In the above command, I want to view the value of dy.


>> [t,y] = ode15s('vdp1000',[0 500],[2 0]);
dy =
0
0
dy =
0
-2
dy =
0
0
dy =
0
-2.0000
..(cont.)

dy =
1.0e-003 *
0.7118
0.0044
>> plot(t,y(:,1),'-o');
>>

-1

-2

-3

500

1000

1500

2000

The van der Pol equation


2
y1 (1 y1 ) y1 + y1 = 0

Is equivalent to a system of coupled first-order differential equations

y2 = (1 y1 ) y2 y1
2

%The van der Pol equation


function out1 = vdp1(t,y)
out1 = [y(2); (1-y(1)^2)*y(2) - y(1)];

Call the function from command window

2500

3000

>> [t,y] = ode45('vdp1',[0 20],[2; 0]); plot(t,y(:,1),'-',t,y(:,2),'-.');


>>
3

-1

-2

-3

10

12

14

16

18

20

>> [t,y] = ode45('vdp1',[7 50],[0; 3]); plot(t,y(:,1),'-',t,y(:,2),'-.');


>>
4

-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.

function [out1,out2,out3] = vdp1_flag(t,y,flag)


if nargin < 3 | isempty(flag)
out1 = [y(1).*(1-y(2).^2)-y(2); y(1)];
else
switch(flag)
case 'init' % Return tspan, y0, and options
out1 = [0 20];
out2 = [2; 0];
out3 = [ ];
otherwise error(['Unknown request ''' flag '''.']);
end
end

Call the function from command window


>> [t,y] = ode45('vdp1_flag',[7 50],[0; 3]); plot(t,y(:,1),'-',t,y(:,2),'-.');
>>
3

-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)

Run the function and we get T = 350oC

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

Vous aimerez peut-être aussi