This action might not be possible to undo. Are you sure you want to continue?
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.
Additional calls are made at the beginning and end of a simulation to perform initialization and termination tasks. Simulink initializes the S-function. including S-functions. Simulink calls Sfunction routines (also called methods). Simulink: .Sets the block sample time(s).Allocates storage areas and the sizes array. First. Then Simulink enters the simulation loop. a simulation structure that contains information about the S-function.43 For blocks with no states. Simulink initializes the model. •Calculation of next sample hit — If a variable step integration routine is selected . where each pass through the loop is referred to as a simulation step. Simulink executes your S-function block. directing it to perform tasks such as computing its outputs. which perform tasks required at each stage.Initializes the SimStruct. During this stage. updating its discrete states. x is an empty vector. . Simulation Stages and S-Function Routines Simulink makes repeated calls during specific stages of simulation to each block in the model. this stage calculates the time of the next variable hit. this includes initializing each block. These tasks include: •Initialization — Prior to the first simulation loop. . During each simulation step.Sets the number and size of input and output ports. In MEX-file S-functions. •Calculation of outputs in the major time step — After this call is complete. it calculates the next stepsize. there are two separate state vectors for the continuous and discrete states.. . This continues until the simulation is complete: Simulink makes repeated calls to S-functions in your model. or computing its derivatives. all the output ports of the blocks are valid for the current time step. that is. During these calls.
If the S-function has continuous states. STEPS: STEP 1 Write the S-function in the MATLAB edit window using M file. This is so Simulink can compute the state(s) for S-function. . all blocks should perform once-per-time-step activities such as updating discrete states for next time around the simulation loop. Simulink calls the output and derivative portions of the S-function at minor time steps. 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. •Integration — This applies to models with continuous states and/or nonsampled zero crossings.44 •Update discrete states in the major time step—In this call.
x.u).u). sys=mdlUpdate(t.x0.u). . sys=mdlGetTimeOfNextVarHit(t. sys=mdlTerminate(t.x.6 and test it with a SINWAVE INPUT: SIMULINK MODEL DIODE M FILE function [sys.x. case 3. case 9.str. case 4. sys=mdlDerivatives(t.u.x0.flag) switch flag.ts] = diode(t.str.u). case 1.x.x. case 0.x. sys=mdlOutputs(t.u).ts]=mdlInitializeSizes.45 EXERCISE: (1) Create a diode model using Simulink s-function which will pass the wave whose input is greater than 0. case 2. [sys.
else sys=0 end function sys=mdlGetTimeOfNextVarHit(t.u) sys = .u) sys = .x. % at least one sample time is needed sys = simsizes(sizes).NumInputs sizes.NumContStates sizes.DirFeedthrough sizes.x0.6. sizes.46 otherwise DAStudio.u) if u>0. end function [sys. set the next hit to be one second later.error('Simulink:blocks:unhandledFlag'. x0 = . function sys=mdlUpdate(t. function sys=mdlDerivatives(t. ts = [0 0]. % Example. -1.u) sampleTime = 1.x. 0.x. sys = t + sampleTime. str = .NumSampleTimes = = = = = = 0. -1. function sys=mdlOutputs(t.x.6 sys=u-0.NumDiscStates sizes.ts]=mdlInitializeSizes sizes = simsizes. 1.x.str. function sys=mdlTerminate(t. .u) sys = . num2str(flag)). 1.NumOutputs sizes.
47 OUTPUT: RESULT: A simple S Function was written and built in Simulink in MATLAB .