Modeling and Simulating Discrete Systems

Multirate and Hybrid Systems

The Simulink® product has the ability to simulate discrete (sampled data) systems, including systems whose components operate at different rates (multirate systems) and systems that mix discrete and continuous components (hybrid systems). This capability stems from two key Simulink features:

The ability to specify sample times on a block-by-block basis, either directly through the SampleTime parameter or indirectly through inheritance, enables you to model systems containing discrete components operating at different rates and hybrid systems containing discrete and continuous components.

Specifying Sample Time

You can specify the sample time of any block that has a SampleTime parameter. You can use the block's parameter dialog box to set this parameter. You do this by entering the sample time in the Sample time field on the dialog box. You can enter either the sample time alone or a vector whose first element is the sample time and whose second element is an offset: [Ts, To]. Various values of the sample time and offset have special meanings.

The following table summarizes valid values for this parameter and how the Simulink software interprets them to determine a block's sample time.

Sample TimeUsage

[Ts, To]
0 < T
s < Tsim
|T
o| < Ts

Specifies that updates occur at simulation times

tn = n * Ts + |To| 

where n is an integer in the range 0..Tsim/Ts and Tsim is the length of the simulation. Blocks that have a sample time greater than 0 are said to have a discrete sample time.

The offset allows you to specify that the block be updated later in the sample interval than other blocks operating at the same rate.

[0, 0], 0

Specifies that updates occur at every major and minor time step. A block that has a sample time of 0 is said to have a continuous sample time.

[0, 1]

Specifies that updates occur only at major time steps, skipping minor time steps (see Minor Time Steps). This setting avoids unnecessary computations for blocks whose sample time cannot change between major time steps. The sample time of a block that executes only at major time steps is said to be fixed in minor time step.

[-1, 0], -1

If the block is not in a triggered subsystem, this setting specifies that the block inherits its sample time from the block connected to its input (inheritance) or, in some cases, from the block connected to its output (back inheritance). If the block is in a triggered subsystem, you must set the SampleTime parameter to this setting.

Note that specifying sample-time inheritance for a source block can cause the Simulink software to assign an inappropriate sample time to the block if the source drives more than one block. For this reason, you should avoid specifying sample-time inheritance for source blocks. If you do, a warning message is displayed when you update or simulate the model.

[-2, Tvo]

Specifies that a block has a variable sample time, that is, computes its output only at times specified by the block. Every block with variable sample time has a unique Tvo determined by the Simulink software. The only built-in Simulink block that can have variable sample time is the Pulse Generator block.

inf

The meaning of this sample time depends on whether the active model configuration's inline parameters optimization (see Inline parameters) is enabled.

If the inline parameters optimization is enabled, inf signifies that the block's output can never change (see Constant Sample Time). This speeds up simulation and the generated code by eliminating the need to recompute the block's output at each time step. If the inline parameters optimization is disabled or the block with inf sample time drives an output port of a conditionally executed subsystem, inf is treated as -1, i.e., as inherited sample time. This allows you to tune the block's parameters during simulation.

Changing a Block's Sample Time

You cannot change the SampleTime parameter of a block while a simulation is running. If you want to change a block's sample time, you must stop and restart the simulation for the change to take effect.

Compiled Sample Time

During the compilation phase of a simulation, the sample time of the block is determined from its SampleTime parameter (if it has a SampleTime parameter), sample-time inheritance, or block type (Continuous blocks always have a continuous sample time). It is this compiled sample time that determines the sample rate of a block during simulation. You can determine the compiled sample time of any block in a model by first updating the model and then getting the block's CompiledSampleTime parameter, using the get_param command.

Purely Discrete Systems

Purely discrete systems can be simulated using any of the solvers; there is no difference in the solutions. To generate output points only at the sample hits, choose one of the discrete solvers.

Multirate Systems

Multirate systems contain blocks that are sampled at different rates. These systems can be modeled with discrete blocks or with both discrete and continuous blocks. For example, consider this simple multirate discrete model.

For this example the DTF1 Discrete Transfer Fcn block's Sample time is set to [1 0.1], which gives it an offset of 0.1. The DTF2 Discrete Transfer Fcn block's Sample time is set to 0.7, with no offset.

Starting the simulation and plotting the outputs using the stairs function

[t,x,y] = sim('multirate', 3);
stairs(t,y)

produces this plot

See Running a Simulation Programmatically for information on the sim command.

For the DTF1 block, which has an offset of 0.1, there is no output until t = 0.1. Because the initial conditions of the transfer functions are zero, the output of DTF1, y(1), is zero before this time.

Determining Step Size for Discrete Systems

Simulating a discrete system requires that the simulator take a simulation step at every sample time hit, that is, at integer multiples of the system's shortest sample time. Otherwise, the simulator might miss key transitions in the system's states. This is avoided by choosing a simulation step size to ensure that steps coincide with sample time hits. The step size that the Simulink software chooses depends on the system's fundamental sample time and the type of solver used to simulate the system.

