Simulink Tutorial Series – 5 [While Loop]

Simulink Tutorial Series - 5

If you are looking for a quick solution about how to implement the while Loop in Simulink model , this article is definitely going to be helpful.

I will explain two different methods to implement a while loop in Simulink model:

  1. While loop in Simulink
  2. Do while loop in Simulink

In this Simulink Tutorial Series, I am going to discuss real-life examples using Simulink model and build the Simulink model step-by-step.

Matlab/Simulink is the leading software for model based development in aerospace, automotive industry etc.

There are several other online resources, where you can get theoretical knowledge about Matlab/Simulink. In fact, Matlab has very good documentation for each of their products. But, there are not many resources to explain the theory along with good working examples. This is going to be a series of articles. This is the 5th article in this series.

In every article in this Simulink Tutorial Series, I will explain how to build models for a particular problem.

What Will You Learn?

In this article, I am going to explain to you how you can implement while loop and do while in the Simulink model.

If you are familiar with other programming languages such as C, C++, Java, or any other programming languages, you already know that every programming language has a while loop and do while loop construct.

Similarly, In Simulink, we can build a model including while loop and do while loop logic.

So, if you go through this article, you will be able to build your own Simulink model using while loop and do while loop logic.

Assumption

I am assuming, you already gone through my previous articles –

Simulink Tutorial Series – 1
Simulink Tutorial Series – 2
Simulink Tutorial Series – 3
Simulink Tutorial Series – 4

What You already know

From my previous posts, you already know –

  1. How to create a simple blank Simulink Model
  2. How to use the Simulink Library Browser and find out the required Library block
  3. How to implement if-else logic in Simulink Model
  4. How to implement switch-case construct in Simulink Model
  5. How implement for loop in Simulink

I have discussed all the above points in my previous post.

What is While Loop in Simulink?

If you already know while loop in Simulink, you can skip this section and move to the next bullet point. Most of you may have previous coding experience and understand the while loop construct.

The while loop allows the programmer to execute a statement or a set of statements multiple times. Here is an example of while loop in c programming language:

    int while_counter = 1;
    int result = 0;
    while (while_counter <= 5)
    {
        result = result + while_counter;
        ++while_counter;
    }

This is an example code snippet of while loop in C programming language. The concept of while is exactly the same as for loop. The only difference is in the syntax. Here, the condition for the while loop is (while_counter<= 5), that means, as long the value of the “while_counter” variable is less than or equal to 5, the while loop will continue to execute its body.

The while loop in Simulink works in the same way as I have explained here.

Normally, the programmer tends to use the while loop when the number of iteration is unknown and unpredictable. On the other hand, the for loop is used when the programmer knows the number of iterations at the time of programming.

What is Do While Loop in Simulink?

Here is an example code snippet of do while loop in C programming language:

  
int while_counter = 1;
int result = 0;
   do
    {
        result = result + while_counter;
        ++while_counter; 
    }
    while(while_counter <= 5);

The do while loop works in the same way as while loop. The only difference is, the do while loop executes the loop condition after executing the loop body. Therefore, the do while loop ensures the execution of the loop body at least once.

So, the do while loop is useful when:
1. we do not know the number of iterations before-hand and
2. we want to execute the loop at least once.

How to implement while loop in Simulink?

First of all, I need the following Simulink Library blocks to implement the while loop in Simulink and showcase a functionality – adding numbers from 1 to 5 and display the output:

  1. While Iterator Library Block
  2. Add block
  3. Delay Block
  4. Display Block

Step-1:

Now, I will drag and drop all the required library blocks in a blank Simulink model/canvas:

Simulink Tutorial Series - 5;

Step-2

Set the constant values and connect these blocks correctly.

Simulink Tutorial Series - 5;2

Step-3

Now, let us configure the while loop. I will set the maximum number of iteration as 5 and select the iteration number port checkbox.

while setting

Step-4

As I said before, my goal is to add the numbers from 1 to 5 using while loop in Simulink. So, now, I will add other required blocks to implement the functionality:

while loop in simulink

Step-5

Now, my final model is ready for demonstrating the while loop in Simulink:

while loop simulink

While loop in Simulink Model Output

The output of the model is displayed below. If you have any questions about this model or execution of the model, please comment in the comment box below.

while loop simulink model

How to implement Do While loop in Simulink?

Now, I will explain step-by-step, how to implement the do-while loop in the Simulink. I am considering a simple functionality for better understanding – Add all the numbers from 1 to 5 and display the result.

Step-1

I will add the required blocks in a blank Simulink model. For do-while, you have to use the same “While Iterator” library block and then update the setting to make it work as a do-while loop.

Do while loop simulink

Step-2

Now, I will double click on the “while Iterator” block and set the while loop as do-while loop. I just need to set the while loop mode as “do-while” to make the “While Iterator” block work as do-while loop instead of while loop.

Do while loop in simulink

Step-3

I will now connect all the other required blocks to implement the functionality:

Do while simulink model

Step-4

Now, the model is ready for the do-while loop. You can notice here that, unlike while loop, there is no initial condition port (IC) in case of do-while loop.

do while in simulink model

Do-While loop in Simulink Model Output

The output of the model is displayed below. If you have any questions about this model or execution of the model, please comment in the comment box below.

do while simulink model output

Download Simulink Model

You can download the above Simulink models by Clicking HERE.

Summary

Matlab/Simulink is a well known and very popular tool used for Model Based Software Development in the aerospace and automotive industry.

Today, in this article (while loop Simulink and do while loop Simulink), I have explained step-by-step, how to implement while loop and do while loop in the Simulink model using Simulink library blocks from scratch.

If you have any questions, please feel free to comment in the comment box below. 👇👇👇

I will keep sharing useful real-life Simulink model example here – Simulink Tutorial Series.

Happy learning!

Simulink Tutorial Series – 5

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top
error: