1 / 14

Simulink: S-Functions

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

kale
Download Presentation

Simulink: S-Functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Simulink: S-Functions ChE 446

  2. 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.

  3. General Structure • Switch statements • switch flag case 0 Statements case 1 statements case 2 statements otherwise statements end

  4. 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.

  5. 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.

  6. 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.

  7. 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.

  8. In-Class Exercise: Nonlinear Bioreactor Model

  9. Simulink Simulation

  10. 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

  11. 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;

  12. 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

  13. Results

  14. Result cont.

More Related