Elementary MATLAB & SIMULINK

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 46

MATLAB

by

A Preliminary R.KARTHIKEYAN ,
Associate Professor/EEE
Sri Venkateswara college of
engineering(Autonomous)

Introduction
A MINIMUM MATLAB & SIMULINK TO GET YOU STARTED – VERY
BASIC TUTORIAL
What is MATLAB & SIMULINK?

MATLAB is an interactive computational


environment for mathematical calculation as well
as to solve engineering problems through
programming.

SIMULINK an associate of MATLAB which uses


graphical programming approach to simulate
dynamical systems
How to start MATLAB?

Double click on the MATLAB icon. First time MATLAB opens with several windows. Close all windows except
Command window. Yes keep only command window.
Click on layout tab and from the dropdown menu select
Command window only. So you will see only command
Window.

This is the MATLAB command prompt >> where you will enter MATLAB command(s) and press enter key
Basic Mathematical operations
Addition >> 2+3 Press Enter Key [PEK]. You will see the answer immediately and it will be assigned to a variable ans

Subtraction >>4-3 [PEK]

Multiplication >> 3*4 [PEK]

Division >> 20/5 [PEK]

Exponentiation >>2^3 [PEK]. The MATLAB screen response is shown below. After every entry press the enter
key
You can assign the values to a variable and then operate on the
Variables. To clear all variables from temporary memory and to
start with a clear slate use
>>clear all or >>clear vars {This will clear all variables}
>>clc This will give you a clean screen
Vector Operation

In MATLAB you create a row vector by separating values by space or comma.

You create a column vector by separating values by semicolon. The single upper ‘
will transpose the vector.

A dot preceding a operator will perform point by point


Operation. Each element is operated with the
Corresponding element. This is also called array
operation
Vector Creation
You can create a row vector in two ways
Way 1
>> Variable name= first number : increment: last number
>>A=0:2:10 This will produce a row vector starting from 0 and increase by 2
until last number 10 is reached. That is 0 2 4 6 8 10

Way 2
Through linspace command.
>>B=linspace(first number,last number,number of points) >>
B=linspace(0,10,5)
MATLAB Programming
If you place the series of commands in a file, store
that and then run it then this is called a MATLAB
program. This is called script m file.

Another type is there called function m file which we


will not cover in this basic tutorial.

To create a m file you need MATLAB which you


get by typing the command edit at command
prompt and press enter key. >>edit

An editor window appears. In that you enter


your commands one after another ,save the file with a
name . Press [Ctrl+S] keys , save with a name. To run
the program just enter the file name and press enter key.
>>rlc
A simple MATLAB program
>>edit an editor window appears either separately or at the upper part of
command window depending on the version you use.

>> ohm
Enter V(V) : 10
Press ctrl+S keys. Save as dialog Enter R(ohm) : 5
Appears.In that enter file name The current in A is :
2
To run the program enter file The power dissipated in W is :
name at command prompt and 20
press enter key to get result
DATA VISUALIZATION
WITH MATLAB

TWO DIMENSIONAL PLOT


MULTIPLE PLOTS
What is Data Visualization?
To visualize data obtained from an experiment , Observation of an event , Outcome of a program or Simulation in a presentable
form using MATLAB.

Why MATLAB?
 MATLAB is commonly used in colleges for programming , problem solving and for simulation of dynamic systems.

 MATLAB’s interpolation algorithms for graphing is so powerful that one tend to obtain realistic graphs.

 The graphs from MATLAB are presentable for publication in journals and books. For demonstration of research results.

Presentation Outline
 General two dimensional plot
 Multiple plots
PARTS OF A GRAPH
Graph title

Polynomial Fits
2500
Basic data
Linear

2000 Graph identifier Second order


Third orde Legend
Fourth order

1500

Pressure(kPa)
Y axis label Graph color
1000 Grid line

500

0
1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 6
Volume(m3)
Axis limits
X axis label
GENERAL PLOT COMMANDS
• Plot(x,y) x – x variable as a row vector y- y variable as a row vector
• xlabel(‘ label name’) x- axis name with the name within single quote
• ylabel(‘ label name’) y- axis name with the name within single quote
• title(‘ title name’) – Graph title with the title within single quote

• axis([xmin xmax ymin ymax]) – To define x and y axis limits

• legend(‘graph1’,’graph 2’,’graph 3’) – to show legend on multiple graphs

• gtext(‘ curve name’) – To indicate curves through mouse click


• grid – To add grid lines to the graph
Ohm’s Law Plot

