# How did I get an algebraic loop error, when the diagnostic was set to warning? 20

Posted by **Seth Popinchalk**,

I once faced a problem where Simulink reported, “Cannot solve algebraic loops...”

Algebraic
loops can be solved by Simulink, but often slow down the simulation. For
this reason, we have the algebraic
loop diagnostic that can be set to *Error*, *Warning* or *None*.
What was peculiar about this model was that the algebraic loop diagnostic was
set to *Warning*, yet the model would report an error, and not simulate! To
makes things more difficult, all I had to work with was a screen shot of the
model, not the model it self.

**What are algebraic loops?**

Algebraic loops exist when a variable shows up on both sides of the equation. For example,

Algebraic loops generally occur in Simulink when there are un-modeled delays, for example, sensors that just feed through a signal from input to output.

**Read the error message**

The first step in debugging a problem like this is to read and understand the error message. Algebraic loop errors occur for different reasons. To understand the cause of the error I had to understand the message.

```
Cannot solve algebraic loop involving 'model/.../Sensor System' because it consists of blocks that
cannot be assigned algebraic variables, i.e., blocks with discrete-valued
outputs, blocks with non-double or complex outputs, Stateflow blocks, or nonvirtual
subsystems.
```

Picking apart this message, we learn that Simulink cannot solve algebraic loops unless it can assign an algebraic variable. The blocks that cannot be assigned algebraic variables are listed in the error.

- Discrete-valued outputs, like logic blocks [0 or 1]
- Blocks that have non-double outputs
- Blocks that output complex values (3+2i)
- Stateflow blocks
- Nonvirtual subsystems

**Figure out how the message relates to the model**

Next, I looked at the screen shot and see if I could find those blocks. This is roughly similar to the screen shot sent to technical support:

The first thing I noticed was that all the signals are of type uint8. Those are non-double types, so that could be part of the problem. Another thing I noticed was the heavy lines on the subsystems. That means these are atomic subsystems. Atomic subsystems are nonvirtual, and those are on the list of blocks that could cause this problem.

**Possible Resolution**

Because these were atomic subsystems, I suggested enabling the Minimize algebraic loop occurrences optimization.

The subsystem can be thought of a function of its inputs and states

In some systems, the output signal calculations do not directly rely on the inputs. If this is true, you can separate the system into two equations.

This option is also available for model reference blocks through the configuration parameters on the Model Referencing entry.

**How did this happen?**

This model was the result of integrating components from many different teams. The original model contained virtual subsystems, and the new components used to upgrade the model were atomic subsystems. Switching back to virtual might have resolved this error also.

The algebraic loop diagnostic did not control this error because this was part of the check for the minimum solvability requirements for this type of problem.

**Now it’s your turn**

There is additional information in the technical support solutions on understanding algebraic loops, and how does Simulink solve them. Have you encountered this error? How did you work around it? Leave a comment here about your experience.

## 20 CommentsOldest to Newest

I have that same problem and i have not find out out to solve it. I would hope that simulink would just do come kind of finite differences to work it out but no. I have even tried to make a discretization of the model without success.

Good luck.

Seth,

Dealing with algebraic loops is by far the most frustrating problem in simulink. It seems to me that there are two types of algebraic loops. The first is the mathematical type, e.g. y = 2-y as in your example. These are usually easy to understand and deal with if we don’t want the solver to solve them. The other type, however, are apparent algebraic loops found by the solver because of how it treats atomic subsystems, embedded matlab function blocks, model references, etc. This type is not really discussed all that well in the documentation. Most, but not all, of the problems I’ve had involve models with these types of blocks, and in these cases the “minimize algebraic loop occurences” hasn’t worked for me. I’ve had problems where the red highlighting of the algebraic loop stops in midstream and I can’t determine where the problem is; I had a case where an algebraic loop was reported when a gain block was set to something other than 1; and I just had a problem where an algebraic loop error was reported in a very simple model, without any atomic subsystems or embedded function blocks. In this particular case, I had an algebraic loop error when I passed a width-3 signal output from a virtual subsystem through a demux, but didn’t have an error when I passed out the 3-signals individually. In this same model, I also had an algebraic loop error when I put a gain block (with gain=1) onto a line, but didn’t have any problems w/o the gain block. This kind of stuff is very frustrating. A similar problem is with enabled subsystems where the enabling signal is dependent of the states of the subsystem. For example, consider two balls moving on a wire and we want to disable them when they collide. I’ve yet to figure out how to do this w/o putting an extra memory block into the system, outside of the enabled subsystems that govern the motion of the balls, which allows the balls to continue to move for one time step after they’ve collided. Thanks for the forum for this rant. Keep up the blog; I’ve been finding it very interesting.

@Pedro – Simulink does uses Newton’s method to perform a search for the solution. If you have trouble getting it to converge, note the algebraic variable, and place an IC block (initial condition) at the output port of that block. You can then provide an initial guess for the solver, and may improve your results. The result from the last solution is used as the initial guess of the next step, and should allow the model to move forward in time. Good luck!

@Paul J – Thanks for the rant. You are right… the second type of algebraic loop are the ones that cause me the most frustration too. In some cases I find conflicting specifications are to blame. If a subsystem is marked as Atomic, Simulink must be able to compute all of the outputs at the same time. If there are dependencies on inputs, and those have a direct feed-through path back to the outputs, there is a sorting problem to be handled. Thanks for contributing to the discussion!

Hi Seth!

I’m very confused. in the Simulink document

