How does Simulink perform simulations?

1 view (last 30 days)
I would like to know how Simulink performs simulations for continuous and discrete systems. I would like to know what the flag ordering of S-function calls is, and when and why the different flags are called during a simulation.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 23 Aug 2010
There are generally two types of signals that are simulated in Simulink: discrete and continuous. Discrete signals are simulated via difference equations while continuous signals are simulated via various numerical integration algorithms (e.g., Euler). Refer to numerical methods texts for more information regarding integration algorithms.
The simulation may be thought of as a formal conversation between two entities: the system description and the integrator. The system description (block diagram or S-function) is responsible for supplying various information to the integrator, while the integrator is responsible for propagating the system through time.
Let's consider a continuous state. In order to get from time 't' to time 't+delta_t', the system description must supply the integrator with the derivative. The integrator then uses this derivative to obtain the next value of the state. It is the responsibility of the integrator to decide how far in time to step along the current derivative and check the error tolerances to determine if the new state is accurate. In terms of Simulink, the above interaction results in the integrator calling the system with a flag of 1 (i.e., flag 1 = request for current derivative). Similarly, the system description must supply information regarding the outputs of the system. The integrator requests information regarding the current outputs by calling the system with a flag of 3.
Now, let's consider a discrete state. In this case the integrator is not responsible for as much work as it is in the continuous case. The discrete states are propagated through time via difference equations
x[n+1] = x[n] + .........
The system description includes an expression for calculating x[n+1] for each state, and the integrator is responsible only for taking care of the timing. For example, if the sample time is 1 second and at time t=3 the value of x[n+1] is defined as 10, then the integrator makes sure that at time, t + ts, the value of the state is 10. In order for this timing to work correctly, the integrator forces an integration step at the times of the sample hits. This means that at the time of every discrete event, all states are updated (both continuous and discrete). For times between sample hits, the values of the discrete states are held constant (i.e., zero order hold).
If you are familiar with S-functions (see the Simulink documentation) and the various flag calls, the following is an explanation of when and why S-functions are called with the various flags. First, it will be useful to know the order in which the flags are called (excluding flag zero which is only called at the beginning of each simulation). Assuming that at each time step, the system was called with every flag, the order would be 4,3,2,1. However, as we will soon see, not all flags are used at every time step. The above order, however, still applies to the flags that are being used.
Before we look at specific cases, it will also be useful to discuss flag 2. In general, flag 2 is used to update discrete states. In Simulink, flag 2 is called at every RrealS integration step, where real is defined as steps that correspond to the time vector that is returned by Simulink. Some of the integration algorithms take intermediate steps during which other flags (1 and 3) are called. Flag 2 is called at every time step so that actions such as plotting or data collection could be executed at every time step of the simulation. Two disadvantages of this are that a check must be made in order to execute the discrete update code only at sample hits, and that it is inefficient to make discrete flag calls at every integration hit. For purely discrete blocks, flag 2 is only called at sample hits. This allows simulation for discrete operations to be more efficient.
Below is an explanation of how flags work in Simulink:
Case 1: Continuous System
------------------------
flag 3 => called at every integration step (including intermediate steps)
flag 1 => R......S
flag 2 => called at every real integration step
flag 4 => called at the begining of the simulation and set to arbitrarily large number indicating that no discrete updates are necessary.
Case 2: Discrete System
-----------------------
flag 3 => same as continous system
flag 1 => not called
flag 2 => called every integration step...must check for sample hits
flag 4 => called at every sample hit

More Answers (0)

Categories

Find more on Simulink in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!