simulink: s-functions - umass · pdf filesimulink m-file s-functions o primary purpose •...

13
Simulink: S-Functions ChE 446

Upload: vuthuan

Post on 06-Feb-2018

215 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Simulink: S-Functions - UMass · PDF fileSimulink M-file S-Functions o Primary purpose • Simulating nonlinear dynamics with MATLAB o How they work • Example M-file S-function script

Simulink: S-Functions

ChE 446

Page 2: Simulink: S-Functions - UMass · PDF fileSimulink M-file S-Functions o Primary purpose • Simulating nonlinear dynamics with MATLAB o How they work • Example M-file S-function script

Simulink M-file S-Functions

o Primary purpose • Simulating nonlinear dynamics with MATLAB

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

Page 3: Simulink: S-Functions - UMass · PDF fileSimulink M-file S-Functions o Primary purpose • Simulating nonlinear dynamics with MATLAB o How they work • Example M-file S-function script

General Structure

o Switch statements o switch flag

case 0 Statements

case 1

statements

case 2

statements

otherwise

statements

end

Page 7: Simulink: S-Functions - UMass · PDF fileSimulink M-file S-Functions o Primary purpose • Simulating nonlinear dynamics with MATLAB o How they work • Example M-file S-function script

In-Class Exercise: Bioreactor Model

YX

Xry

YSK

S

YX

SK

S

YSSD

YSK

SDY

XSK

SDX

dt

d

Y

Ym

YX

Xm

X

i

Y

Ym

X

Xm

,,

,

,

11

x

Page 8: Simulink: S-Functions - UMass · PDF fileSimulink M-file S-Functions o Primary purpose • Simulating nonlinear dynamics with MATLAB o How they work • Example M-file S-function script

Simulink Simulation

To Workspace1

r

To Workspace

D

S-Function

bioreactor

Dilution rate

0.65

Page 9: Simulink: S-Functions - UMass · PDF fileSimulink M-file S-Functions o Primary purpose • Simulating nonlinear dynamics with MATLAB o How they work • Example M-file S-function script

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

Page 10: Simulink: S-Functions - UMass · PDF fileSimulink M-file S-Functions o Primary purpose • Simulating nonlinear dynamics with MATLAB o How they work • Example M-file S-function script

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;

Page 11: Simulink: S-Functions - UMass · PDF fileSimulink M-file S-Functions o Primary purpose • Simulating nonlinear dynamics with MATLAB o How they work • Example M-file S-function script

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

Page 12: Simulink: S-Functions - UMass · PDF fileSimulink M-file S-Functions o Primary purpose • Simulating nonlinear dynamics with MATLAB o How they work • Example M-file S-function script

Results

0 50 100 150 200 2500

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1F

raction

Time (h)

D=0.65

D=0.8

D=0.75

Page 13: Simulink: S-Functions - UMass · PDF fileSimulink M-file S-Functions o Primary purpose • Simulating nonlinear dynamics with MATLAB o How they work • Example M-file S-function script

Result cont.

0 1 2 3 4 5 6 7 8 9 100

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

S

mu

muX

muY