Upload
poopoopho
View
118
Download
3
Embed Size (px)
Citation preview
MATLAB and Simulink:
Dynamic Modeling
ChE 446
Simulink blockso Sources
• Constant (output a constant)• Step (output a step change)• Clock ( Outputs the time )
o Sinks• Scope (simulation time plot of input)• Display (real-time display of input value during
a simulation)• To Workspace (saves the input to the matlab
workspace)o Linear
• Sum (add or subtract inputs)o Nonlinear
• S-Function (user defined function)
Simulink M-file S-Functions
o Primary purposes• Simulating non-linear dynamics with MATLAB
o How they work• Example M-file S-function script (Simulink/User-
Defined Functions/S-function/examples/M-files/Level-1 M-files) 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.
Simulink M-file S-Functions
o Switch statementso switch flag
case 0Statements
case 1statements
case 2statements
otherwisestatements
end
Simulink M-file S-Functions
o Case 0: initializationo 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/access/helpdesk/help/toolbox/simulink/sfg/f7-60056.html for more information.
Simulink M-file S-Functions
o Case 1: calculate derivativeso When Simulink sends flag=1, it expects the
function to return time derivativeso You enter these derivatives like so:
o Derivatives are returned (as ‘sys’) as a vectoro See
http://www.mathworks.com/access/helpdesk/help/toolbox/simulink/sfg/f7-60056.html for more information.
,2,1,2,1 21 xxfxxfdxdtuxfdt
xd
Simulink M-file S-Functions
o Case 3: calculate outputso When Simulink sends flag=2, it expects the
function to return outputso You enter these like so:
o Outputs are returned (as ‘sys’) as a vector or as a scalar, depending on the number of inputs
o See http://www.mathworks.com/access/helpdesk/help/toolbox/simulink/sfg/f7-60056.html for more information.
,2,1,2,1 21 xxgxxgyuxgy
Simulink M-file S-Functions
o Cases 2,4,9: not usedo Case 2 updates discrete states, sample timeso Case 4 calculates the time that the next discrete
variable update occurs.o Case 9 executes any statements you want to
perform at the END of the simulation. Plot the output, for example.
o Cases 5-8 are not used; accordingly, there is an “otherwise” statement to catch these cases (which signal an error)
o See http://www.mathworks.com/access/helpdesk/help/toolbox/simulink/sfg/f7-60056.html for more information.
Dynamic Simulation: Linear vs. Nonlinear
o Solution to HW 2 will be posted on website, including S-functions for open loop and closed loop simulation.
o In-class exercise: compare the nonlinear model from the homework to a linearized model
Nonlinear model
21
1
3158
12
3
52
2
11
3
32
3
31
3
32
3
31
10
xx
xy
xu
ux
ux
dt
xd
xxx
xxxxxxxxx
Linearized model
xxxy
ux
uxxx
ux
ux
dt
xd
0
71
00
00
7
325
163
2325
1163
23
25
811
43
6415
325
3811
2143
23
36415
25
3325
811
43
6415
325
1
00
00
A
723
25
B 0325
163 C 0D
85
33
23
2225
11
3
yyxx
xxxx
r
1
r_ss
0.625
To Workspace
D
State -Space
x' = Ax + Bu y = Cx + Du
Scope 1
Scope
S-Function
bioreact
Input
0.76
D_ss
0.75
If you build it, …
Bioreactor S-functionfunction [sys,x0] = bioreact(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
Tasks
o Compare response of linear model to non-linear model for:o Constant inputo Step inputo Sine input
o Compare time-varying functions (step, sine, etc.) for both large and small deviations from steady-state