| Using Simulink | ![]() |
Some Simulink blocks have input ports with direct feedthrough. This means that the output of these blocks cannot be computed without knowing the values of the signals entering the blocks at these input ports. Some examples of blocks with direct feedthrough inputs are as follows:
An algebraic loop generally occurs when an input port with direct feedthrough is driven by the output of the same block, either directly, or by a feedback path through other blocks with direct feedthrough. An example of an algebraic loop is this simple scalar loop.
Mathematically, this loop implies that the output of the Sum block is an algebraic state z constrained to equal the first input u minus z (i.e. z = u - z). The solution of this simple loop is z = u/2, but most algebraic loops cannot be solved by inspection.
It is easy to create vector algebraic loops with multiple algebraic state variables z1, z2, etc., as shown in this model.
The Algebraic Constraint block is a convenient way to model algebraic equations and specify initial guesses. The Algebraic Constraint block constrains its input signal F(z) to zero and outputs an algebraic state z. This block outputs the value necessary to produce a zero at the input. The output must affect the input through some feedback path. You can provide an initial guess of the algebraic state value in the block's dialog box to improve algebraic loop solver efficiency.
A scalar algebraic loop represents a scalar algebraic equation or constraint of the form F(z) = 0, where z is the output of one of the blocks in the loop and the function F consists of the feedback path through the other blocks in the loop to the input of the block. In the simple one-block example shown on the previous page, F(z) = z - (u - z). In the vector loop example shown above, the equations are
Algebraic loops arise when a model includes an algebraic constraint F(z) = 0. This constraint might arise as a consequence of the physical interconnectivity of the system you are modeling, or it might arise because you are specifically trying to model a differential/algebraic system (DAE).
When a model contains an algebraic loop, Simulink calls a loop solving routine at each time step. The loop solver performs iterations to determine the solution to the problem (if it can). As a result, models with algebraic loops run slower than models without them.
To solve F(z) = 0, the Simulink loop solver uses Newton's method with weak line search and rank-one updates to a Jacobian matrix of partial derivatives. Although the method is robust, it is possible to create loops for which the loop solver will not converge without a good initial guess for the algebraic states z. You can specify an initial guess for a line in an algebraic loop by placing an IC block (which is normally used to specify an initial condition for a signal) on that line. As shown above, another way to specify an initial guess for a line in an algebraic loop is to use an Algebraic Constraint block.
Whenever possible, use an IC block or an Algebraic Constraint block to specify an initial guess for the algebraic state variables in a loop.
Highlighting Algebraic Loops
You can cause Simulink to highlight algebraic loops when you update, simulate, or debug a model. Use the ashow command to highlight algebraic loops when debugging a model.
To cause Simulink to highlight algebraic loops that it detects when updating or simulating a model, set the Algebraic loop diagnostic on the Diagnostics pane of the Configuration Parameters dialog box to Error (see The Configuration Parameters Dialog Box for more information). This causes Simulink to display an error dialog (the Diagnostics Viewer) and recolor portions of the diagram that represent the algebraic loops that it detects. Simulink uses red to color the blocks and lines that constitute the loops. Closing the error dialog restores the diagram to its original colors.
For example, the following figure shows the block diagram of the hydcyl demo model in its original colors.
The following figure shows the diagram after updating when the Algebraic loop diagnostic is set to Error.
In this example, Simulink has colored the algebraic loop red, making it stand out from the rest of the diagram.
Eliminating Algebraic Loops
Simulink can eliminate some algebraic loops that include any of the following types of blocks:
To enable automatic algebraic loop elimination for a loop involving a particular instance of an Atomic Subsystem or Enabled Subsystem block, select the Minimize algebraic loop occurrences parameter on the block's parameters dialog box. To enable algebraic loop elimination for a loop involving a Model block, check the Minimize algebraic loop occurrences parameter on the Model Referencing configuration parameters dialog (see Model Referencing Pane) of the model referenced by the Model block. If a loop includes more than one instance of these blocks, you should enable algebraic loop elimination for all of them, including nested blocks.
The Simulink Minimize algebraic loop solver diagnostic allows you to specify the action Simulink should take, for example, display a warning message, if it is unable to eliminate an algebraic loop involving a block for which algebraic loop elimination is enabled. See The Diagnostics Pane for more information.
Algebraic loop minimization is off by default because it is incompatible with conditional input branch optimization in Simulink (see The Optimization Pane) and with single output/update function optimization in Real-Time Workshop®. If you need these optimizations for an atomic or enabled subsystem or referenced model involved in an algebraic loop, you must eliminate the algebraic loop yourself.
As an example of the ability of Simulink to eliminate algebraic loops, consider the following model.
Simulating this model with the solver's Algebraic Loop diagnostic set to error (see The Diagnostics Pane) reveals that this model contains an algebraic loop involving its atomic subsystem.
Checking the atomic subsystem's Minimize algebraic loop occurrences parameter causes Simulink to eliminate the algebraic loop from the compiled version of the model.
As a result, the model now simulates without error.
Note that Simulink is able to eliminate the algebraic loop involving this model's atomic subsystem because the atomic subsystem contains a block with a port that does not have direct feed through, i.e., the Integrator block.
If you remove the Integrator block from the atomic subsystem, Simulink is unable to eliminate the algebraic loop. Hence, attempting to simulate the model results in an error.
| Zero-Crossing Detection | Modeling and Simulating Discrete Systems | ![]() |
© 1994-2005 The MathWorks, Inc.