{"id":7774,"date":"2018-07-09T16:03:37","date_gmt":"2018-07-09T21:03:37","guid":{"rendered":"https:\/\/blogs.mathworks.com\/simulink\/?p=7774"},"modified":"2018-07-09T16:03:37","modified_gmt":"2018-07-09T21:03:37","slug":"visualizing-fixed-point-algebraic-loops","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2018\/07\/09\/visualizing-fixed-point-algebraic-loops\/","title":{"rendered":"Visualizing Fixed-Point Algebraic Loops"},"content":{"rendered":"<p>A few days ago I had to debug a very large model generating an error similar to the following:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2018Q3\/algLoopError.png\" alt=\"Algebraic Loop Error\" \/><\/p>\n<p>In my case, the loop involved more than a hundred blocks and signals spread everywhere in the model and combined in buses, which was making it complex to analyze.<\/p>\n<p><strong>ashow (And why you don't need it anymore!)<\/strong><\/p>\n<p>For many years, I have been using the <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/ashow.html\"><tt>ashow<\/tt><\/a> function to highlight algebraic loops. Here is an example of how it works for a simple model:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2018Q3\/ashow.png\" alt=\"Algebraic Loop highlighted by ashow\" \/><\/p>\n<p>However, in addition to a few usability challenges, one major difficulty with <tt>ashow<\/tt> is that it can only highlight a subset of algebraic loops - and, of course, it cannot highlight the one I needed to solve.<\/p>\n<p>The limitation affecting me was that the algebraic loop was made of fixed-point signals. This led to a sort of chicken-and-egg problem. For <tt>ashow<\/tt> to work, it needs the model to be compiled. But since the loop is made of fixed-point signals, the Simulink engine knows that it cannot solve the loop, and errors out during model compilation, making it impossible to use <tt>ashow<\/tt>.<\/p>\n<p>That's when I remembered a relatively new function that I tend to forget: <tt>Simulink.BlockDiagram.getAlgebraicLoops<\/tt><\/p>\n<p><strong>Simulink.BlockDiagram.getAlgebraicLoops<\/strong><\/p>\n<p>If you read the <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/ug\/algebraic-loops.html\">documentation page on algebraic Loops<\/a> attentively, you might have noticed that Simulink now offers the function <tt>Simulink.BlockDiagram.getAlgebraicLoops<\/tt>, a more modern and powerful way to highlight algebraic loops.<\/p>\n<p>For the same example as above, here is what it looks like:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2018Q3\/highlight_vdp.png\" alt=\"Loop highlighted using Simulink.BlockDiagram.getAlgebraicLoops\" \/><\/p>\n<p>This function was introduced in R2015a, and in R2017b it was enhanced to be able to find and highlight more types of algebraic loops. In my case, it allowed me to highlight a loop made of fixed-point signals. Seeing the loop allowed me to figure out the best place to introduce a delay and break the loop.<\/p>\n<p>A few things to note:<\/p>\n<ul>\n<li>The block highlighted in red is what we call the algebraic variable. Introducing a delay at the output of this block is probably a good first thing to try to remove the algebraic loop (<a href=\"https:\/\/blogs.mathworks.com\/simulink\/2015\/07\/18\/why-you-should-never-break-an-algebraic-loop-with-with-a-memory-block\">Unless the loop is continuous, as explained in this post<\/a>).<\/li>\n<li>If we detect that the loop is <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/ug\/algebraic-loops.html#buqlt88-1\">artificial<\/a>, it will be highlighted with a dashed line, meaning that you should fix it using the Minimize Algebraic Loop feature of <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/ug\/algebraic-loops.html#bsygb2i-1\">Atomic Subsystem<\/a> or <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/gui\/minimize-algebraic-loop-occurrences.html\">Model Reference<\/a>.<\/li>\n<li><tt>Simulink.BlockDiagram.getAlgebraicLoops<\/tt> also returns an output object with properties of the loops in the model, including handles to the blocks involved, which you can use at your convenience.<\/li>\n<\/ul>\n<p>Here is an example of a fixed-point model with an algebraic loop highlighted:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2018Q3\/fixedPointAlgLoop.png\" alt=\"Loop highlighted using Simulink.BlockDiagram.getAlgebraicLoops\" \/><\/p>\n<p><strong>Now it's your turn<\/strong><\/p>\n<p>Are you struggling with algebraic loops? Does using <tt>Simulink.BlockDiagram.getAlgebraicLoops<\/tt> help you understanding them better? Let us know in the comments below.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2018Q3\/fixedPointAlgLoop.png\" onError=\"this.style.display ='none';\" \/><\/div>\n<p>A few days ago I had to debug a very large model generating an error similar to the following:<\/p>\n<p>In my case, the loop involved more than a hundred blocks and signals spread everywhere in the model and... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2018\/07\/09\/visualizing-fixed-point-algebraic-loops\/\">read more >><\/a><\/p>\n","protected":false},"author":41,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[121,43],"tags":[432,44],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/7774"}],"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\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/comments?post=7774"}],"version-history":[{"count":24,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/7774\/revisions"}],"predecessor-version":[{"id":7822,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/7774\/revisions\/7822"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=7774"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=7774"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=7774"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}