{"id":2483,"date":"2013-08-28T12:36:16","date_gmt":"2013-08-28T17:36:16","guid":{"rendered":"https:\/\/blogs.mathworks.com\/seth\/?p=2483"},"modified":"2013-08-28T12:36:16","modified_gmt":"2013-08-28T17:36:16","slug":"implementing-an-iterative-algorithm-using-the-while-iterator-subsystem","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2013\/08\/28\/implementing-an-iterative-algorithm-using-the-while-iterator-subsystem\/","title":{"rendered":"Implementing an iterative algorithm using the While Iterator Subsystem"},"content":{"rendered":"<p>Last week I helped a user implementing an iterative algorithm in Simulink using the <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/whileiteratorsubsystem.html\">While Iterator Subsystem<\/a>. I thought it would be interesting to share this technique.<\/p>\r\n\r\n<p><strong>Example Iterative Algorithm<\/strong><\/p>\r\n\r\n<p>An <a href=\"http:\/\/en.wikipedia.org\/wiki\/Iterative_method\">iterative algorithm<\/a> is an algorithm that, when called multiple times, converges toward a more and more accurate solution.<\/p>\r\n\r\n<p>A well known example of this is the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Methods_of_computing_square_roots\">Babylonian method of computing square roots<\/a>. <\/p>\r\n\r\n<p><\/p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q3\/square_root_method.png\" alt=\"Babylonian method of finding square roots\" \/>\r\n\r\n<p>For example, using this method the following script takes about 10 iterations to compute the square root of 900 with an error less than 1e-6.<\/p>\r\n\r\n<p><\/p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q3\/matlab_while_example.png\" alt=\"Finding square root in MATLAB\" \/>\r\n\r\n<p><strong>Iterative Algorithm in Simulink<\/strong><\/p>\r\n\r\n<p>Typically, in Simulink, blocks are executed once per time step (<em>it is not always true, but let's assume that for today<\/em>). For example, if I implement the equations above at the top level of a model with a fixed step solver and a sample time of 1 second, it will take ten seconds of simulation before the algorithm converges:<\/p>\r\n\r\n<p><\/p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q3\/iterative_through_time.png\" alt=\"Finding a square root in Simulink, one step at a time\" \/>\r\n\r\n<p>As you can see, for a constant input, the algorithm slowly converges. But what if the input changes constantly? And what about the first steps where the answer is wrong?<\/p>\r\n\r\n<p>We clearly need the algorithm to iterate multiple times at the same time step. To do that, you need an Iterator Subsystem:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q3\/iterative_simulink.png\" alt=\"Finding a square root in Simulink using a while iterator\" \/><\/p>\r\n\r\n<p>With this implementation, at every step you will get an answer within the specified tolerance.<\/p>\r\n\r\n<p><strong>Starting with results for the previous step<\/strong><\/p>\r\n\r\n<p>In the implementation above, if we start the search with an initial guess of 1 every step, this is not very efficient. To help with that, you can start the search with the result of the previous time step by configuring the While Iterator Subsystem to hold its state:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q3\/whileconfig.png\" alt=\"Finding a square root in Simulink using a while iterator\" \/><\/p>\r\n\r\n<p><strong>Now it's your turn<\/strong><\/p>\r\n\r\n<p>Of course, I do not recommend using this algorithm to compute the square root of a Signal in Simulink. The <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/sqrt.html\">Square Root<\/a> block is more optimized than the above approach.<\/p>\r\n\r\n<p>I hope this gives you an overview of the concept. Let us know if you use the <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/whileiteratorsubsystem.html\">While Iterator Subsystem<\/a> to implement iterative algorithms by leaving a <a href=\"https:\/\/blogs.mathworks.com\/seth\/?p=2483&#comment\">comment here<\/a><\/p>\r\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q3\/whileconfig.png\" onError=\"this.style.display ='none';\" \/><\/div><p>Last week I helped a user implementing an iterative algorithm in Simulink using the While Iterator Subsystem. I thought it would be interesting to share this technique.\r\n\r\nExample Iterative... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2013\/08\/28\/implementing-an-iterative-algorithm-using-the-while-iterator-subsystem\/\">read more >><\/a><\/p>","protected":false},"author":41,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[33],"tags":[337],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/2483"}],"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=2483"}],"version-history":[{"count":19,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/2483\/revisions"}],"predecessor-version":[{"id":2595,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/2483\/revisions\/2595"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=2483"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=2483"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=2483"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}