Professional Documents
Culture Documents
Elementary MATLAB & SIMULINK
Elementary MATLAB & SIMULINK
Elementary MATLAB & SIMULINK
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?
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
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
You create a column vector by separating values by semicolon. The single upper ‘
will transpose the vector.
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.
>> 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
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
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
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
>>plot(V,I),xlabel('V(V)'),ylabel('I(A)'),title('Ohm''s Law'),grid 18
20
0.0082
0.0091
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.
Line Colors
>> y1=t.^2; y1 t
2
>> y2=t.^3; y2 t 3
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);
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=[
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.
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
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
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
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
Scope sinks
After connection
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.
Here we are going to learn how to link MATLAB and Simulink and get good graphs out of Simulink
After you run the model ,double click on scope to see the output waveform. You may look something as
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
2. Next drag and drop a subsystem block form port & subsystems library
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
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
Double click on
subsystem and
enter as
Right click on subsystem block and chose properties. A dialog box opens
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