http://www.mathworks.com/access/helpdesk/help/toolbox/simulink/ug/f7-8243.html#f7-19688

solving simple algebraic loops with The Algebraic Constraint block and find z1 = 0, z2 = 1.

i solve this example without this block and find z1 = -1, z2 = 1;

Which answer is correct?

unfortunately mathworks can not be accessed in iran. I dont know why mathworks guys do that but I can’t solve my simulink related problems.

hi javad

if you have any problem please write me with my Email.

I hope that i can help you with your mat/simu problem.

Hi Suth,

I try to simulate a load tap-changing transformer in simulink but its control part has to be impelented in stateflow, I did it but simulating the system I get this error message,

Trouble solving algebraic loop containing ‘handy_ltc23/Three-Phase OLTC Regulating Transformer/Chart’ at time 5.033333333333321. Stopping simulation. There may be a singularity in the solution. If the model is correct, try reducing the step size (either by reducing the fixed step size or by tightening the error tolerances)

The point is that I have to use stateflow for its control part and I can’t refuse that, inside the stateflow chart I just want to update a variable one unit every 4 sec, can Not I do it?

I’ll be very grateful if you give me a key

regards

Mohamamd

how D flip flop can be designed in simulink… we r facing problem while simuliting using NAND gate as it shows algebric loop error..

I am not sure of your implementation of the D flip flop, and I am not sure how it is causing an algebraic loop error. There is flip flop library in the Simulink Extras library. You may be able to try using that.

hello, I have a program which in the simulink matlab 2007 run well , but when I want run it in the simulink matlab 2008 the simulations stops with the error: Trouble solving algebraic loop containing ‘test1_L/Fuel cell/transfortation’ at time 0. Stopping simulation. There may be a singularity in the solution. If the model is correct, try reducing the step size (either by reducing the fixed step size or by tightening the error tolerances). how I can solve this problem, thanks

The problem in my point of view was solved easily by putting “unit delay” inside the loop

Simulink library browser–>Discrete–>unit delay

After doing that, no problem message appeared

Try this, may be it can help

All the best

Mahmoud Moussa

I’ve just solved an algebraic loop by changing tehe name of a block from “Antialiasing” to “Subsystem”. Answer to that? It’s just curiosity. I think i’ll neveer understand this program…

From the discussion, I understand that to solve the 1st type of algebraic loop it´s enough to decouple the input/output direct feedthrough of every direct feedthrough block by adding a state variable between the input and the output. And, as far as I understand, this can be easily done with an IC block. Inserting this block is also equivalent to equip the direct feedthrough block with a state x that evolves like x(k+1)=x(k), and to consider a switch of the output so that we have y=x at t=t0 and y=f(u) for t>t0, where x,u and y represent the state, the input and the output respectively). This is equivalent to force the output at the value of the IC block at t=t0, and the algebraic loop would be solved, isn’t it? But at this point, I would argue that it is possible to use any block for which you can set a initial condition/output (e.g. rate transition blocks for example), isn’t it?

So, to solve an algebraic loop is enough to break it in just one point, right?

While, for the 2nd type of algebraic loop that Paul J. mentioned, one has just to pay attention to the specification and, of course, he must be a bit lucky, isn’t it?

@Asier – most likely, changing the name of the block changed the starting point for the sorting algorithm, and it came up with a different sorted order. This suggests that your system is under-specified (your system is inheriting a lot of information from defaults), or has loops that can be unwound in many different ways by the sorting algorithm.

@Ubaldo – Th IC block doesn’t add state. It does specify an initial guess for the value of a signal, and in the case of some algebraic loops, this can help in solving them. If I am reading what you wrote correctly, the process of solving the loop as you describe it, is similar to what is done at each step in the model. The algebraic loops represents an equality constraint on the system, and the algebraic variable has to solve for the equation: f(x) = x. The algebraic variable is where Simulink chose to “break” the loop, and set up the constraint.

Breaking the loop with a delay is not always the right answer. A constraint like this implies a constraint in the dynamic system you have described. It can be a result of a real constraint in the physical system, or could result from the way it is expressed in the model.

For the type of algebraic loop “caused” by atomic units in the model (atomic subsystems, model reference, MATLAB Function, etc.) it is a case of conflicting requirements. The connections in the model specify a data dependency, and the types of units used to express the algorithm specify an input/output relationship with direct feed-through. When the execution order of these algorithmic units loops back on itself, the loop is defined. I don’t think of it as luck, so much as a conflict of specifications.

Regardless, it is very frustrating when you can see that the system has some kind of break point/state, but Simulink can’t.

A more complete explanation of breaking fake algebraic loops can be found at http://www.mathworks.com/support/solutions/en/data/1-8XGT73/index.html?solution=1-8XGT73

hii

i found a very simple way to resolve this problem:”Simulink cannot solve the algebraic loop…”

*************************************

It is only enough to add a “unit delay” block in the feedback branch !

*************************************1/z***********

regards

abolhasan

Me too Like Paul J. I had a case where an algebraic loop was reported when a gain block was set to something other than 1; and I just had a problem where an algebraic loop error was reported in a very simple model where there is a transfer between two states (stateflow).

abolhasan it tells me that i have a problem with 4th derivative input in state space .

how can i solve it ?

The abolhasan zare’s solution is corret!! I solve it using the memory block (simulink) to do the loops!! =D Thanks!!

I am trying to create a fault(sag) in a power system. It is a dynamic condition.When I create it lets say 2sec I dont get a algebric loop error…But when I create it on 0.5sec I get this error.Can anyone help me on this?