simulink: s-functions - umass · pdf filesimulink m-file s-functions o primary purpose •...
TRANSCRIPT
Simulink: S-Functions
ChE 446
Simulink M-file S-Functions
o Primary purpose • Simulating nonlinear dynamics with MATLAB
o How they work • Example M-file S-function script (Simulink/User’s
Guide/Developing S-Functions/Overview of S-Functions) explains the basics
• Each iteration, the S-function performs calculations based on the value of a flag (initialize, find derivatives, update actual values, etc.); it returns the answer, then changes the flag for the next iteration.
• The code is reasonably well-documented as to what to enter where; we’ll help later.
• See http://www.mathworks.com/help/simulink/slref/sfunction.html for more information.
General Structure
o Switch statements o switch flag
case 0 Statements
case 1
statements
case 2
statements
otherwise
statements
end
Initialization
o Case 0: initialization
o First, Simulink sends flag=0, which:
o Declares things (x, t, y, u)
o Sets x to an initial value (x0)
o Returns ‘sys’, it’s standard return variable, set equal to: [#cont. states., #discrete states, #outputs, #inputs, direct feedthrough?, #sample times]
o See
http://www.mathworks.com/help/simulink/sfg/maintaining-level-1-matlab-s-functions.html for more information.
Derivative Calculation
o Case 1: calculate derivatives o When Simulink sends flag=1, it expects the
function to return time derivatives
o You enter these derivatives like so:
o Derivatives are returned (as ‘sys’) as a vector
o See http://www.mathworks.com/help/simulink/sfg/maintaining-level-1-matlab-s-functions.html for more information.
,2,1,2,1),( 21 xxfxxfdxdtdt
d uxf
x
Output Calculation
o Case 3: calculate outputs o When Simulink sends flag=2, it expects the
function to return outputs
o You enter these like so:
o Outputs are returned (as ‘sys’) as a vector or as a scalar, depending on the number of outputs
o See http://www.mathworks.com/help/simulink/sfg/maintaining-level-1-matlab-s-functions.html for more information.
,2,1,2,1),( 21 xxgxxgy uxgy
In-Class Exercise: Bioreactor Model
YX
Xry
YSK
S
YX
SK
S
YSSD
YSK
SDY
XSK
SDX
dt
d
Y
Ym
YX
Xm
X
i
Y
Ym
X
Xm
,,
,
,
11
x
Simulink Simulation
To Workspace1
r
To Workspace
D
S-Function
bioreactor
Dilution rate
0.65
Tasks
Write S-function for bioreactor model
Construct Simulink simulation
Simulate and plot results for:
D = 0.65
D = 0.8
Find unique D for which system is stable
Bioreactor S-function
function [sys,x0] = bioreactor(t,x,u,flag)
Kx=1.0; Ky=5.0; Yx=0.5; Yy=0.75;
muxmax=1.0; muymax=2.0; Si=10.0;
switch flag,
case 1,
D=u; X=x(1); Y=x(2); S=x(3);
mux=muxmax*S/(Kx+S);
muy=muymax*S/(Ky+S);
dxdt = [-D*X+mux*X, -D*Y+muy*Y, D*(Si-S)-mux*X/Yx-muy*Y/Yy];
sys = dxdt;
Bioreactor S-function cont.
case 3,
X=x(1); Y=x(2); r=X/(X+Y); y = r;
sys = y;
case 0,
NumContStates = 3; NumOutputs = 1; NumInputs = 1;
sys = [NumContStates,0,NumOutputs,NumInputs,0,0];
x0 = [2.5 1.5 3.0];
case { 2, 4, 9 },
sys = [];
Otherwise
% error([’Unhandled flag = ’,num2str(flag)]);
end
Results
0 50 100 150 200 2500
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1F
raction
Time (h)
D=0.65
D=0.8
D=0.75
Result cont.
0 1 2 3 4 5 6 7 8 9 100
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
S
mu
muX
muY