{"id":21,"date":"2008-07-03T21:30:00","date_gmt":"2008-07-04T02:30:00","guid":{"rendered":"https:\/\/blogs.mathworks.com\/seth\/2008\/07\/03\/how-did-i-get-an-algebraic-loop-error-when-the-diagnostic-was-set-to-warning\/"},"modified":"2018-01-18T16:15:05","modified_gmt":"2018-01-18T21:15:05","slug":"how-did-i-get-an-algebraic-loop-error-when-the-diagnostic-was-set-to-warning","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2008\/07\/03\/how-did-i-get-an-algebraic-loop-error-when-the-diagnostic-was-set-to-warning\/","title":{"rendered":"How did I get an algebraic loop error, when the diagnostic was set to warning?"},"content":{"rendered":"<p>I once faced a problem where Simulink reported, \u201cCannot solve algebraic loops...\u201d<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q3\/algLoopErrorScreenShot.png\" alt=\"Algebraic loop error, Cannot solve algebraic loops\"><\/p>\r\n\r\n<p><a href=\"#f7-19688\">Algebraic\r\nloops<\/a> can be solved by Simulink, but often slow down the simulation.  For\r\nthis reason, we have the <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/gui\/diagnostics-pane-solver.html#bq9_mwg-1\">algebraic\r\nloop diagnostic<\/a> that can be set to <em>Error<\/em>, <em>Warning<\/em> or <em>None<\/em>. \r\nWhat was peculiar about this model was that the algebraic loop diagnostic was\r\nset to <em>Warning<\/em>, yet the model would report an error, and not simulate!  To\r\nmakes things more difficult, all I had to work with was a screen shot of the\r\nmodel, not the model it self. <\/p>\r\n\r\n<p><strong>What are algebraic loops?<\/strong><\/p>\r\n\r\n<p>Algebraic loops exist when a variable shows up on both sides of the equation. For\r\nexample,<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q3\/eqYminus2.png\" alt=\"y=y-2\"><\/p>\r\n\r\n<p>Algebraic loops generally occur in Simulink when there are un-modeled delays, for\r\nexample, sensors that just feed through a signal from input to output.<\/p>\r\n\r\n<p><strong>Read the error message<\/strong><\/p>\r\n\r\n<p>The first step in debugging a problem like this is to read and understand the error\r\nmessage.  Algebraic loop errors occur for different reasons.  To understand the\r\ncause of the error I had to understand the message. <\/p>\r\n\r\n<p><code>Cannot solve algebraic loop involving 'model\/...\/Sensor System' because it consists of blocks that\r\n        cannot be assigned algebraic variables, i.e., blocks with discrete-valued\r\n        outputs, blocks with non-double or complex outputs, Stateflow blocks, or nonvirtual\r\n    subsystems.<\/code>\r\n<\/p>\r\n\r\n<p>Picking apart this message, we learn that Simulink cannot solve algebraic loops unless\r\nit can assign an algebraic variable.  The blocks that cannot be assigned\r\nalgebraic variables are listed in the error.<\/p>\r\n<p>\r\n    <ul>\r\n        <li>Discrete-valued outputs, like logic blocks [0 or 1]<\/li>\r\n        <li>Blocks that have non-double outputs<\/li>\r\n        <li>Blocks that output complex values (3+2i)<\/li>\r\n        <li>Stateflow blocks<\/li>\r\n        <li>Nonvirtual subsystems<\/li>\r\n    <\/ul>\r\n<\/p>\r\n<p><strong>Figure out how the message relates to the model<\/strong><\/p>\r\n\r\n<p>Next, I looked at the screen shot and see if I could find those blocks. This is roughly\r\nsimilar to the screen shot sent to technical support:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q3\/algLoopModelScreenShot.png\" alt=\"Model screen shot with algebraic loop error\"><\/p>\r\n\r\n<p>The first thing I noticed was that all the signals are of type uint8.  Those are\r\nnon-double types, so that could be part of the problem.  Another thing I\r\nnoticed was the heavy lines on the subsystems.  That means these are atomic\r\nsubsystems.  Atomic subsystems are nonvirtual, and those are on the list of\r\nblocks that could cause this problem.<\/p>\r\n\r\n<p><strong>Possible Resolution<\/strong><\/p>\r\n\r\n<p>Because\r\nthese were atomic subsystems, I suggested enabling the <a\r\nhref=\"#f7-24010\">Minimize\r\nalgebraic loop occurrences<\/a> optimization.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q3\/minimizeAlgLoopCheckbox.png\" alt=\"Minimize algebraic loop occurrences\"><\/p>\r\n\r\n<p>The\r\nsubsystem can be thought of a function of its inputs and states<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q3\/eqFofInputsAndStates.png\" alt=\"outputs=f(inputs,states)\"><\/p>\r\n\r\n<p>In some systems, the output signal calculations do not directly rely on the inputs. \r\nIf this is true, you can separate the system into two equations.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q3\/eqStateGofInputs.png\" alt=\"states=g(inputs)\"><\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q3\/eqOutHofStates.png\" alt=\"outputs=h(states)\"><\/p>\r\n\r\n<p>This option is also available for model\r\nreference blocks through the configuration parameters on the Model\r\nReferencing entry. <\/p>\r\n\r\n<p><strong>How did this happen?<\/strong><\/p>\r\n\r\n<p>This model was the result of integrating components from many different teams.  The\r\noriginal model contained virtual subsystems, and the new components used to\r\nupgrade the model were atomic subsystems.  Switching back to virtual might have\r\nresolved this error also.<\/p>\r\n\r\n<p>The algebraic loop diagnostic did not control this error because this was part of\r\nthe check for the minimum solvability requirements for this type of problem.<\/p>\r\n\r\n<p><strong>Now it\u2019s your turn<\/strong><\/p>\r\n\r\n<p>There is additional information in the technical support solutions on <a\r\nhref=\"https:\/\/www.mathworks.com\/support\/solutions\/data\/1-16V6S.html?solution=1-16V6S\">\r\nunderstanding algebraic loops<\/a>, and <a\r\nhref=\"https:\/\/www.mathworks.com\/support\/solutions\/data\/1-2S1NR3.html?solution=1-2S1NR3\">how\r\ndoes Simulink solve them<\/a>. Have you encountered this error? How did you work\r\naround it? Leave a <a href=\"https:\/\/blogs.mathworks.com\/seth\/?p=21&amp;#comment\">comment\r\nhere<\/a> about your experience. <\/p>","protected":false},"excerpt":{"rendered":"<p>I once faced a problem where Simulink reported, \u201cCannot solve algebraic loops...\u201d\r\n\r\n\r\n\r\nAlgebraic\r\nloops can be solved by Simulink, but often slow down the simulation.  For\r\nthis reason, we have the... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2008\/07\/03\/how-did-i-get-an-algebraic-loop-error-when-the-diagnostic-was-set-to-warning\/\">read more >><\/a><\/p>","protected":false},"author":40,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[43,30],"tags":[44,448],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/21"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/users\/40"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/comments?post=21"}],"version-history":[{"count":4,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/21\/revisions"}],"predecessor-version":[{"id":7036,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/21\/revisions\/7036"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=21"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=21"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=21"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}