Você está na página 1de 15

Simulink:

ynamic Modeling

ChE 446
Simulink blocks
o 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 statements
o switch flag
case 0
Statements
case 1
statements
case 2
statements
otherwise
statements
end
Simulink M-file S-Functions
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/access/helpdesk/help/too
for more information.
Simulink M-file S-Functions
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:
 
dx 
= f ( x u ) ⇒ dxdt = [ f1 ( x(1) , x( 2 ) , ) f 2 ( x(1) , x( 2 ) , ) ]
dt

o Derivatives are returned (as ‘sys’) as a vector


o See
http://www.mathworks.com/access/helpdesk/help/too
for more information.
Simulink M-file S-Functions
o Case 3: calculate outputs
o When Simulink sends flag=2, it expects the
function to return outputs
o You enter these like so:
  
y = g ( x u ) ⇒ y = [ g1 ( x(1) , x( 2) , ) g 2 ( x(1) , x( 2 ) , ) ]

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/too
for more information.
Simulink M-file S-Functions
o Cases 2,4,9: not used
o Case 2 updates discrete states, sample times
o 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/too
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

  
x1 x3
− ux +
dx
1 1+ x3 
= − ux +
2
2 x2 x3
5 + x3

dt  
 u (10 − x ) −2 x1 x3
− 8 x2 x3
 3 1+ x3 15+ 3 x3 
x1
y=
x1 + x2
x1′ = x1 − 52 x′2 = x2 − 32
x3′ = x3 − 3 y′ = y − 85
Linearized model

   5
x ′ − 5
2u
′  0 0  5
 − 52 
dx ′
32 3
   ′  3  ′
32

= ′
64 x3 − 2 u
′ = 0 0  ⋅ x +  − 2 u
15 3 15
64
dt  3  − 3 − 7
− x ′ −
 4 1 2 8 3 x ′ − 11 ′
x − 7 u ′   4 − 1 −  11
 
8

y′ = 16 x1′ − 32 x′2 = ( 16 − 32 0) ⋅ x ′
3 5 3 5

0 0 5
32 − 52 
A =  0 0 15
64

 B = − 32  C = [ 163 −5
32 0] D = [ 0]
− 34 − 1 − 
11
8
− 7 
If you build it, …
D

To Workspace

Scope 1
bioreact
1
S -Function r

0.76

Input x' = Ax + Bu
y = Cx + Du
Scope
State -Space
0.75

D_ss 0.625

r_ss
Bioreactor S-function
function [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 input
o Step input
o Sine input
o Compare time-varying functions
(step, sine, etc.) for both large and
small deviations from steady-state

Você também pode gostar