S-Function Concepts - MATLAB & Simulink (original) (raw)

Direct Feedthrough

Direct feedthrough means that the output (or the variable sample time for variable sample time blocks) is controlled directly by the value of an input port signal. Typically, an S-function input port has direct feedthrough if

An example of a system that requires its inputs (that is, has direct feedthrough) is the operation

where u is the input, k is the gain, and_y_ is the output.

An example of a system that does not require its inputs (that is, does not have direct feedthrough) is the simple integration algorithm

where x is the state, x˙ is the state derivative with respect to time, u is the input, and y is the output. Simulink® integrates the variable x˙.

It is very important to set the direct feedthrough flag correctly because it affects the execution order of the blocks in your model and is used to detect algebraic loops (see Algebraic Loop Concepts in Using Simulink). If the simulation results for a model containing your S-function do not converge, or the simulation fails, you may have the direct feedthrough flag set incorrectly. Try turning on the direct feedthrough flag and setting the Algebraic loop solver diagnostic towarning (see the Algebraic loop option on the Model Configuration Parameters: Diagnostics reference page in Simulink Graphical User Interface). Subsequently running the simulation displays any algebraic loops in the model and shows if the engine has placed your S-function within an algebraic loop.

Dynamically Sized Arrays

You can write your S-function to support arbitrary input dimensions. In this case, the Simulink engine determines the actual input dimensions when the simulation is started by evaluating the dimensions of the input vectors driving the S-function. Your S-function can also use the input dimensions to determine the number of continuous states, the number of discrete states, and the number of outputs.

Note

A dynamically sized input can have a different size for each instance of the S-function in a particular model or during different simulations, however the input size of each instance of the S-function is static over the course of a particular simulation.

A C MEX S-function and Level-2 MATLAB® S-function can have multiple input and output ports and each port can have different dimensions. The number of dimensions and the size of each dimension can be determined dynamically.

For example, the following illustration shows two instances of the same S-Function block in a model.

The upper S-Function block is driven by a block with a three-element output vector. The lower S-Function block is driven by a block with a scalar output. By specifying that the S-Function block has dynamically sized inputs, the same S-function can accommodate both situations. The Simulink engine automatically calls the block with the appropriately sized input vector. Similarly, if other block characteristics, such as the number of outputs or the number of discrete or continuous states, are specified as dynamically sized, the engine defines these vectors to be the same length as the input vector.

See Input and Output Ports for more information on configuring S-function input and output ports.

Setting Sample Times and Offsets

Both Level-2 MATLAB and C MEX S-functions provide the following sample time options, which allow for a high degree of flexibility in specifying when an S-function executes:

S-functions can be either single or multirate; a multirate S-function has multiple sample times.

Sample times are specified in pairs in this format: [sample_time, offset_time].

Valid C MEX S-Function Sample Times

The valid sample time pairs for a C MEX S-function are

[CONTINUOUS_SAMPLE_TIME, 0.0] [CONTINUOUS_SAMPLE_TIME, FIXED_IN_MINOR_STEP_OFFSET] [discretesampletimeperiod, offset] [VARIABLE_SAMPLE_TIME, 0.0]

where

CONTINUOUS_SAMPLE_TIME = 0.0 FIXED_IN_MINOR_STEP_OFFSET = 1.0 VARIABLE_SAMPLE_TIME = -2.0

and variable names in italics indicate that a real value is required.

Alternatively, you can specify that the sample time is inherited from the driving block. In this case, the C MEX S-function has only one sample time pair, either

[INHERITED_SAMPLE_TIME, 0.0]

or

[INHERITED_SAMPLE_TIME, FIXED_IN_MINOR_STEP_OFFSET]

where

INHERITED_SAMPLE_TIME = -1.0

Valid Level-2 MATLAB S-Function Sample Times

The valid sample time pairs for a Level-2 MATLAB S-function are

[0 offset] % Continuous sample time [discretesampletimeperiod, offset] % Discrete sample time [-1, 0] % Inherited sample time [-2, 0] % Variable sample time [0, 1] % Fixed-in-minor-step

where variable names in italics indicate that a real value is required. When using a continuous sample time, an offset of1 indicates the output is fixed in minor integration time steps. An offset of 0 indicates the output changes at every minor integration time step.

Level-2 MATLAB S-function doesn't allow [-1, 1] sample time.

Guidelines for Choosing a Sample Time

Use the following guidelines for help with specifying sample times:

If your S-function has no intrinsic sample time, you must indicate that your sample time is inherited. There are two cases:

See Specify S-Function Sample Times for information on implementing different types of sample times in S-functions.

See Also

Level-2 MATLAB S-Function | S-Function Builder | S-Function | MATLAB Function

Topics