Use these techniques to remove algebraic loops in a model.

Algebraic loops can occur in large models when atomic subsystems create feedback loops.

In the generic model here, there are two algebraic loops that involve subsystems.

BlackBox_A —> BlackBox_B —> BlackBox_C —> BlackBox_A

BlackBox_B —> BlackBox_C —> BlackBox_B

When you update this model, Simulink^{®} detects the loop BlackBox_A —> BlackBox_B —> BlackBox_C —>
BlackBox_A.

Since you do not know the contents of these subsystems, break the loops by adding a Unit Delay block outside the subsystems. There are three ways to use the Unit Delay block to break these loops:

Add a Unit Delay between

`BlackBox_A`

and`BlackBox_C`

.Add a Unit Delay between

`BlackBox_B`

and`BlackBox_C`

.Add Unit Delay blocks to both algebraic loops.

**Add a unit delay between BlackBox_A and BlackBox_C**

If you add a unit delay on the feedback signal between the subsystems BlackBox_A and BlackBox_C, you introduce the minimum number of unit delays (1) to the system. By introducing the delay before BlackBox_A, BlackBox_B and BlackBox_C use data from the current time step.

**Add a unit delay between BlackBox_B and BlackBox_C**

If you add a unit delay between the subsystems BlackBox_B and BlackBox_C, you break the algebraic loop between BlackBox_B and BlackBox_C. In addition, you break the loop between BlackBox_A and BlackBox_C, because that signal completes the algebraic loop. By inserting the Unit Delay block before BlackBox_C, BlackBox_C now works with data from the previous time step only.

**Add unit delays to both algebraic loops**

In the example here, you insert Unit Delay blocks to break both algebraic loops. In this model, BlackBox_A and BlackBox_B use data from the previous time step. BlackBox_C uses data from the current time step.

If Simulink cannot solve the algebraic loop, the software reports 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.

Create Initial Guesses Using the IC and Algebraic Constraint Blocks

Your model might contain loops for which the loop solver cannot converge without a good, initial guess for the algebraic states. You can specify an initial guess for the algebraic state variables, but use this technique only when you think the loop is legitimate.

There are two ways to specify an initial guess:

Place an IC block in the algebraic loop.

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

When you enable **Minimize algebraic loop occurrences**, Simulink 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 are not direct feedthrough, but Simulink identifies the atomic subsystem as 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,

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

In the

**Diagnostics**pane of Model Configuration Parameters, set the**Algebraic loop**parameter to`warning`

or`none`

.In the

**Data Import/Export**pane, make sure the**Signal logging**parameter is disabled. If signal logging is enabled, Simulink cannot eliminate artificial algebraic loops.To display the sorted order for this model and the atomic subsystem, select

**Display**>**Blocks**>**Sorted Execution Order**.Reviewing the sorted 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 sorted order, see Control and Display the Sorted Order.

In the top-level model’s

**Subsystem Parameters**dialog box, select**Minimize algebraic loop occurrences**. This parameter directs Simulink to try to eliminate the algebraic loop that contains the atomic subsystem, when it simulates the model. Save the changes.Click

**Simulation**>**Update Diagram**to recalculate the sorted order.Now there are two levels of sorted order inside the subsystem: 1 and 2.

To eliminate the artificial algebraic loop, Simulink tries to make the input of the subsystem or referenced model non-direct feedthrough.

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

`mdlOutputs`

`mdlDerivatives`

`mdlUpdate`

In the original version of this model, the execution of the `mdlOutputs`

method starts with
the Plant block because the Plant block is non-direct feedthrough. The execution
finishes with the Controller block.

For more information about these methods, see Block Methods.

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

These conditions are true:

Atomic unit 2 is not 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:

`mdlOutputs`

method of model`mdlOutputs`

method of Atomic unit 2`mdlOutputs`

methods of other blocks

`mdlDerivatives`

method of model`mdlOutputs`

method of Atomic unit 1`mdlDerivatives`

method of Atomic unit 2`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. Simulink can then set the `DirectFeedthrough`

property of the
block Inport to `false`

to indicate that the input port does not have
direct feedthrough.

If an atomic subsystem causes an artificial algebraic loop, convert the atomic subsystem to a virtual subsystem. This change has no effect on the behavior of the model. 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:

Open the model that contains the atomic subsystem.

Right-click the atomic subsystem and select

**Subsystem Parameters**.Clear the

**Treat as atomic unit**parameter.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

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:

Change the

`C`

and`D`

matrices as follows: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.

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.

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 example 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 sorted order, and resimulate the model, Simulink does not display the sorted 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.

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

Setting the **Minimize algebraic loop occurrences** parameter does
not always work. Simulink cannot change the `DirectFeedthrough`

property of an
Inport for an atomic subsystem if the Inport is connected to an Outport only through
direct-feedthrough blocks.

In this model, the subsystem Plant+Controller causes an algebraic loop, but it has an extra Gain block and an extra output.

Simulink cannot move the `mdlOutputs`

method of the Controller
block to the `mdlDerivative`

method of an Atomic unit 1 because the
output of the atomic subsystem depends on the output of the Controller block. You cannot
make the subsystem non-direct-feedthrough.

You can modify this model to eliminate the artificial algebraic loop by redefining the atomic subsystem by adding additional Inport and Gain blocks, as you can see in the model here. Doing so makes In1 non-direct-feedthrough and In2 direct feedthrough, thus breaking the algebraic loop.