160 likes | 577 Views
Simulink: S-Functions. ChE 446. Simulink M-file S-Functions. Primary purpose Simulating nonlinear dynamics with MATLAB How they work Example M-file S-function script (Simulink/User’s Guide/Developing S-Functions/Overview of S-Functions) explains the basics
E N D
Simulink: S-Functions ChE 446
Simulink M-file S-Functions • Primary purpose • Simulating nonlinear dynamics with MATLAB • 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 • Switch statements • switch flag case 0 Statements case 1 statements case 2 statements otherwise statements end
Initialization • Case 0: initialization • First, Simulink sends flag=0, which: • Declares things (x, t, y, u) • Sets x to an initial value (x0) • Returns ‘sys’, it’s standard return variable, set equal to:[#cont. states., #discrete states, #outputs, #inputs, direct feedthrough?, #sample times] • See http://www.mathworks.com/help/simulink/sfg/maintaining-level-1-matlab-s-functions.html for more information.
Derivative Calculation • Case 1: calculate derivatives • When Simulink sends flag=1, it expects the function to return time derivatives • You enter these derivatives like so: • Derivatives are returned (as ‘sys’) as a vector • See http://www.mathworks.com/help/simulink/sfg/maintaining-level-1-matlab-s-functions.html for more information.
Output Calculation • Case 3: calculate outputs • When Simulink sends flag=2, it expects the function to return outputs • You enter these like so: • Outputs are returned (as ‘sys’) as a vector or as a scalar, depending on the number of outputs • See http://www.mathworks.com/help/simulink/sfg/maintaining-level-1-matlab-s-functions.html for more information.
Remaining Cases • Cases 2,4,9: not used in this class • 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/help/simulink/sfg/maintaining-level-1-matlab-s-functions.html for more information.
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