Sunteți pe pagina 1din 15

INTERNATIONAL ISLAMIC UNIVERSITY MALAYSIA

KULLIYYAH OF ENGINEERING
DEPARTMENT OF BIOCHEMICAL-BIOTECHNOLOGY ENGINEERING

BTE 4225
COMPUTER SIMULATION IN BIOLOGICAL SYSTEM

MATLAB REPORT
AND
ASSIGNMENT FIVE
Combination of using MATLAB Function and Function Blocks in SIMULINK
and Data Fitting

RICCARAHMAN BINTI NASARUDDIN


0611866
SECTION ONE
st
1 SEPTEMBER 2009

MATLAB FUNCTION IN SIMULINK


An embedded MATLAB Function block lets you compose a MATLAB function within a Simulink. This
block is slower than the Fcn block because it calls the MATLAB parser during each integration step.
EXAMPLE 1 MODEL 1
Create a m-file to calculate the the mass-flow rate equation:

Command of function eqn_mlfcn in M-file:


function mdot=eqn_mlfcn(x)
Pm=x(1);
N=x(2);
mdot=-0.366 + 0.08979*Pm*N - 0.0337*N* Pm.^2 + 0.0001*Pm*N.^2;

Simulink model:
Ramp 1
MATLAB
Function
eqn _mlfcn function

Ramp 2

Result in scope window:

Scope

EXAMPLE 1 MODEL 2
Simulink model:

Ramp 1

XY Graph

XY Graph

MATLAB
Function
eqn _ mlfcn function

Scope

Ramp 2

Results:
XY Graph 1:

Graph in scope window:

XY Graph 2:

FCN BLOCK IN SIMULINK


The Fcn block applies the specified mathematical expression to its input. The expression can be made up
of one or more of these components:

u The input to the block. If u is a vector, u(i) represents the ith element of the vector; u(1) or u
alone represents the first element.
Numeric constants
Arithmetic operators (+ - * /^)
Relational operators (== != > < >= <=) The expression returns 1 if the relation is true;
otherwise, it returns 0.
Logical operators (&& || !) The expression returns 1 if the relation is true; otherwise, it returns
0.
Parentheses
Mathematical functions abs, acos, asin, atan, atan2, ceil, cos, cosh, exp, fabs, floor, hypot, ln,
log, log10, pow, power, rem, sgn, sin, sinh, sqrt, tan, and tanh.
Workspace variables Variable names that are not recognized in the preceding list of items are
passed to MATLAB for evaluation. Matrix or vector elements must be specifically referenced
(e.g., A(1,1) instead of A for the first element in the matrix).

The Fcn block observes the following rules of operator precedence:


1) ( )
2) ^
3) + - (unary)
4) !
5) /
6) + 7) > < <= >=
8) == !=
9) &&
10) ||
The expression differs from a MATLAB expression in that the expression cannot perform matrix
computations. Also, this block does not support the colon operator (:).
Block input can be a scalar or vector. The output is always a scalar. For vector output, consider using the
Math Function block. If a block input is a vector and the function operates on input elements individually
(for example, the sin function), the block operates on only the first vector element.

EXAMPLE 1 Function = sin(u(1)*exp(2.3*(-u(2))))


Simulink model:

XY Graph

XY Graph 1
Ramp
f(u)
sin(u(1)*exp (2.3*(-u(2))))

Scope

Ramp 1

Results:
XY Graph:

Graph in scope window:

XY Graph 1:

