Sie sind auf Seite 1von 6

42

9. S - FUNCTION
AIM: To write a program to understand the concept of S – Function in MATLAB

SOFTWARE REQUIRED: MATLAB

THEORY:

An S-function is a computer language description of a dynamic system. S-functions can be


written using MATLAB or C. C language S-functions are compiled as MEX-files using the MEX
utility. As with other MEX-files, they are dynamically linked into MATLAB when needed.
S-functions use a special calling syntax that enables us to interact with Simulink’s equation
solvers. This interaction is very similar to the interaction that takes place between the solvers and
built-in Simulink blocks.The form of an S-function is very general and can accommodate
continuous, discrete, and hybrid systems. As a result, nearly all Simulink models can be described
as S-functions.S-functions are incorporated into Simulink models by using the S-Function block in
the Nonlinear Block sublibrary. Use the S-Function block’s dialog box to specify the name of the
underlying S-function.
The most common use of S-functions is to create custom Simulink blocks. But it can use
S-functions for a variety of applications, including:
•Adding new general purpose blocks to Simulink
•Incorporating existing C code into a simulation
•Describing a system as a mathematical set of equations
•Using graphical animations
Each block within a Simulink model has the following general characteristics: a vector of inputs, u,
a vector of outputs, y, and a vector of states, x, as shown by this illustration: The state vector may
consist of continuous states, discrete states, or a combination of both. The mathematical
relationships between the inputs, outputs, and the states are expressed by the following equations:
In M-file S-functions, Simulink partitions the state vector into two parts: the continuous states and
the discrete states. The continuous states occupy the first part of the state vector, and the discrete
states occupy the second part.
43

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.

Simulation Stages and S-Function Routines


Simulink makes repeated calls during specific stages of simulation to each block in the
model, directing it to perform tasks such as computing its outputs, updating its discrete states, or
computing its derivatives. Additional calls are made at the beginning and end of a simulation to
perform initialization and termination tasks.
First, Simulink initializes the model; this includes initializing each block, including S-functions.
Then Simulink enters the simulation loop, where each pass through the loop is referred to as a
simulation step. During each simulation step, Simulink executes your S-function block. This
continues until the simulation is complete:
Simulink makes repeated calls to S-functions in your model. During these calls, Simulink calls S-
function routines (also called methods), which perform tasks required at each stage. These tasks
include:
•Initialization — Prior to the first simulation loop, Simulink initializes the S-function.
During this stage, Simulink: - Initializes the SimStruct, a simulation structure that contains
information about the S-function. - Sets the number and size of input and output ports. - Sets
the block sample time(s).- Allocates storage areas and the sizes array.
•Calculation of next sample hit — If a variable step integration routine is selected , this
stage calculates the time of the next variable hit, that is, it calculates the next stepsize.
•Calculation of outputs in the major time step — After this call is complete, all the output
ports of the blocks are valid for the current time step.
44
•Update discrete states in the major time step—In this call, all blocks should perform
once-per-time-step activities such as updating discrete states for next time around the
simulation loop.
•Integration — This applies to models with continuous states and/or nonsampled zero
crossings. If the S-function has continuous states, Simulink calls the output and derivative
portions of the S-function at minor time steps. This is so Simulink can compute the state(s)
for S-function.

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)

sampleTime = 1; % Example, set the next hit to be one second


later.
sys = t + sampleTime;

function sys=mdlTerminate(t,x,u)

sys = [];
47
OUTPUT:

RESULT: A simple S Function was written and built in Simulink in MATLAB

Das könnte Ihnen auch gefallen