{"id":57,"date":"2009-06-02T03:00:41","date_gmt":"2009-06-02T03:00:41","guid":{"rendered":"https:\/\/blogs.mathworks.com\/seth\/2009\/06\/02\/the-if-else-construct-in-models\/"},"modified":"2009-06-02T17:30:07","modified_gmt":"2009-06-02T17:30:07","slug":"the-if-else-construct-in-models","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2009\/06\/02\/the-if-else-construct-in-models\/","title":{"rendered":"The If-Else Construct in Models"},"content":{"rendered":"<p>Balaji Kalluri asked a question in the comments of a <a\r\nhref=\"https:\/\/blogs.mathworks.com\/seth\/2008\/04\/03\/advanced-maneuvers-using-mux-and-demux\/\">post\r\nabout the MUX block<\/a>.\u00a0 Balaji asked:<\/p>\r\n\r\n<p><blockquote>Hi All<br>\r\n        I would be highly obliged, if someone can show me a route to model a typical\r\n        2-to-1 Multiplexer, the way we have read &amp; understood it. I want to\r\n        translate behavior of typical digital 2-to-1 Mux works like this into SIMULINK\r\nmodel.<br><br>\r\n\r\nif(sel==0)<br>\r\nout = in1;<br>\r\nelseif (sel==1)<br>\r\nout = in2;<\/blockquote><\/p>\r\n\r\n<p>Today I want to look at one way to solve this question.<\/p>\r\n\r\n<p><strong>Controlling which signal passes through (Switch)<\/strong><\/p>\r\n\r\n<p>One way to think about this problem is to control which\r\nsignal gets passed through the system.\u00a0 The switch block provides a way to pass\r\nthrough one signal or another based on the value of a control signal.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q2\/switchBlockMdl.png\" alt=\"Model of conditional pass through using a switch.\"><\/p>\r\n\r\n<p>This creates an if-expression: when the conditional\r\nstatement is true, out is set equal to the first inport, otherwise, out is set\r\nequal to the third inport.<\/p>\r\n\r\n<p><strong>Optimizing the model<\/strong><\/p>\r\n\r\n<p>This represents the desired logic, but there is another way\r\nto think about the problem.\u00a0 Consider that both <em>in1<\/em> and <em>in2<\/em> are\r\nthe result of some other expensive calculations.\u00a0 When this is the case, you\r\nwill want to perform only one of the calculations needed for your output\r\nexpression instead of always computing both values and then discarding the one\r\nthat isn\u2019t needed.<\/p>\r\n\r\n<p>Luckily, this is a common pattern and Simulink provides an\r\noptimization to improve efficiency.\u00a0 By default, <em>Conditional input branch\r\nexecution<\/em> is turned <em>On<\/em> (in the Configuration Parameters -&gt;\r\nOptimization page).<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q2\/conditionalInputBranchExecution.png\" alt=\"Conditional Input Branch Execution optimization from Simulink configuration parameters.\">\u00a0<\/p>\r\n\r\n<p>This optimization allows Simulink to group the upstream\r\nblocks together and tuck them into the conditional statement of the Switch\r\nblock.\u00a0 You may be able to understand the behavior if you turn on the block\r\nsorted order display, but I prefer to look at the code for simple models like\r\nthis.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q2\/codeCompareCondBranchOpt.png\" alt=\"Comparison of generated code with and without conditional input branch execution optimization.\"><\/p>\r\n\r\n<p>This optimization is not just limited to the Switch blocks.\u00a0\r\nOther blocks that produce conditional expressions can incorporate upstream and\r\ndownstream blocks in their code.<\/p>\r\n\r\n<p>I want to end this discussion with two important notes.<\/p>\r\n\r\n<ol>\r\n<li>I made up this example specifically to demonstrate the effect of this optimization.\u00a0\r\nBecause it is so simple, I had to disable Expression Folding, or else the code\r\nwould be the same, no matter what.\u00a0 I do not encourage anyone disable\r\nexpression folding without good reason.<\/li>\r\n<li>Because this is an optimization, you cannot guarantee this behavior.\u00a0 If you add\r\nsomething as simple as a <em>Test point<\/em> between the Gain and Switch Block\r\nthis will disable the optimization.<\/li>\r\n<\/ol>\r\n\r\n<p>There are other ways to model efficient conditional behavior\r\nand explicitly guard the execution of an algorithm.\u00a0 I will leave that topic for\r\na future post.<\/p>\r\n\r\n<p><strong>Now it\u2019s your turn<\/strong><\/p>\r\n\r\n<p>Do you think about the efficiency of your models?\u00a0 How do\r\nyou use the Switch block?\u00a0 Leave a <a\r\nhref=\"https:\/\/blogs.mathworks.com\/seth\/?p=57&amp;#comment\">comment here<\/a> and\r\nshare what you know.<\/p>","protected":false},"excerpt":{"rendered":"<p>Balaji Kalluri asked a question in the comments of a post\r\nabout the MUX block.\u00a0 Balaji asked:\r\n\r\nHi All\r\n        I would be highly obliged, if someone can show me a route to model a typical\r\n       ... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2009\/06\/02\/the-if-else-construct-in-models\/\">read more >><\/a><\/p>","protected":false},"author":40,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[67,55,11],"tags":[93,441,92],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/57"}],"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=57"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/57\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=57"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=57"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=57"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}