From experiment we obtained the following data to draw a plot of ohm’s law V(V) I(A)
0 0
2 0.0009
>> V=[0 2 4 6 8 10 12 14 16 18 20]; (Define voltage values) 4 0.0018
6 0.0027
8 0.0036
>> I=[0 0.0009 0.0018 0.0027 0.0036 0.0045 0.0055 0.0064 0.0073 10 0.0045

0.0082 0.0091]; (Define current values) 12 0.0055


14 0.0064
16 0.0073

>>plot(V,I),xlabel('V(V)'),ylabel('I(A)'),title('Ohm''s Law'),grid 18
20
0.0082
0.0091

Press enter key after each entry

Note the double single quote for apostrophe


To copy the graph to word , PPT or Excel

In the figure window Edit  Copy Figure

Then paste using [Ctrl+V] shortcut


After pasting in PPT slide

Ohm's Law
0.01

0.009

0.008

0.007

0.006
I(A)

0.005

0.004

0.003

0.002

0.001

0
0 2 4 6 8 10 12 14 16 18 20
V(V)
Two curves
You can draw multiple curves on a single graph.

To differentiate we can use line markers and Line colours

Line Markers Commonly used

o – circle * - asterisk d – diamond p- pentagon s – square

Line Colors

k – black r- red g – green b- blue m-magenta

Let us see an example


>> t=0:0.5:10; We define t from 0 to 10 in steps of 0.5

>> y1=t.^2; y1  t
2

>> y2=t.^3; y2  t 3

>> plot(t,y1,'-o',t,y2,'-*'),xlabel('t'),ylabel('y_{1} & y_{2}'),title('Two curves'),grid

Line with ‘o’ circle marker

Line with ‘*’ star marker To introduce subscript use this


Two curves
1000

900

800

700

600
y1 & y2

500

400

300

200

100

0
0 1 2 3 4 5 6 7 8 9 10
t
Legend on a graph

>> t=0:pi/50:2*pi;

>> y1=sin(t);

>> y2=cos(t);

>> plot(t,y1,t,y2),xlabel('t'),ylabel('y_{1} & y_{2}'),title('Two Curves'),legend('sin(x)','cos(x)'),grid

Legend of sine and cosine curves through legend command


Two Curves
1
sin(x)
0.8 cos(x)

0.6

0.4

0.2
y1 & y2

-0.2

-0.4

-0.6

-0.8

