In this tutorial, I will explain you how to work with conditional logical statements in MATLAB Simulink. First of all, a brief and concise introduction of logical statements i.e. IF Else statements is provided, and how we can use them in MATLAB’s simulink. After that the concept provided in introduction part is implemented with the help of MATLAB’s Simulink, and the theoretical results are compared with the virtual results of the block diagram provided by Simulink which comes out to be same. At the end of the tutorial you are provided with an exercise to do it by yourself, and in the next tutorials I will assume that you have done those exercises and I will not explain the concept regarding them.
Table of Contents
Introduction to conditional (IF ELSE) statements
If else statements are used to allows different executions depending on the value of the conditional logic. Based on if the condition given and the input variable matches, the if statements under the if block will be executed and if the condition does not match the else block will be executed.
If you have an experience to work with other programming languages, you must have encountered with conditional statements i.e. If-ELSE statements or IF-THEN-ELSE statement. These statements are used to implement a certain portion of code if a specific condition is true and implement the other portion if that certain condition comes out to be false. Conditional statements are also sometime referred to as conditional expressions or conditional constraints, and are specifically used in cases where we want to execute only selective lines of code.
In Simulink, however, these conditional constraints are referred as switch blocks. Their working depends on a certain condition, which if comes out to be true, will execute one statement, and if turns out to be false, will implement other statement as you will see shortly in the explanation with program portion.
Example of using if else statements in simulink
- Lets’ now move towards the programming part. I will now perform here a simple example which will help you understand the working of switch block in Simulink and how it is able to perform the if else statement. Open MATLAB first of all and then open Simulink by using either command window or the Simulink icon on the front page of MATLAB. After that click on the library browser icon present on the main window of Simulink as shown in the figure below,
Figure 1: Library browser
- This button will open the library browser of the Simulink from which we can select any required block of the need. From the commonly used blocks section of library browser select the constant block as shown in the figure below,
Figure 2: Constant block search
- Drag and drop the constant block from the library browser to simulink block diagram portion. This block will be used as an input to be displayed if the condition is true. Place three such blocks in the simulink main as shown in the figure below,
Figure 3: Constant blocks
- These blocks have different purposes. One of this block is used to store the input which will be displayed at the output if the condition of the conditional block is true. Similarly, one of the block is used to display the output if the condition is false. The third block is a special purpose block and is used as a conditional block. For instance, the condition in the switch block will be verified with respect to this conditional constant block. And condition will be named as true or false depending on this block. Name these blocks according to their working so that they can be recognized in the future as shown in the figure below,
Figure 4: Named blocks
- After naming the blocks lets now come toward the switching part. Simulink provides us with a block named as switch. You can either search it by its name or go to the library browser of simulink and click on the signal routing section as shown in the figure below,
Figure 5: Signal routing
Figure 6: Switch block
- Drag this block from the library browser and drop in front of the already placed three blocks. Connect the true block at the top input of switch block, condition at the middle input and false at the lower input of the switch block as shown in the figure below,
Figure 7: Connected blocks
- Now as we are interested in displaying our result at the output of the switch to verify the working, lets place a display block. Go to the sinks section of the library browser as shown in the figure below,
Figure 8: Sinks
Figure 9: Display block
- Drag this block from library browser and drop it at the output of the switch block and connect both the blocks, as shown in the figure below,
Figure 10: Display connected
- Now lets’ set the condition of the switch block which will decide whether the condition in the condition constant is true or false. Double click on the switch block and in the parameter dialog box, we can select the type of the condition and the threshold vale as well. For instance in our case I selected the threshold condition as shown in the figure below,
Figure 11: Threshold condition
- Now we can also change the value of the threshold as I have selected 5 in this case as shown in the figure below,
Figure 12: Threshold value
- The condition I have selected above will give a true output if the value in the conditional block is greater than 5 and false otherwise as you will see shortly. Now in the true constant block, by double clicking on it, set a value of 10 in the block parameter dialog box as shown in the figure below,
Figure 13: True block value
Figure 14: False block value
Figure 15: Block diagram
- Run the block diagram from the run button and the output will be displayed in the display block as shown in the figure below,
Figure 16: False output
- The output is zero because the condition is false in this case. Now update the value of the conditional block to a value greater than 5. For this case I change the constant value to 6 as shown in the figure below,
Figure 17: Conditional constant value
- Again run the function and the output this time will be 10 (true condition value) because the condition in this case is true as shown in the figure below
Figure 18: True output
Design a conditional block diagram than will display a 10 at the output if the conditional block is 0 and 100 otherwise.