You are on page 1of 4

LAB-03

LEARNING MATLAB BASICS PART 03

Generating functions and introduction to Simulink

Objectives/lab goals:
Learning major command such as:

1. Generating Functions
2. Simulink

Functions:
A function is a group of statements that together perform a task. In MATLAB, functions are defined in
separate files. The name of the file and of the function should be the same.

Functions can accept more than one input arguments and may return more than one output arguments.
Syntax of a function statement is −
function [out1,out2, ..., outN] = myfun(in1,in2,in3, ..., inN)
Example

1. The following function named mymaxnum should be written in a file named mymaxnum.m. It
takes five numbers as argument and returns the maximum of the numbers.
2. Create a function file, named mymaxnum.m and type the following code in it.
3. Command function [] is used to generate the new function named as “mymaxnum”.
4. Then if else are used for conditioning.
5. This will return the number which is largest.

Figure 1: Defining new function


Example:

1. Create a new m file and named it same as the function name.


2. Insert two output variables such as area and circ.
3. In the right side of the assignment, write the name of the input variable that is radius.
4. Write the inside code and enclosed it with end keyword.

Figure 2: Example of defining new function as in this case mymaxnum is a new function introduced in matlab

Introduction to Simulink in MATLAB


Simulink (Simulation and link) is developed by MathWorks as an add-on with MATLAB. It is a
graphical programming language which offers modelling, simulation and analyzing of multi domain
dynamic systems under Graphical User Interface (GUI) environment. The Simulink have tight
integration with the MATLAB environment and have a comprehensive block libraries and toolboxes for
linear and nonlinear analyses. The system models can be so easily constructed via just click and drag
operations. The Simulink comes handy while dealing with control theory and model based design.

After the MATLAB is opened Simulink session can be started in 2 ways

 In the MATLAB command window enter the command

>> simulink

 The alternate method is to click the Simulink icon in the MATLAB toolbar
Figure 3: accessing simulink through matlab

A Simulink library window will pop up as shown below:

Figure 4: Simulink Library Browser

The Library Browser contains various toolboxes in left side and corresponding utilities and blocks on
the right side.

Creating a model:

To start creating a model go to File –> New or alternatively Ctrl+N. A Work space / Model window will
pop up as shown below:

It will take a few moments for the first time running on laptop.
Figure 5: simulink window

Here create a simple model of integrating a sine wave and display both the input sine wave and the
integral form. To create this model, required is

1. A sine wave signal source


2. Integrator
3. A Multiplexer, as i need to display the 2 signals in the Display screen. Note that the display
screen called ‘scope’ has only 1 channel. So either to show 2 signals so have to use a mux or
have to use 2 screen blocks
4. A display screen

Charting down all the needed components is good practice when it comes to designing of higher models.

STEP 1:
Selecting Blocks
 For a sine wave sources

Simulink –>Sources –> Select sine wave from the list


 For Integrator

Simulink –>Continuous–>Select integrator from the block list


 For Multiplexer

Simulink –>Signal routing –> Select Mux from the list


 For Display

Simulink –>Sink –> Scop

You might also like