How to use Loops in Simulink MATLAB : tutorial 7

In this tutorial, I will explain you the working of conditional loops and how to implement them using Simulink. At the beginning you are provided with a brief introduction of loops, and where we can use them also a basic introduction of loops in other programming languages and their working principles. After that loops are implemented on Simulink’s platform and their working is explained step by step. After simple implementation loops are implemented using examples of counter. A counters named as up counter is implemented on Simulink and its outputs is displayed. At the end I gave a simple exercise to perform it on your own, this will help you strengthen your concepts regarding algebraic loops in Simulink.

Introduction to Algebraic loops

 Algebraic loops mean to perform a certain task iteratively up to a certain number of times or un till a specific condition is fulfilled. In Simulink there is no proper block to implement loops, however we can design our own loop in which the output is fed to the input with the delay in between to remove any ambiguity. In order to make the loop error free we must provide a condition which will decide whether the loop will be continued or stopped.

In other programming languages the loops are named as for loop and while loop and at this stage of programming I assume that you are familiar with these loops.

Explanation with a program:

Designing an up counter with Simulink 

Lets’ now move towards the programing part. I will now perform here a simple example which will help you understand the working of loops in simulink. Open MATLAB and then open Simulink. In Simulink click on the library browser icon and open library browser as we have been doing in previous tutorials. From the library browser click on the commonly used blocks section as we have been doing in previous tutorials and then select the constant block from that section as shown in the figure below,How to use Loops in Simulink MATLAB : tutorial 7

Figure 1: Constant block

  • This block will be used as the size of the step of the counter i.e. If o set it to 2 the counter will change its value in steps of 2 as you will see shortly, once the model of the up counter is complete. In order to design an algebraic loop for the implementation of the up counter we need an add block which will add 1 to the previous value after each iteration of the loop. From the library browser select the math operation section as shown in the figure below,How to use Loops in Simulink MATLAB : tutorial 7

Figure 2: Math operation

  • From this section we need an add blocks and the purpose of that blocks is also described. For the time being select the add block from the math operation section of library browser as shown in the figure below,How to use Loops in Simulink MATLAB : tutorial 7

Figure 3: Add block

  • This add block will add up the previous value of the counter to the constant block placed previously to complete one iteration. Now if we directly attach the output of the add block to its input Simulink will consider it as an error because both the operations are implemented on the same instant. Therefore, we need a delay block that will add a delay in the addition of the previous value. From the library browser select the commonly used blocks section and from that section select the delay block as shown in the figure below,How to use Loops in Simulink MATLAB : tutorial 7

Figure 4: Delay block

  • Up till now in these tutorials we have been using the drag and drop method to place a component on the Simulink model. We also have another choice. Right click on the block you want to add or place in the simulink model and then click on Add block to the model from the drop down menu as shown in the figure below,How to use Loops in Simulink MATLAB : tutorial 7

Figure 5: Adding to model

  • This will add the selected block to the current model. Now in order to see the output of the counter we also need some output display. This display block, however, will only show the most recent output after the execution that’s why it is not suitable here. We will use the scope block of Simulink to see all the process of the loop in one window. Therefore, from the commonly used blocks section select the scope block as shown in the figure below,How to use Loops in Simulink MATLAB : tutorial 7

Figure 6: Scope block

  • After selecting the block add the block to the current model using any of the suitable method. The placed components for the up counter are shown in the figure below,How to use Loops in Simulink MATLAB : tutorial 7

Figure 7: Placed components

  • As we need to add a delay in the output of the add block and feed its output to the input of the add block we must flip it in the first hand. Right click on the flip block then select rotate and flip after that select flip as shown in the figure below,How to use Loops in Simulink MATLAB : tutorial 7

Figure 8: Flipping block

  • You can also do this by selecting the block and pressing Ctrl+I from the keyboard. Connect all the components as shown in the figure below,How to use Loops in Simulink MATLAB : tutorial 7

Figure 9: Block diagram

  • Run the model from the run icon present at the top of the Simulink model and wait for the compilation to done. Once the compilation is done double click on the scope to see the output of the counter which must be a stair case as shown in the figure below,How to use Loops in Simulink MATLAB : tutorial 7

Figure 10: Output

  • The problem with the counter is that there is no resetting condition of this counter. A counter with no resetting condition is of no use. It will start updating the previous value to infinity. We must need to provide it some limiting value after which it must reset its value to zero and start over again. For that purpose, we need a conditional statement as we have used in previous tutorial. From the library browser select the signal routing section as shown in the figure below,How to use Loops in Simulink MATLAB : tutorial 7

Figure 11: Signal routing

  • After that select the switch block from this section as shown in the figure below,How to use Loops in Simulink MATLAB : tutorial 7

Figure 12: Switch block

  • Select the threshold value of the switch block to that specific value up to which you want your counter to travel all the way from zero. In this case I have selected the value of 10 as shown in the figure below,How to use Loops in Simulink MATLAB : tutorial 7

Figure 13: Threshold value

  • The counter will start adding one as long as this condition is false so the false condition will be simply the output of the counter as shown in the figure below,How to use Loops in Simulink MATLAB : tutorial 7

Figure 14: False condition

  • The true condition however will reset the value of the counter to 0. Therefore, in the true condition place a constant block with a value of 0 in it as shown in the figure below,How to use Loops in Simulink MATLAB : tutorial 7

Figure 15: True condition

  • The input to the delay block this time will be the output of the switch block and the complete block diagram is shown in the figure below,How to use Loops in Simulink MATLAB : tutorial 7

Figure 16: Conditional up counter

  • Run the block diagram and the output of the counter can be viewed by double clicking on the scope block as shown in the figure below,How to use Loops in Simulink MATLAB : tutorial 7

Figure 17: Output of up counter

This is a simple up counter which will start from zero and end to the value provided by us in the switch block.

Exercise:

  • Design an down counter using MATLAB’s Simulink.

(Hint: You might need to change the condition of the switch block from > to <)

Leave a Comment