-1
0 1 2 3 4 5 6 7
t
Curve Indicator through mouse click
Consider the following experimental result
Transistor output Characteristics
Vbe=0.5V Vbe=1V Vbe=2V
Vce(V) Id(A) Vce(V) Id(A) Vce(V) Id(A)
0 0 0 0 0 0
0.5 0.125 0.5 0.25 0.5 0.375
1 1 1 2 1 3
1.5 3.375 1.5 6.75 1.5 10.125
2 8 2 16 2 24
2.5 15.625 2.5 31.25 2.5 46.875
3 27 3 54 3 81
3.5 42.875 3.5 85.75 3.5 128.625
4 64 4 128 4 192
4.5 91.125 4.5 182.25 4.5 273.375
5 125 5 250 5 375
In the command window enter Id=[

Select the portion shown in Excel

Press [CTRL+C] to copy

In MATLAB command window click cursor next to [

Press [CTRL+V] to paste .Next enter ] to close and press enter key
You see the response as

Plot between pair of columns ie first two columns ,next two columns and then last to columns

>> plot(Id(:,1),Id(:,2),Id(:,3),Id(:,4),Id(:,5),Id(:,6)),xlabel('V_{ce}'),ylabel('I_{d}'),title('Diode
Characteristics'),gtext('V_{be}=0.5V'),gtext('V_{be}=1V'),gtext('V_{be}=2V'),grid

To indicate Vbe we use gtext command.A cross hair that moves with mouse appears

You have to move mouse over or near a curve and click once to fix indicator.

Repeat this for the next two curves.


Click here
After second click
Subplots
In one big graph you
show
Small graphs

The syntax is
subplot(m,n,r)

m= number of rows
n=number of columns
r= picture number
counted from
Top to bottom with
scanning from left to
right in Z manner

The subplot is followed


by usual plot commands

An example will make


this clear
>> x=0:0.5:5;
>> y1=sin(x);
>> y2=x.^2;
>> y3=x.^3;
>> y4= exp(x);
>> subplot(2,2,1),plot(x,y1),subplot(2,2,2),plot(x,y2),subplot(2,2,3),plot(x,y3),subplot(2,2,4),plot(x,y4)

1 25

20
0.5
15
0
10
-0.5
5

-1 0
0 2 4 6 0 2 4 6

150 150

100 100

50 50

0 0
0 2 4 6 0 2 4 6
Another Example

>> x=0:0.1:50;
>> y1=sin(x);
>> Y2=tan(x);
>>subplot(2,1,1),plot(x,y1),xlabel('x'),
ylabel('y_{1}'),title('sin(x)'),grid,subplot(2,1,2),
plot(x,Y2),xlabel('x'),ylabel('y_{2}'),title('tan(x)'),
grid
Response
>>subplot(2,1,1),plot(x,y1),xlabel('x'),ylabel('y_{1}'),title('sin(x)'),grid,subplot(2,1,2),plot(x,Y2),xlabel('x'),ylabel('y_{2}'),title('tan(x)'),grid

sin(x)
1

0.5

0
y1
-0.5

-1
0 5 10 15 20 25 30 35 40 45 50
x
tan(x)
400

200

0
y2

-200

-400
0 5 10 15 20 25 30 35 40 45 50
x
Simulink – A basic introduction

Simulink can be thought of as a graphical programming approach. In Simulink you


use block diagram format to formulate the simulation.

You get the blocks from various Simulink libraries and toolboxes , connect them as
a pattern , set simulation parameters , run the simulation and get the graphical
result

To get Simulink type simulink at command prompt >>simulink [PEK]

Simulink library browser appears. In that click as shown

A model window appears. In that we create your


Simulink simulation. First I will drag and drop sine wave block from sources library
Next from sinks library drag and drop scope block. Connect these blocks and run.
To connect the blocks click
the outport of first block
drag the mouse with left
mouse button and join at the
Inport of second block. These
two blocks will be connected
by a straight line as shown

To simulate press the run button


.After run double click the
Scope block to view output
To simulate power electronic circuits we have to use components in the
Simpowersytems library. For this type >> powerlib at the command prompt

Next we will
Simulate a
Half wave
Rectifier in
Simulink using
powerlib
components
Simulation of a simple half wave rectifier with simulink
As said earlier first you should know which component is available from which Library. For this simulation we use
the following components.
Component Library Path to get and icon
Powerlib Electrical sources AC voltage source
AC voltage source Electrical sources

Diode Power electronics PowerlibPower electronicsdiode

Voltage Measurements PowerlibMeasurementsVoltage measurement


measurement

Resistance-series Elements PowerlibElementsseries RLC branch in that choose only R


RLC branch

Scope sinks
After connection

Set the time to 0.1

After that press the simulation


Button to start simulation

Press Ctrl+S buttons and save


You can drag at the corner to
Increase the size of scope After simulation double click on
scope block. Click the sixth button
from left to get the proper output

In latest version this may differ little


bit
SIMULINK GRAPH – Linking MATLAB with SIMULINK(works up to version R2018a)

SIMULINK a part of MATLAB is mainly used to simulate dynamic systems.

The scope block displays mostly the output in the form of a graph.

The scope display usually do not have xlabel, ylabel, title, legend etc.

Also the display clarity is very poor

Here we are going to learn how to link MATLAB and Simulink and get good graphs out of Simulink

Consider a Simulink model

After you run the model ,double click on scope to see the output waveform. You may look something as

This graph don’t have any details expected of a graph

We will link Simulink and MATLAB and by just double click on a block in Simulink model you will
get good result.
Your Simulink model and the driving program should reside in the same folder which is your current working folder

1. First create a Simulink model and save by a suitable name

2. Next drag and drop a subsystem block form port & subsystems library

3. Double click on the scope block and click on second icon

4. Scope parameters dialog appears. In that click the History tab. Remove the top tick mark and put the tick mark in the
second.

5. Give a suitable name to variable here We have given as x and for format choose as Array

6. Click Apply and then OK button


Create the following MATLAB program to run simulation and get graph

% Simulation of damped sinusoidal oscillator


%
% File name :dsoscs.m
%
sim('dsosc'); % This is the Simulink simulation command with the Simulink file name
%
plot(x(:,1),x(:,2)),xlabel('t(s)'),ylabel('x(t)=5.e^{-0.2t}.sin(\omegat-\pi/2)')
title('Damped Signal Generator'),grid

Save the file as dsoscs . The first column of x is time while second column is actual values
Next double click the subsystem block. When it opened select its contents and delete the same

Return back by
clicking this arrow

Your block will


Look like this

Double click on
subsystem and
enter as
Right click on subsystem block and chose properties. A dialog box opens

Click on the Callbacks tab

Under callback functions list


Click on openfcn as shown

Enter the program file name as shown and close


Next save the Simulink model again

Now double click on the subsystem icon to run Simulink model and get the proper graph as output.
Thank you for your Listening
Give your valuable
feedback to rkar@svce.ac.in

You might also like