Main Content

Remove Algebraic Loops

Use these techniques to remove algebraic loops in a model.

Remove Algebraic Loops by Introducing Delay

Feedback loops can create algebraic loops in a model. You can remove these algebraic loops by introducing a delay in the loop using the Unit Delay block.

Open the model AlgebraicLoopUnitDelay. The model contains three atomic subsystems, A, B, and C. The output from subsystem A is an input for subsystem B, and the output from subsystem B is an input for subsystem C. The output from subsystem C feeds back into subsystem A and subsystem B, creating two feedback loops.

mdl = "AlgebraicLoop";
open_system(mdl)

The model AlgebraicLoop.

To determine whether the model contains algebraic loops, use the Simulink.BlockDiagram.getAlgebraicLoops function. The function opens the Algebraic Loops viewer, which shows that the model contains one real algebraic loop.

Simulink.BlockDiagram.getAlgebraicLoops(mdl);

Figure Algebraic Loops in 'AlgebraicLoop' contains an object of type uigridlayout.

The Simulink.BlockDiagram.getAlgebraicLoops function highlights the path of the algebraic loop in the model in yellow and highlights blocks with algebraic variables in red. In the model AlgebraicLoop, the Gain blocks in the feedback loops are both highlighted as algebraic variables.

The model AlgebraicLoop with the algebraic loop and algebraic loop variables highlighted.

In general, to remove algebraic loops from your model, add a delay before or after each algebraic variable. To see this solution, open the model AlgebraicLoopTwoUnitDelays which is the same as the model AlgebraicLoop but with a Unit Delay block added after the Gain block in each feedback loop.

mdl2 = "AlgebraicLoopTwoUnitDelays";
open_system(mdl2)

The model AlgebraicLoopTwoUnitDelays

To confirm that the Unit Delay blocks removed the algebraic loops, use the Simulink.BlockDiagram.getAlgebraicLoops function.

Simulink.BlockDiagram.getAlgebraicLoops(mdl2);
No algebraic loops were found.

With this placement of Unit Delay blocks, subsystem A and subsystem B use the output from subsystem C from the previous time step.

In this model, you can also remove the algebraic loop by adding a single Unit Delay block between subsystem B and subsystem C. This signal path is part of both feedback loops, so the delay affects the path of both algebraic variables. To see this solution, open the model AlgebraicLoopOneUnitDelay.

mdl3 = "AlgebraicLoopOneUnitDelay";
open_system(mdl3)

The model AlgebraicLoopOneUnitDelay

To confirm that the model does not contain an algebraic loop, use the Simulink.BlockDiagram.getAlgebraicLoops function.

Simulink.BlockDiagram.getAlgebraicLoops(mdl3);
No algebraic loops were found.

With this placement of the Unit Delay block, subsystem C uses the value from the previous time step for the output from subsystem B to produce the feedback value for the current time step.

Solve Algebraic Loops Manually

If the solver is unable to solve the algebraic loop, the software issues an error. Use one of these techniques to solve the loop manually:

  • Restructure the underlying DAEs using techniques such as differentiation or change of coordinates. These techniques put the DAEs in a form that is easier for the algebraic loop solver to solve.

  • Convert the DAEs to ODEs, which eliminates any algebraic loops.

  • Only for a legitimate algebraic loop, specify an initial guess for the algebraic states in the loop to help the algebraic loop solver converge. You can specify the initial guess one of these ways:

    • Place an IC block in the algebraic loop.

    • Specify an initial guess for a signal in an algebraic loop using an Algebraic Constraint block.

How the Software Eliminates Artificial Algebraic Loops

When you enable the Minimize algebraic loop occurrences parameter, the software tries to eliminate artificial algebraic loops. In this example, the model contains an atomic subsystem that causes an artificial algebraic loop.

The contents of the atomic subsystem do not have direct feedthrough, but the software identifies the atomic subsystem as having direct feedthrough.

If the Algebraic loop diagnostic is set to error, simulating the model results in an error because the model contains an artificial algebraic loop involving its atomic subsystem.

To eliminate this algebraic loop,

  1. Create the model from the preceding graphics, with the atomic subsystem that causes the artificial algebraic loop.

  2. In the Diagnostics pane of Model Configuration Parameters, set the Algebraic loop parameter to warning or none.

  3. To display the block execution order for this model and the atomic subsystem, in the Debug tab, select Information Overlays > Execution Order.

    Reviewing the execution order can help you understand how to eliminate the artificial algebraic loop.

    All the blocks in the subsystem execute at the same level: 1. (0 is the lowest level, indicating the first blocks to execute.)

    Note

    For more information about block execution order, see Control and Display Execution Order.

  4. In the top-level model’s Subsystem Parameters dialog box, select Minimize algebraic loop occurrences. This parameter directs the software to try to eliminate the algebraic loop that contains the atomic subsystem when you simulate the model.

  5. Save the changes.

  6. In the Modeling tab, click Update Model to recalculate the execution order.

    Now there are two levels of execution order inside the subsystem: 1 and 2.

To eliminate the artificial algebraic loop, the software tries to adjust the execution order so that the subsystem or model reference does not have direct feedthrough.

When you simulate a model, all blocks execute methods in this order:

  1. mdlOutputs

  2. mdlDerivatives

  3. mdlUpdate

In the original version of this model, the execution of the mdlOutputs method starts with the Plant block because the Plant block does not have direct feedthrough. The execution finishes with the Controller block.

For more information about these methods, see Simulink Models.