ASSIGNMENT 4:
A mass balance for a chemical in a completely mixed bioreactor can be written as
Vdc/dt = F Qc kVc2
Where V = volume (12 m3), c = concentration (g/m3), F = feed rate (175 g/min), Q =flow rate (1
m3/min), and k = a second order reaction rate (0.15 m#/g/min). If c(0)=0, solve the ODE until the
concentration reaches a stable level.
The model diagram:

175
Constant

Clock
F

c(0)=0
Vc'

c'

1/12

Integrator 1

1/m

Add

1/s

XY Graph
Graph x versus t

Qc

c
1
Q

kVc2
0.15
k

Results:

12
V

c2

Concentration become stable at concentration, c = 9.5 g/m3


FITTING EQUATIONS TO EXPERIMENTAL DATA
EXAMPLE 1: ANTOINE EQUATION

Pv = 10

A+

T +C

Command in M-file:
function Antoine
clear, clc, format short g, format compact
% X = Temperature in deg C
% Y = vapour pressure in mm Hg
xyData=[-187.68 1.268E-06; -183.15 7.268E-06;
-173.15 0.0001883; -163.15 0.0025884;
-153.15 0.0221106; -143.15 0.1315085;
-133.15 0.5900482; -123.15 2.117681;
-113.15 6.352669; -103.15 16.50044;
-93.15 37.87601;
-83.15 78.7521;
-73.15 150.754;
-63.15 269.2572;
-53.15 453.7621;
-42.08 757.5202;
-33.15 1110.03;
-23.15 1635.044;
-13.15 2332.562;
-3.15
3225.086;
6.85
4365.116;
16.85
5767.654;
26.85
7485.2;
36.85
9525.254;
46.85
1.2E+04;
56.85
1.485E+04;
66.85
1.823E+04; 76.85
2.213E+04;
86.85
2.663E+04];
% Assign the data properly
X=xyData(:,1);
m=size(X,1); %Determine the number of data points
Y=xyData(:,2);
% set the descriptions
prob_title = ([' Antoine Equation Parameters, Nonlinear Regression']);
dep_var_name=['Vapor Pressure (mmHg) '];
ind_var_name=['Temperature (C)'];
parm=[6 -1000 200];

npar=size(parm,2); % Determine the number of the parameters


options=optimset('MaxFunEvals',1000); % Change the default value for MaxFunEvals
Beta=fminsearch(@AntFun,parm,options,X,Y); % Find optimal parameters using fminsearch
[f,Ycalc]=AntFun(Beta,X,Y); % Compute Y (calculated) at the optimum
disp(['
Results,' prob_title ]);
Res=[ ];
for i=1:npar
Res=[Res; i Beta(i)];
end
disp('
Parameter No.
Value
');
disp(Res);
s2=sum((Y-Ycalc)'*(Y-Ycalc))/(m-npar); %variance
disp([' Variance ', num2str(s2)]);
ymean=mean(Y);
R2=(Ycalc-ymean)'*(Ycalc-ymean)/((Y-ymean)'*(Y-ymean));
correlation coefficient
disp([' Correlation Coefficient ', num2str(R2)])
plot(Y,Y-Ycalc,'*') % Residual plot
title(['Residual Plot, ' prob_title ])
xlabel([dep_var_name '(Measured)'])
ylabel('Residual')
pause
plot(X,Ycalc,'r-',X,Y,'b+' ) %Plot of experimental and calculated data
title(['Calculated/Experimental Data ' prob_title])
xlabel([ind_var_name])
ylabel([dep_var_name])
%------------------------------------function [f,Ycalc]=AntFun(parm,X,Y)
a=parm(1);
b=parm(2);
c=parm(3);
for i=1:size(X,1);
Ycalc(i,1)=10^(a+b/(X(i)+c));
end
resid(:,1)=Y-Ycalc;
f=resid'*resid;

Result in command window:


Results, Antoine Equation Parameters, Nonlinear Regression
Parameter No.
Value
1
7.2644
2
-1046.3
3
281.61
Variance 507.9109
Correlation Coefficient 0.99922

Result as graph:

%linear

Residual Plot, Antoine Equation Parameters, Nonlinear Regression


50
40
30
20

Residual

10
0
-10
-20
-30
-40
-50

x 10

0.5

1
1.5
2
Vapor Pressure (mmHg) (Measured)

2.5

3
4

x 10

Calculated/Experimental Data Antoine Equation Parameters, Nonlinear Regression

2.5

Vapor Pressure (mmHg)

1.5

0.5

0
-200

-150

-100

-50
Temperature (C)

50

100

ASSIGNMENT 5
Fitting data to Eqn & MATLAB Functions in SIMULINK
Apply Fitting data to Eqn & MATLAB Functions in SIMULINK to any data or equation from BTE text
books.
Topic: Environmental Engineering.
Adsorption using activated carbon.
The following is the given data of adsorption of PNP (p-Nitrophenol) from an aqueous solution at 25 oC by
5 g activated carbon
PNP sorbed, q (mg/g)
125.1
152.9
194.6
222.4
264.1
291.9

PNP concentration in solution, C (mg/L)


0.834
1.529
3.197
6.255
25.02
45.87

1. Plot the graph of q versus C using langmuir isotherm


q = (1/qmax) C + (1/qmaxb)

2. If the initial concentration of PNP is 50 mg/L. Find the q for the sorbed PNP until 100% sorbed by
activated carbon.
ANSWER FOR PART 1
1. Command in M-file:
function adsorption_langmuir
clear, clc, format short g, format compact
% X = Concentration of PNP in solution (mg/L)
% Y = Sorbed of PNP (mg/g)
xyData=[0.834 125.1;
1.529 122.9;
3.197 194.6;
6.255 222.4;
25.02 264.1;
45.87 291.9]
% Assign the data properly
X=xyData(:,1);
m=size(X,1); %Determine the number of data points
Y=xyData(:,2);
% set the descriptions
prob_title = ([' LANGMUIR ISOTHERM FOR ACTIVATED CARBON ADSORPTION OF PNP']);
dep_var_name=['q, Sorbed PNP (mg/g) '];
ind_var_name=['C, Concentration of PNP (mg/L)'];
parm=[6 -1000 200];
npar=size(parm,2); % Determine the number of the parameters
options=optimset('MaxFunEvals',1000); % Change the default value for MaxFunEvals
Beta=fminsearch(@AntFun,parm,options,X,Y); % Find optimal parameters using fminsearch
[f,Ycalc]=AntFun(Beta,X,Y); % Compute Y (calculated) at the optimum
disp(['
Results,' prob_title ]);
Res=[ ];
for i=1:npar
Res=[Res; i Beta(i)];
end
disp('
Parameter No.
Value
');
disp(Res);
s2=sum((Y-Ycalc)'*(Y-Ycalc))/(m-npar); %variance
disp([' Variance ', num2str(s2)]);
ymean=mean(Y);
R2=(Ycalc-ymean)'*(Ycalc-ymean)/((Y-ymean)'*(Y-ymean));
correlation coefficient
disp([' Correlation Coefficient ', num2str(R2)])
plot(Y,Y-Ycalc,'*') % Residual plot
title(['Residual Plot, ' prob_title ])
xlabel([dep_var_name '(Measured)'])
ylabel('Residual')
pause
plot(X,Ycalc,'r-',X,Y,'b+' ) %Plot of experimental and calculated data
title(['Calculated/Experimental Data ' prob_title])
xlabel([ind_var_name])

%linear

ylabel([dep_var_name])
%------------------------------------function [f,Ycalc]=AntFun(parm,X,Y)
a=parm(1);
b=parm(2);
c=parm(3);
for i=1:size(X,1);
Ycalc(i,1)=10^(a+b/(X(i)+c));
end
resid(:,1)=Y-Ycalc;
f=resid'*resid;

2. Results:
In command window:
xyData =
0.834
125.1
1.529
122.9
3.197
194.6
6.255
222.4
25.02
264.1
45.87
291.9
Results, LANGMUIR ISOTHERM FOR ACTIVATED CARBON ADSORPTION OF PNP
Parameter No.
Value
1
2.4773
2
-1.0566
3
1.7194
Variance 243.1844
Correlation Coefficient 0.97148

Residual plot:

Residual Plot, LANGMUIR ISOTHERM FOR ACTIVATED CARBON ADSORPTION OF PNP


15

10

Residual

-5

-10

-15

-20
120

140

160

180
200
220
240
q, Sorbed PNP (mg/g) (Measured)

260

280

300

Calculated/Experimental Data LANGMUIR ISOTHERM FOR ACTIVATED CARBON ADSORPTION OF PNP


300

280

260

q, Sorbed PNP (mg/g)

240

220

200

180

160

140

120

10

15

20
25
30
C, Concentration of PNP (mg/L)

35

40

45

ANSWER FOR PART 2


1. Equation in M-file:
function q=adsorption(x)
m=5;
Ci=50;
Cf=x(1);
q=(Ci-Cf)/5;

2. Simulink model:

XY Graph

MATLAB
Function
Ramp 1
(Value of Cf )

3. Results:

adsorption function

Scope

50

XY Graph:

Scope window:

S-ar putea să vă placă și