Documentos de Académico
Documentos de Profesional
Documentos de Cultura
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.
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.
1141
The Level1 Mfile SFunctions are shown in Figure 11.64 and the Level1 Mfile SFunctions are shown in Figure 11.65.
1142
SFunctions in Simulink
Figure 11.66 shows the Subsystem Semantics (Definitions) for the Simulink family of 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.
1143
(11.3)
vC = y
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
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
This specifies the input and output arguments. a. Input arguments t time variable
1145
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
1146
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;
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
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
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.
* 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
SFunctions in Simulink
Figure 11.69. Input and output waveforms for the model of Figure 11.68
1149