Beruflich Dokumente
Kultur Dokumente
9. S - FUNCTION
AIM: To write a program to understand the concept of S – Function in MATLAB
THEORY:
For blocks with no states, x is an empty vector. In MEX-file S-functions, there are two separate
state vectors for the continuous and discrete states.
STEPS:
STEP 1
Write the S-function in the MATLAB edit window using M file.
STEP 2
Save the function exactly as the function name in MATLAB/work directory
STEP 3
Open Simulink and open a new model window
STEP 4
Click on the user defined function of Simulink library browser
STEP 5
Drag the function block to the new model window and click on the block to change its name
exactly a sthe function name declared in M-file which was saved in matlab/work directory
STEP 6
Then this block will work as a user defined block which will behave as other blocks of the
simulink library.
45
EXERCISE:
(1) Create a diode model using Simulink s-function which will pass the wave whose input is greater
than 0.6 and test it with a SINWAVE
INPUT:
SIMULINK MODEL
DIODE M FILE
function [sys,x0,str,ts] = diode(t,x,u,flag)
switch flag,
case 0,
[sys,x0,str,ts]=mdlInitializeSizes;
case 1,
sys=mdlDerivatives(t,x,u);
case 2,
sys=mdlUpdate(t,x,u);
case 3,
sys=mdlOutputs(t,x,u);
case 4,
sys=mdlGetTimeOfNextVarHit(t,x,u);
case 9,
sys=mdlTerminate(t,x,u);
46
otherwise
DAStudio.error('Simulink:blocks:unhandledFlag',
num2str(flag));
end
function [sys,x0,str,ts]=mdlInitializeSizes
sizes = simsizes;
sizes.NumContStates = 0;
sizes.NumDiscStates = 0;
sizes.NumOutputs = -1;
sizes.NumInputs = -1;
sizes.DirFeedthrough = 1;
sizes.NumSampleTimes = 1; % at least one sample time is needed
sys = simsizes(sizes);
x0 = [];
str = [];
ts = [0 0];
function sys=mdlDerivatives(t,x,u)
sys = [];
function sys=mdlUpdate(t,x,u)
sys = [];
function sys=mdlOutputs(t,x,u)
if u>0.6
sys=u-0.6;
else sys=0
end
function sys=mdlGetTimeOfNextVarHit(t,x,u)
function sys=mdlTerminate(t,x,u)
sys = [];
47
OUTPUT: