Writing S-Functions    

How S-Functions Work

To create S-functions, you need to know how S-functions work. Understanding how S-functions work, in turn, requires understanding how Simulink simulates a model, and this, in turn requires an understanding of the mathematics of blocks. This section therefore begins by explaining the mathematical relationship between a block's inputs, states, and outputs.

Mathematics of Simulink Blocks

A Simulink block consists of a set of inputs, a set of states, and a set of outputs, where the outputs are a function of the sample time, the inputs, and the block's states.

The following equations express the mathematical relationships between the inputs, outputs, and the states.

Simulation Stages

Execution of a Simulink model proceeds in stages. First comes the initialization phase. In this phase, Simulink incorporates library blocks into the model, propagates widths, data types, and sample times, evaluates block parameters, determines block execution order, and allocates memory. Then Simulink enters a simulation loop, where each pass through the loop is referred to as a simulation step. During each simulation step, Simulink executes each of the model's blocks in the order determined during initialization. For each block, Simulink invokes functions that compute the block's states, derivatives, and outputs for the current sample time. This continues until the simulation is complete.

The following figure illustrates the stages of a simulation.

Figure 1-2: How Simulink Performs Simulation


  When to Use an S-Function S-Function Callback Methods