Seth on Simulink

July 3rd, 2008

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

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

Algebraic loop error, 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,

y=y-2

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:

Model screen shot with algebraic loop error

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.

Minimize algebraic loop occurrences

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

outputs=f(inputs,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.

states=g(inputs)

outputs=h(states)

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.

5 Responses to “How did I get an algebraic loop error, when the diagnostic was set to warning?”

  1. Pedro replied on :

    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.

  2. Paul J. replied on :

    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.

  3. Seth replied on :

    @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!

  4. Amin replied on :

    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?

  5. javadaein replied on :

    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.

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


Seth Popinchalk is an Application Engineer for The MathWorks. He writes here about Simulink and other MathWorks tools used in Model-Based Design.
  • Jarrod Rivituso: “I think of signal lines as the variables in a program.” I say the exact same thing...
  • actuator: This is very impressive. Thanks for sharing
  • Guy: @Pat and Paul, I agree that the slblocks documentation is unexisting. From what I remember, when subsystems are...
  • Jeff: Hi Seth, Thanks for the feedback. I actually decided to go with the second option since it made more sense for...
  • javadaein: unfortunately mathworks can not be accessed in iran. I dont know why mathworks guys do that but I...
  • Pat: Dear Paul, I am facing the same problem, were you able to solve it!? Pat
  • Seth: @Syed Murtuza Hussain - Happy to provide some inspiration. @Rienzi Mosqueda - The example models I would share...
  • Seth: @Jeff - Regarding your first question about handling a bus with a Level 2 M-file S-function: you are correct,...
  • M.tamilselvan: this piece of snippet is short and crisp,need to post more mental model strutures in depth possible...
  • rienzi mosqueda: hi.!im currently into the studying a robotic arm and possibly simulating one by controlling it by a...

These postings are the author's and don't necessarily represent the opinions of The MathWorks.