The fundamental sample time of a discrete system is the greatest integer divisor of the system's actual sample times. For example, suppose that a system has sample times of 0.25 and 0.5 second. The fundamental sample time in this case is 0.25 second. Suppose, instead, the sample times are 0.5 and 0.75 second. In this case, the fundamental sample time is again 0.25 second.

You can direct the Simulink software to use either a fixed-step or a variable-step discrete solver to solve a discrete system. A fixed-step solver sets the simulation step size equal to the discrete system's fundamental sample time. A variable-step solver varies the step size to equal the distance between actual sample time hits.

The following diagram illustrates the difference between a fixed-step and a variable-size solver.

In the diagram, arrows indicate simulation steps and circles represent sample time hits. As the diagram illustrates, a variable-step solver requires fewer simulation steps to simulate a system, if the fundamental sample time is less than any of the actual sample times of the system being simulated. On the other hand, a fixed-step solver requires less memory to implement and is faster if one of the system's sample times is fundamental. This can be an advantage in applications that entail generating code from a Simulink model (using Real-Time Workshop®).

Sample Time Propagation

When updating a model's diagram, for example, at the beginning of a simulation, a process called sample time propagation is used to determine the sample times of blocks that inherit their sample times. The figure below illustrates a Discrete Filter block with a sample time of Ts driving a Gain block.

Because the Gain block's output is simply the input multiplied by a constant, its output changes at the same rate as the filter. In other words, the Gain block has an effective sample rate equal to that of the filter's sample rate. This is the fundamental mechanism behind sample time propagation in the Simulink product.

An inherited sample time is assigned to a block based on the sample times of the blocks connected to its inputs, using the following rules.

Propagating Sample Times Back to Source Blocks

When you update or simulate a model that specifies a source block's sample time as inherited (-1), the source block's sample time may be back propagated, i.e., it sets the source block's sample time to be the same as the sample time specified or inherited by the block to which the source block is connected. This only happens if it can be done without changing the results of simulating the model. For example, in the model below, the Simulink software recognizes that the Sine Wave block is driving a Discrete-Time Integrator block whose sample time is 1, so it assigns the Sine Wave block a sample time of 1.

You can verify this by selecting Sample Time Colors from the Simulink Format menu and noting that all blocks are colored red. Because the Discrete-Time Integrator block only looks at its input at its sample times, this change does not affect the outcome of the simulation but does result in a performance improvement.

Replacing the Discrete-Time Integrator block with a continuous Integrator block, as shown below, and recoloring the model by choosing Update diagram from the Edit menu cause the Sine Wave and Gain blocks to change to continuous blocks, as indicated by their being colored black.

Constant Sample Time

A block whose output cannot change from its initial value during a simulation is said to have constant sample time. A block has constant sample time if it satisfies both of the following conditions:

When the Simulink software updates a model, for example, at the beginning of a simulation, it determines which blocks, if any, have constant sample time, and computes the initial values of the output ports. During the simulation, the initial values are used whenever the outputs of blocks with constant sample time are required, thus avoiding unnecessary computations.

You can determine which blocks have constant sample time by selecting Sample Time Colors from the Format menu and updating the model. Blocks with constant sample time are colored magenta.

For example, in this model, as sample time colors show, both the Constant and Gain blocks have constant sample time.

The Gain block has constant sample time because it inherits its sample time from the Constant block and all of the model's parameters are inlined, i.e., nontunable.

How Blocks with Infinite Sample Times and Tunable Parameters are Treated

A block that has tunable parameters cannot have constant sample time even if its sample time is specified to be infinite. This is because the fact that a block has one or more tunable parameters means that you can change the values of its parameters during simulation and hence the value of its outputs. In this case, sample time propagation (see Sample Time Propagation) is used to determine the block's actual sample time.

For example, consider the following model.

In this example, although the Constant block's sample time is specified to be infinite, it cannot have constant sample time because the inlined parameters option is off for this model and therefore the block's Constant value parameter is tunable. Since the Constant block's output can change during the simulation, a sample time for the block has to be determined that ensures accurate simulation results. It does this by treating the Constant block's sample time as inherited and using sample time propagation to determine its sample time. The first nonvirtual block in the diagram branch to which the Constant block is connected is the Discrete-Time Integrator block. As a result, the block inherits its sample time (1 sec) via back propagation from the Discrete-Time Integrator block.

Mixed Continuous and Discrete Systems

Mixed continuous and discrete systems are composed of both sampled and continuous blocks. Such systems can be simulated using any of the integration methods, although certain methods are more efficient and accurate than others. For most mixed continuous and discrete systems, the Runge-Kutta variable-step methods, ode23 and ode45, are superior to the other methods in terms of efficiency and accuracy. Because of discontinuities associated with the sample and hold of the discrete blocks, the ode15s and ode113 methods are not recommended for mixed continuous and discrete systems.

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS