Simulink:
ynamic Modeling
ChE 446
Simulink blocks o
o
o o
Sources • Constant (output a constant) • Step (output a step change) • Clock ( Outputs the time ) 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) Linear • Sum (add or subtract inputs) Nonlinear • S-Function (user defined function)
Simulink M-file S-Functions o Primary purposes •
Simulating non-linear dynamics with MATLAB
•
Example M-file S-function script (Simulink/UserDefined Functions/S-function/examples/Mfiles/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.
o How they work •
•
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 o
When Simulink sends flag=1, it expects the function to return time derivatives You enter these derivatives like so:
dx = f ( x u ) ⇒ dxdt = [ f1 ( x(1) , x( 2 ) , ) dt o o
f 2 ( x(1) , x( 2 ) , ) ]
Derivatives are returned (as ‘sys’) as a vector See http://www.mathworks.com/access/helpdesk/help/too for more information.
Simulink M-file S-Functions o Case 3: calculate outputs o o
When Simulink sends flag=2, it expects the function to return outputs You enter these like so:
y = g ( x u ) ⇒ y = [ g1 ( x(1) , x( 2) , ) o o
g 2 ( x(1) , x( 2 ) , ) ]
Outputs are returned (as ‘sys’) as a vector or as a scalar, depending on the number of inputs 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 o o o o
Case 2 updates discrete states, sample times Case 4 calculates the time that the next discrete variable update occurs. Case 9 executes any statements you want to perform at the END of the simulation. Plot the output, for example. Cases 5-8 are not used; accordingly, there is an “otherwise” statement to catch these cases (which signal an error) 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
− ux + dx = − ux + dt u (10 − x ) − x1 y= x1 + x2
x1 x3 1 1+ x3 2 x2 x3 2 5 + x3 2 x1 x3 8 x2 x3 3 1+ x3 15+ 3 x3
−
x1′ = x1 − 52
x3′ = x3 − 3 y′ = y − 85
Linearized model 5 5 ′ ′ x − 0 32 3 2u dx ′ 15 3 ′ ′ = = 0 64 x3 − 2 u dt 3 − 3 11 ′ ′ ′ ′ − x − x − x − 7 u 4 1 2 8 3 4 3 5 3 5 y′ = 16 x1′ − 32 x′2 = ( 16 − 32 0) ⋅ x ′
0 A = 0 − 34
− 1 − 0 0
5 32 15 64 11 8
− 52 B = − 32 − 7
x′2 = x2 − 32
− 52 ′ 3 ′ 0 ⋅ x + − 2 u − 7 − 1 − 0
C = [ 163
5 32 15 64 11 8
−5 32
0]
D = [ 0]
If you build it, … D To Workspace
Scope 1
bioreact
1 r
S -Function 0.76 Input
0.75 D_ss
x' = Ax + Bu y = Cx + Du
Scope
State -Space 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/Yxmuy*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