You are on page 1of 9

The Switch Case and The Switch Case Action Subsystem Blocks 11.

16 The Switch Case and The Switch Case Action Subsystem Blocks

The Switch Case block implement a Clike switch control flow statement. The Switch Case Action Subsystem block is a Subsystem block that is preconfigured to serve as a starting point for creating a subsystem whose execution is triggered by a Switch Case block. For an example, please refer to the Help menu for the Switch Case block that includes also a pseudocode.

11.17 The Subsystem Examples Block

The Subsystem Examples block includes a library of Sfunctions. To run an example, in MATLABs Command Window we type
sfundemos

and MATLAB will display the SFunction directory blocks shown in Figure 11.62. In this text we will be concerned with the Mfile SFunctions only. An introduction to Sfunctions with an example is presented in the next section. Next, we double-click on the Mfile SFunctions block of Figure 11.62 and MATLAB displays the Level1 and Level2 Mfile SFunctions shown in Figure 11.63.

Figure 11.62. SFunction directory blocks

Introduction to Simulink with Engineering Applications Copyright Orchard Publications

1141

Chapter 11 The Ports & Subsystems Library

Figure 11.63. Levels of Mfile SFunctions

The Level1 Mfile SFunctions are shown in Figure 11.64 and the Level1 Mfile SFunctions are shown in Figure 11.65.

Figure 11.64. List of Level1 Mfile SFunctions

Figure 11.65. List of Level2 Mfile SFunctions

1142

Introduction to Simulink with Engineering Applications Copyright Orchard Publications

SFunctions in Simulink
Figure 11.66 shows the Subsystem Semantics (Definitions) for the Simulink family of subsystems.

Figure 11.66. Classes and types of Simulink subsystems

Simulink consists of two classes of subsystems, Virtual subsystems and Nonvirtual subsystems. Virtual subsystems provide graphical hierarchy in models and do not impact execution. Nonvirtual subsystems are executed as a single unit (atomic execution) by Simulink. The blocks within a nonvirtual subsystem execute only when all subsystems inputs are valid. All nonvirtual subsystems are drawn with a bold border. It is highly recommended that each of the subsystem blocks shown in Figure 11.66 be explored to become familiar with them.

11.18 SFunctions in Simulink


An Sfunction is a computer language description of a Simulink block. Sfunctions can be written in MATLAB, C, C++, Ada, or Fortran. Files in C, C++, Ada, and Fortran Sfunctions are compiled as mex-files using the mex utility.* Sfunctions use a special calling syntax that enables us to interact with Simulink's equation solvers. The form of an Sfunction is very general and applies to continuous, discrete, and hybrid systems. Sfunctions allow us to add our own blocks to Simulink models. After we write our Sfunction and place its name in an SFunction block. We can also use Sfunctions with the Real-Time Workshop. With mfile SFunctions we can define our own ordinary differential equations, discrete-time system equations and any type of algorithm that can be used with Simulink block diagrams. To become familiar with SFunctions, we begin our discussion with an example.
* For a discussion on mexfiles please refer to in the online MATLAB documentation. These files are dynamically linked into MATLAB when specified. In this text we will only be concerned with mfiles. Real-Time Workshop is an extension of the capabilities provided by MATLAB and Simulink. It generates, and compiles source code from Simulink models to create real-time software applications on a variety of systems. Refer to Writing SFunctions for RealTime Workshop and the RealTime Workshop documentation for more information.

Introduction to Simulink with Engineering Applications Copyright Orchard Publications

1143

Chapter 11 The Ports & Subsystems Library


Example 11.13 For the simple RC circuit of Figure 11.41 it can be shown* that the statespace equations are
dx 1 ----- = ------- x + V S RC dt y = x R VS C +

(11.3)

vC = y

Figure 11.67. RC circuit for Example 11.13

Example 11.14 We will create an SFunction block that implements the relations of (11.3). We begin by writing the function mfile below and we save it as RCckt.m
function dx = RCckt(t,x,Vs) % % Model for RC series circuit, function m-file RCckt.m % % Define circuit constants % R = 10^6; % Resistance in Ohms C = 10^(-6); % Capacitance in Farads dx = -1/(R*C)*x+Vs; % % % % The arguments x and dx are column vectors for state and derivative respectively. The variable t on the first line above specifies the simulation time. The default is [0 10].

To test this function mfile for correctness, on MATLABs Command Window we issue the command
[t,x,Vs]=ode45(@RCckt, [0 10], 0, [ ], 1)

The above command specifies a simulation time interval [0 10], an initial condition value of 0, the null vector [ ] can be used for options, and the input value is set to 1. Upon execution of this command MATLAB displays several values for t, x, and Vs. Next, we write the Sfunction mfile shown below, and we save it as RCckt_sfun.m An explanation for each line of this file is provided afterwards.
* For a detailed discussion on state variables, please refer to Signals and Systems with MATLAB Applications, ISBN 0970951167.

1144

Introduction to Simulink with Engineering Applications Copyright Orchard Publications

SFunctions in Simulink
function [sys,x0,str,ts]=... RCckt_sfcn(t,x,u,flag,xinit) % % This is the m-file S-Function RCckt_sfcn.m % switch flag case 0 str = []; ts = [0 0]; x0 = xinit; % Alternately, the three lines above can be combined into a single line as % [sys,x0,str,ts]=mdlInitializeSizes(t,x,u) sizes = simsizes; sizes.NumContStates = 1; sizes.NumDiscStates = 0; sizes.NumOutputs = 1; sizes.NumInputs = 1; sizes.DirFeedthrough = 0; sizes.NumSampleTimes = 1; sys =simsizes(sizes); case 1 Vs = u; sys = RCckt(t,x,Vs); case 3 sys = x; case {2 4 9} sys = []; otherwise error(['unhandled flag =',num2str(flag)]); end % 2:discrete % 3:calcTimeHit % 9:termination % Output % Derivatives % Initialize

