MATLAB Simulink – Function ”; Previous Next In this chapter we will understand how to make use of MATLAB function in Simulink. Open Simulink and click on blank model. We need a constant block, a MATLAB function block and a display block for the output. Here is the model created − The MATLAB function is available inside the user defined functions library. Now, click on MATLAB function and drag it inside your model canvas. Double click on the MATLAB function block and write a function of your choice. We will try to display 1 if the number given is odd and 0 if it is even. You can also save the function to be used later with another one. Once the function is done click on the upward arrow close to the MATLAB function and it will take you back to the model you created earlier. Now, let us test the model by using the constant value as 5. We can see the display as 1, which indicates that the value 5 is an odd number. Let us now change the constant value to 10 as shown below and run the simulation: The output is 0 indicating it is even number. Print Page Previous Next Advertisements ”;
Category: matlab Simulink
MATLAB Simulink – Script
MATLAB Simulink – MATLAB Script ”; Previous Next In this chapter, we will use MATLAB script to create a model. We do have a direct and easy method to create a model by just picking the blocks we need. But, writing the code to create a model can sometimes help you to automate a task as your projects get complex. So, let us learn how to create models by using the Application Programming Interfaces (APIs) as discussed below. We will create a very simple sine wave model. For that we need sine wave and scope blocks. Inside the MATLAB command window, we can use API to create the model and blocks. To create a new model, the API is as follows − new_system(”matlabmodel”) Here, matlabmodel is the name of the model. You can open the model by using open_system() with the name of the model as parameter to the function. The command is as shown below − open_system(”matlabmodel”) When you click enter, the model is opened as shown below − Now, let us add sine wave block to it. The command is to add_block(source, dest). You will get the source of the block from Simulink library browser. The highlighted code is the source of sine wave. Let us add that in the add_block as shown below − add_block(”simulink/Sources/Sine Wave”,”matlabmodel/Sine”) The following screen will appear on your computer − Let us now add the scope block, as mentioned below − add_block(”simulink/Sinks/Scope”,”matlabmodel/Scope”, ‘Position’ , [200 315 135 50]) The model shows the scope block as shown below − You can make use of position inside the add_block to position the block properly. For example, add_block(‘simulink/Sinks/Scope’, ‘matlabmodel/Scope’, ‘Position’ , [200 315 135 50] Now, let us connect the line between the sine wave and scope by using the command as shown below − add_line(”matlabmodel”, ”Sine/1”, ”Scope/1”); For add_line, you have to pass the name of your model, followed by the block name and the input port of the blocks. So now, we want to connect the first output port of sine wave with the first input port of scope. Let us now run the simulation by using the command below − result = sim(”matlabmodel”); Now to view the simulation result, run one more command as shown below − open_system(”matlabmodel/Scope”); You will get the output inside the scope as follows − Print Page Previous Next Advertisements ”;
MATLAB Simulink – Environment Setup ”; Previous Next MATLAB simulink is a MATLAB product and to work with it, we need to download MATLAB. The official website of matlab is https://www.mathworks.com/ The following page will appear on your screen − To download MATLAB go to https://in.mathworks.com/downloads/ as shown below. MATLAB is not free to download and you need to pay for the licensed copy. Later on you can download it. A free trial version is available for which you have to create a login for your respective account. Once an account is created, they allow you to download MATLAB and also an online version for a trial of 30 days’ license. Once you are done with the creating a login from their website, download MATLAB and install on your system. Then, start MATLAB or you can also make use of their online version. Simulink comes in-built with MATLAB. Once you install MATLAB, you will get Simulink as shown below − Print Page Previous Next Advertisements ”;
MATLAB Simulink – Mathematical Library ”; Previous Next In this chapter, we will learn how to sum the two given signals and get the output. Select the blank model and open Simulink library browser as shown below − We are going to combine chirp signal and sine wave blocks by using add block from Mathoperation and see the final display. Let us pick the block we want. Select chirp signal and sine wave from sources library, add block from math operations, scope block from sinks library. Join the lines to each block. Double click chirp signal and change the initial frequency from 0.1 to 0.05 and click on Ok button. The other blocks are kept as the default values. Now, click on run to see the output in scope as shown below. Print Page Previous Next Advertisements ”;
MATLAB Simulink – Signals Processing ”; Previous Next In this chapter, we will understand the signals generation in Simulink. To start with, select a blank model from Simulink page and open Simulink browser library as shown below − In sources library, you will get a signal generator symbol. It will help us to create different types of signals. Select the Signal Generator and drag it to get inside the blank model as shown below − To see the output of the signal generator, we need one more block called scope from sinks library as shown below − Select the block and drag it to get inside the model. Double click on signal generator or right click and select block parameters and it will display a screen as shown below − The signal generator can show waveforms like sine, square, sawtooth, random. We will select the square waveform. Let the amplitude and frequency be as 1. Click on OK to update the changes made. Now, connect the lines between signal generator and scope block as shown below − Now click on Run button to see the square waveform as shown below − Let us now try the sawtooth wave form. Right click signal generator or double click and change the waveform to sawtooth. Let us change the frequency to 2. Click on OK to update the changes. Now run the model to see the changes as shown below − Let us now add some more signals to the above model. We will take the step signal from the sources library as shown below − We just have one input for the scope block. Let us increase it to 2 inputs. Right click and open the block parameters. Click on OK button to update the changes. Now, the scope block has 2 inputs as shown below − Connect the step input arrow with the scope arrow. Now click on Run button to run the model. You can add some more signals and test the same. Print Page Previous Next Advertisements ”;
MATLAB Simulink – Adding Delay To Signals ”; Previous Next We have learnt in the previous chapter about the different signal simulations. In this chapter, we will learn how to add delay to the signals. Let us take a blank model and add sine wave and scope block to it as shown below − Let us now run the model to see the simulation in scope block. The sine wave is as shown below − Let us now add delay for the sine wave. We will make use of transport delay block from continuous library as shown below − Select the block and drag it in your model canvas. Now that we have the Transport delay in our model, right click on it and open block parameters as shown below − Let us change the time delay from 1 to 3. Make the changes and click on OK button. Now add one more input port to scope block. Right click on scope block and select the signals and ports. Select 2 for number of input ports as shown below − Now connect the transport delay to sine wave and to scope as shown below Now run the simulation to see a delay of 3 seconds to the sign wave. Right click scope block and select block parameters to see the display. Print Page Previous Next Advertisements ”;
MATLAB Simulink – Home
MATLAB Simulink Tutorial PDF Version Quick Guide Resources Job Search Discussion MATLAB (Matrix Laboratory) is a programming language developed by a computer software company MathWorks. Simulink is a simulation and model-based design environment for dynamic and embedded systems, which are integrated with MATLAB. Simulink is also developed by MathWorks. This tutorial is designed to give students fluency in MATLAB Simulink. Problem-based examples have also been given in simple and easy way to make your learning fast and effective. Audience This tutorial has been prepared for the beginners to help them understand basic to advanced functionality of MATLAB Simulink. After completing this tutorial you will find yourself at a moderate level of expertise in using Simulink from where you can take yourself to next levels. Prerequisites We assume you have a little knowledge of any computer programming and understand concepts like variables, constants, expression, statements, etc. If you have done programming in any other high-level programming language like C, C++ or Java, then it will be very much beneficial and learning MATLAB Simulink will be like a fun for you. Print Page Previous Next Advertisements ”;
MATLAB Simulink – Blocks
MATLAB Simulink – Blocks ”; Previous Next In this chapter, we will learn about one of the basic elements in Simulink. These are termed as blocks. Blocks in Simulink helps to create models. You can make use of a Simulink library browser that has different types of blocks for creating a model. First, open a blank model. The display will be as shown below − You can save your model by clicking on the Save button. Hence, your changes will be saved successfully. Now, open the library browser to get the blocks into your model canvas. The two ways to select the blocks are as follows − Using Simulink browser library. Searching for block inside model canvas. Simulink browser library Open the Simulink library browser as shown below − If you looking for a specific block and don’t know which library, you can search for it inside the search block which is available as shown below − Here, we got all the blocks related to Sine. You can also go inside the library and pick your block. Add block, Product block etc. The display will be as shown below − To bring the block inside your model, you can select the block and drag it inside your model, as shown below − Another way is to right click the block and add to your current model. An example for the same is given below − We have not saved our model. Hence, it comes as untitled. Now, you can add to the model untitled. The block will be seen inside your mode. Searching for block inside model canvas Another way to add blocks to your model is to click inside the model and type the name of the block. It will search in the library browser and list all the model as per what you have typed. An example for the same is given below − We have typed Sine and it displays all the blocks related to sine. Print Page Previous Next Advertisements ”;
Build Model & Apply If-else Logic ”; Previous Next In this chapter, we will create a model and apply if-else logic to it. Let us first collect blocks to create our model. Now, open MATLAB Simulink (blank model) and the Simulink library browser as shown below − Click on the Blank Model and open Simulink library browser as shown below − The blocks we require to build the model with if-else logic is as follows − Constant block from Commonly used blocks Switch block from Signal Routing Display block from Sinks Let us now get all the blocks together to create a model as shown below − Let us now connect the lines with each block. So you can see that the constant block has one output and the switch has three inputs and one output. We are going to connect them to the display block. After connecting the lines, the model is as shown below − Now, double click the switch block and add a threshold. The threshold value will be compared with the block in the center. Based on the constant value of the middle block, the first block value will be displayed or the last constant block value will be displayed. Let us add a threshold value to the switch as shown below − The threshold value given is 3. Click on OK to update the threshold. Now the threshold value is seen inside the switch block as shown below − The middle constant block will be compared with the switch threshold and accordingly the display will be decided. Let us now update the middle constant block with some value as shown below − The value of the constant block is 1. Let us now change the first constant block and give it a value as 0.5 as shown below − Let us now change the last constant with value as 2.5 as shown below − Hence, the first constant value is 0.5, the middle constant value is 1 and the last one is 2.5. The middle constant value 1 will be compared to switch threshold value i.e. 3 as (1 >3). It will print the value as 2.5 the last constant value. Click on the run button to get the output in the display block as shown below − Let us now change the middle constant to a higher value than the threshold of switch and see the output − The value is changed from 1 to 3.5. Click on OK and run the model to see the output in the display − Now, since the value of the middle constant is greater, the value from the first constant is printed in display. If it is less, then the value from the last constant will be printed. Print Page Previous Next Advertisements ”;
MATLAB Simulink – Starting Simulink ”; Previous Next In this chapter, we will understand about using Simulink to build models. Here is a MATLAB display − You can start Simulink by using simulink command in the MATLAB command window as shown below − Click on enter to open the Simulink startup page as shown below − You can also open Simulink from MATLAB interface directly by clicking on Simulink icon as shown below − When you click on the Simulink icon, it will take you to a Simulink startup page, as shown below − The startup page has a blank model, subsystem, library to start the model from scratch. There are also some built-in templates that can help the users to start with. To create a model, the user can click on blank model and it will display a page as shown below − Click on Save to save your model. The blocks to build your model are available inside the Simulink library browser. Click on library browser as shown below − The library browser has a list of all types of libraries with different blocks as shown below − Libraries in Simulink Let us understand some of the commonly used libraries in Simulink. Continuous A continuous blocks library gives you blocks related to derivatives and integrations. The list of blocks are as follows − Dashboard With Dashboard, you will get controls and indicator blocks that help to interact with simulations. The following screen will appear on your computer − Discontinuities Here, you will get a list of discontinuous functions blocks as displayed below − Discrete Here, you will get time relation function blocks as shown below − Logic and Bit Operations In this category, you will get all logical and relational type blocks as displayed below − Lookup Tables You will all the sine, cosine function blocks as shown below − Math Operations All mathematical operations like Add, Absolute, divide, subtract are available. The list is as follows − Messages and Events This block has all the message/communication related functions as shown below − Model Verification The blocks present here helps to self-verify models, such as Check Input Resolution. The following screen will appear on your computer − Model-Wide Utilities This gives you blocks like Model info, Block Support Table etc. The following screen will appear on your computer − Ports and Subsystems You will get blocks like a subsystem, switch case, enable etc. The list is displayed below − Signal Attributes Modify the signal attribute blocks such as Data Type Conversion. The following screen will appear on your computer − Signal Routing The blocks in this category is used to route signal blocks such as bus creator, switch etc.The following screen will appear on your computer − Sinks The blocks in this category help to display or export signal data blocks such as Scope andTo Workspace. The following screen will appear on your computer − Sources It helps to generate or import data blocks. For example, sine wave. The following screen will appear on your computer − String This category has string related blocks as shown below − User Defined functions Custom function blocks such as MATLAB Function, MATLAB System, Simulink Function, and Initialize Function. The following screen will appear on your computer − Print Page Previous Next Advertisements ”;