If you enable the Minimize algebraic loop occurrences parameter for the atomic subsystem, the software divides the subsystem into two atomic units.

These conditions are true:

  • Atomic unit 2 does not have direct feedthrough.

  • Atomic unit 1 has only a mdlOutputs method.

Only the mdlDerivatives or mdlUpdate methods of Atomic unit 2 need the output of Atomic unit 1. Simulink® can execute what normally would have been executed during the mdlOutput method of Atomic unit 1 in the mdlDerivatives methods of Atomic unit 2.

The new execution order for the model is:

  1. mdlOutputs method of model

    1. mdlOutputs method of Atomic unit 2

    2. mdlOutputs methods of other blocks

  2. mdlDerivatives method of model

    1. mdlOutputs method of Atomic unit 1

    2. mdlDerivatives method of Atomic unit 2

    3. mdlDerivatives method of other blocks

For the Minimize algebraic loop occurrences technique to be successful, the subsystem or referenced model must have a non-direct-feedthrough block connected directly to an Inport and must not contain any signals designated as test points. The software can then set the DirectFeedthrough property of the block Inport to false to indicate that the input port does not have direct feedthrough.

Eliminate Artificial Algebraic Loops Caused by Atomic Subsystems

If an atomic subsystem causes an artificial algebraic loop, convert the atomic subsystem to a virtual subsystem. When the subsystem is atomic and you simulate the model, Simulink invokes the algebraic loop solver. The solver terminates after one iteration. The algebraic loop is automatically solved because there is no algebraic constant. After you make the subsystem virtual, Simulink does not invoke the algebraic loop solver during simulation.

To convert an atomic subsystem to a virtual subsystem:

  1. Open the model that contains the atomic subsystem.

  2. Right-click the atomic subsystem and select Subsystem Parameters.

  3. Clear the Treat as atomic unit parameter.

  4. Save the changes.

If you replace the atomic subsystem with a virtual subsystem and the simulation still fails with an algebraic loop error, examine the model for one of these:

  • An algebraic constraint

  • An artificial algebraic loop that was not caused by this atomic subsystem

Bundled Signals That Create Artificial Algebraic Loops

Some models bundle signals together. This bundling can cause Simulink to detect an algebraic loop, even when an algebraic constraint does not exist. If you redirect one or more signals, you may be able to remove the artificial algebraic loop.

In this example, a linearized model simulates the dynamics of a two-tank system fed by a single pump. In this model:

  • Output q1 is the rate of the fluid flow into the tank from the pump.

  • Output h2 is the height of the fluid in the second tank.

  • The State-Space block defines the dynamic response of the tank system to the pump operation:

  • The output from the State-Space block is a vector that contains q1 and h2.

If you simulate this model with the Algebraic loop parameter set to warn or error, Simulink identifies the algebraic loop.

To eliminate this algebraic loop:

  1. Change the C and D matrices as follows:

  2. Pass q1 directly to the Scope instead of through the State-Space block.

    Now, the input (q1) does not pass directly to the output (the D matrix is 0), so the State-Space block no longer has direct feedthrough. The feedback signal has only one element now, so the Selector block is no longer necessary, as you can see in the following model.

Model and Block Parameters to Diagnose and Eliminate Artificial Algebraic Loops

There are two parameters to consider when you think that your model has an artificial algebraic loop:

  • Minimize algebraic loop occurrences parameter — Specify that Simulink try to eliminate any artificial algebraic loops for:

    • Atomic subsystems — In the Subsystem Parameters dialog box, select Minimize algebraic loop occurrences.

    • Model blocks — For the referenced model, in the Model Referencing pane of Configuration Parameters, select Minimize algebraic loop occurrences.

  • Minimize algebraic loop parameter — Specifies what diagnostic action Simulink takes if the Minimize algebraic loop occurrences parameter has no effect.

    The Minimize algebraic loop parameter is in the Diagnostics pane of Configuration Parameters. The diagnostic actions for this parameter are:

    Setting Simulation Response
    none

    Simulink takes no action.

    warning

    Simulink displays a warning that the Minimize algebraic loop occurrences parameter has no effect.

    error

    Simulink terminates the simulation and displays an error that the Minimize algebraic loop occurrences parameter has no effect.

Block Reduction and Artificial Algebraic Loops

When you enable the Block reduction optimization in Model Configuration Parameters, Simulink collapses certain groups of blocks into a single, more efficient block, or removes them entirely. Enabling block reduction results in faster execution during model simulation and in generating code.

Enabling block reduction can also help Simulink solve artificial algebraic loops.

Consider the following model.

Initially, block reduction is turned off. When you simulate this model, the Atomic Unit subsystem and Gain and Compare to Constant blocks are part of an algebraic loop that Simulink cannot solve.

If you enable block reduction and execution order, and simulate the model again, Simulink does not display the execution order for blocks that have been reduced. You can now quickly see which blocks have been reduced.

The Compare to Constant and Gain blocks have been eliminated from the model, so they no longer generate an algebraic loop error. The Atomic Unit subsystem generates a warning:

Warning: If the inport 'ex_aloop_block_reduction_errwarn/
Atomic Unit/In1' of subsystem 'ex_aloop_block_reduction_errwarn/
Atomic Unit' involves direct feedback, then an algebraic loop
exists, which Simulink cannot remove. Consider clearing the 
'Minimize algebraic loop occurrences' parameter to avoid this 
warning.

Tip

Use Bus Selector blocks to pass only the required signals into atomic subsystems.

See Also

Related Topics