The first line of the Sfunction m-file RCckt_sfun.m is written as


function [sys,x0,str,ts]=... RCckt_sfcn(t,x,u,flag,xinit)

This specifies the input and output arguments. a. Input arguments t time variable

Introduction to Simulink with Engineering Applications Copyright Orchard Publications

1145

Chapter 11 The Ports & Subsystems Library


x column vector for the state variables u column vector for the input variables; will be supplied by other Simulink blocks flag an indication of which group of information and calculations. The table below lists the integer numbers assigned to an Sfunction routine.
TABLE 11.3 Flags used in S-function m-files Flag 0 1 2 3 4 9 S-Function Routine mdlInitializeSizes mdlDerivatives mdlUpdate mdlOutputs mdlGetTimeOfNextVarHit mdlTerminate Simulation Stage Initialization - sets input and output vector sizes and specifies initial conditions for the state variables. Calculation of derivatives Update of discrete states - not used for this example Calculation of outputs Calculation of next sample hit - not used for this example End of simulation tasks

additional supplied parameter; in this example the initial condition a. Output arguments
xinit -

sys a vector of information requested by Simulink. This vector will hold different information depending on the flag value as shown in the table below.
TABLE 11.4 Information for vector sys for different flag values Flag 0 Information requested sys = [a, b, c, d, e, f, g] a = number of continuous time states b = number of discrete time states c = number of outputs d= number of inputs e = not used but must be set to 0 if requested f = applies to direct algebraic feed through of input to output, 0 for No, 1 for Yes. It is relevant if during flag=3, the output variables depend on the input variables. g = number of sample times. For continuous systems must be set to 1. sys = column vector of the state variables derivatives sys = column vector of output variables sys = [ ] (null vector) if not applicable

1 3 2,4,9

x0 a column vector of initial conditions. Applies only to flag = 0

1146

Introduction to Simulink with Engineering Applications Copyright Orchard Publications

SFunctions in Simulink
str reserved for future use; for m-file Sfunctions it must be set to null vector. Applies only to flag = 0 ts an array of two columns to specify sample time and time offsets. For continuous-time systems it is set to [0 0]. If it is desired that Sfunction should run at the same rate as the block to which it is connected (inherited sample time), we must set ts to [1 0]. If we want to run at discrete sample time, say 0.25 seconds starting at 0.1 seconds after the start of simulation time, we must set ts to [0.25 0.1]. Applies only to flag = 0. Let us now review the mfile Sfunction RCckt_sfcn structure. We begin with the function RCckt_sfcn defined as follows:
function [sys,x0,str,ts]=... RCckt_sfcn(t,x,u,flag,xinit) % % This is the m-file S-Function RCckt_sfun.m %

Next, we use flag; this specifies an integer value that indicates the task to be performed by the S function and begins with the statement
switch flag case 0 str = []; ts = [0 0]; x0 = xinit;

Initialization begins with


% Initialize % % % % Must be set to null. Reserved for future use Specify sampling time. For continuous-time systems is always set to [0 0] Column vector for initial conditions

Simulink will not recognize our mfile Sfunction unless we provide specific information about number of inputs, number of outputs, states, and other characteristics. This information is provided with the simsizes function. This function returns an initialized structure of the variables in which we can assign the required values. Thus, in MATLABs Command Window we invoke this command as shown below and we manually enter the values shown.
sizes = simsizes; sizes.NumContStates = 1; sizes.NumDiscStates = 0; sizes.NumOutputs = 1; sizes.NumInputs = 1; sizes.DirFeedthrough = 0; sizes.NumSampleTimes = 1;

Direct Feedthrough in line 5 above implies that the output is controlled by the value of the input. Generally, if an SFunction has an input port, it has direct feedthrough if the input u is accessed in mdlOutputs. For instance, if y = ku where u is the input, k is the gain, and y is the output, the system has direct feedthrough only if flag=3. After we initialize the sizes structure we invoke simsizes again as shown below Introduction to Simulink with Engineering Applications Copyright Orchard Publications

1147

Chapter 11 The Ports & Subsystems Library


sys =simsizes(sizes);

and this passes the information in the sizes structure to sys which is a vector that holds the information required by Simulink.* For case 1 (derivatives) we assign V S to the input u and then we apply it to the RCckt.m file. Thus,
case 1 Vs = u; sys = RCckt(t,x,Vs); % Derivatives

For case 3 (output) we assign the output x to the input sys. Thus,
case 3 sys = x; % Output

Flags 2, 4, and 9 are not used so they output the null vector sys = [] shown below.
case {2 4 9} sys = []; % 2:discrete % 3:calcTimeHit % 9:termination

otherwise error(['unhandled flag =',num2str(flag)]); end

Next, we open a window to create a new model, from the UserDefined Functions library we drag an SFunction block into it, in the Function Block Parameters dialog box we assign the S function name RCckt_sfcn to it, we type the initial condition 0, and we add the other blocks shown in Figure 11.68. The parameter values can be constants, names of variables defined in the models workspace, or MATLAB expressions. The input and output waveforms are shown in Figure 11.69.

Figure 11.68. Model for Example 11.14

* Upon execution of the statement sys=simsizes(sizes), MATLAB displays a row vector of seven 0s, one for each of the simsizes function above. Sys(5) is reserved for root finding and for the present must be set to 0.

1148

Introduction to Simulink with Engineering Applications Copyright Orchard Publications

SFunctions in Simulink

Figure 11.69. Input and output waveforms for the model of Figure 11.68

Introduction to Simulink with Engineering Applications Copyright Orchard Publications

1149

You might also like