Introduction To Matlab: S P Simon
Introduction To Matlab: S P Simon
Presented by:
S P SIMON
DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING
NATIONAL INSTITUTE OF TECHNOLOGY
TIRUCHIRAPPALLI-620 015
CONTENTS
MATHEMATICS
BASIC PROGRAMMING
GRAPHICS
INTRODUCTION TO SIMULINK
12/08/21 S.P.Simon-EEE-NITT 2
INTRODUCTION TO MATLAB ENVIRONMENT
What is MATLAB?
A language for technical computing
An interactive system whose basic data element is a matrix
MATLAB stands for MATrix LABoratory
Typical Uses
Math and computation
Algorithm development
Modeling, simulation, and prototyping
Data analysis, exploration, and visualization
Scientific and engineering graphics
Application development including GUI
12/08/21 S.P.Simon-EEE-NITT 3
INTRODUCTION TO MATLAB ENVIRONMENT
12/08/21 S.P.Simon-EEE-NITT 4
INTRODUCTION TO MATLAB ENVIRONMENT
12/08/21 S.P.Simon-EEE-NITT 5
INTRODUCTION TO MATLAB ENVIRONMENT
Command
Variable
window
browser
Start Matlab from the
“start” menu
12/08/21 S.P.Simon-EEE-NITT 6
Command history
INTRODUCTION TO MATLAB ENVIRONMENT
12/08/21 S.P.Simon-EEE-NITT 7
INTRODUCTION TO MATLAB ENVIRONMENT
WORKSPACE
save my.dat
clear
clc
who
load my.dat -mat
who
12/08/21 S.P.Simon-EEE-NITT 8
MATHEMATICS
MATRICES IN MATLAB
MATRICES GENERATION
eye - Identity matrix
12/08/21 S.P.Simon-EEE-NITT 9
MATHEMATICS
Given:
Matrix Index
Always starts with 1 (not 0)
12/08/21 S.P.Simon-EEE-NITT 10
MATHEMATICS
MATRICES OPERATIONS
Given A and B:
12/08/21 S.P.Simon-EEE-NITT 11
MATHEMATICS
Given A:
12/08/21 S.P.Simon-EEE-NITT 12
MATHEMATICS
USEFUL FUNCTIONS
Function Description
det Determinant
12/08/21 S.P.Simon-EEE-NITT 13
MATHEMATICS
EXAMPLES
Given B:
12/08/21 S.P.Simon-EEE-NITT 14
MATHEMATICS
>>help function_name
>>lookfor keyword
12/08/21 S.P.Simon-EEE-NITT 15
MATHEMATICS
Given A and B:
USE OF M FILE
Click to create
a new M-File
Extension “.m”
A text file containing script or function or program to run
You can run more than 1 line of commands at a time
12/08/21 S.P.Simon-EEE-NITT 17
MATHEMATICS
12/08/21 S.P.Simon-EEE-NITT 18
MATHEMATICS
12/08/21 S.P.Simon-EEE-NITT 19
MATHEMATICS
TO
RUNADD
THEPATH
M-FILE File – Set Path…
Add Folder…
Select the folder where
you placed your M-File
12/08/21 S.P.Simon-EEE-NITT 20
MATHEMATICS
RUN AGAIN
12/08/21 S.P.Simon-EEE-NITT 21
MATHEMATICS
RUN AGAIN 2
12/08/21 S.P.Simon-EEE-NITT 22
BASIC PROGRAMMING
Works on Conditional
statements
Short-circuited in MATLAB -
once a condition is true, the
sequence terminates.
»if_examp
i, j = imaginary unit
※ Don't use i or j as a variable except for-loop index!!!
12/08/21 S.P.Simon-EEE-NITT 24
BASIC PROGRAMMING
12/08/21 S.P.Simon-EEE-NITT 25
BASIC PROGRAMMING
Similar to other
programming languages
Repeats loop a set
number of times (based N=10;
on index) N=10;
for
for II =
= 1:N
1:N
Can be nested for
for JJ =
= 1:N
1:N
A(I,J)
A(I,J) = = 1/(I+J-1);
1/(I+J-1);
end
end
end
end
12/08/21 S.P.Simon-EEE-NITT 26
BASIC PROGRAMMING
Similar to other
programming languages
I=1;
I=1; N=10;
N=10;
Repeats loop a set
number of times (based while
while I<=N
I<=N
on index) J=1;
J=1;
Can be nested while
while J<=N
J<=N
A(I,J)=1/(I+J-
A(I,J)=1/(I+J-
1);
1);
J=J+1;
J=J+1;
end
end
I=I+1;
I=I+1;
end
end
12/08/21 S.P.Simon-EEE-NITT 27
BASIC PROGRAMMING
ARRAY OPERATION
Density
Density =
= Mass(I,J)/(Length.*Width.*Height);
Mass(I,J)/(Length.*Width.*Height);
Using Loops:
[rows,
[rows, cols]
cols] =
= size(M);
size(M);
for
for II =
= 1:rows
1:rows
for
for JJ =
= 1:cols
1:cols
Density(I,J)
Density(I,J) = = M(I,J)/(L(I,J)*W(I,J)*H(I,J));
M(I,J)/(L(I,J)*W(I,J)*H(I,J));
end
end
end
end
»array_vs_loops
12/08/21 S.P.Simon-EEE-NITT 28
BASIC PROGRAMMING
M-FILE FUNCTIONS
Function name
has the same constraints as variable name
ONE function in ONE file cf) subfunction
file name > function name
The name of the M-file and of the function SHOULD be the same
12/08/21 S.P.Simon-EEE-NITT 29
BASIC PROGRAMMING
function
function yy =
= mean(x)
mean(x)
%
%MEAN
MEANAverage
Averageorormean
meanvalue.
value.
%
%For
Forvectors,
vectors,MEAN(x)
MEAN(x)returns
returnsthe
themean
meanvalue.
value.
Online %
%For
Formatrices,
matrices,MEAN(x)
MEAN(x)isisaarow
rowvector
vector
Help %
%containing
containingthe
themean
meanvalue
valueof ofeach
eachcolumn.
column.
[m,n]
[m,n] =
= size(x);
size(x);
ifif m
m ==
== 11
MATLAB
Code mm== n;
n;
end
end
12/08/21
yy =
= sum(x)/m;
sum(x)/m;
S.P.Simon-EEE-NITT 30
M-FILE FUNCTIONS
local and global variables
function value=tg(x, y)
global Z
x1 = x^2; y1 = y^2; Z1 = Z^2
value=x1+y1+z1; return
save as tg.m
Declare the variable as global in every function that requires access to it
Issue the global command at the top of the M-file (Recommended!!)
global Z
x = 3; y = 4; Z= 5;
tg(x, y)
x, y, Z
save as test.m and run test!
12/08/21 S.P.Simon-EEE-NITT 31
BASIC PROGRAMMING
M-FILE FUNCTIONS
for n = 0:pi/10:pi
m=feval('sin',n);
disp([n m])
end
Creating a Plot
t = 0:pi/100:2*pi;
y = sin(t);
plot(t, y)
plot Command
y3 = sin(t - 0.5);
plot(t, y, t, y2, t, y3)
Subplots
clf
y4 = cos(t);
subplot(2, 2, 1); plot(t, y)
subplot(2, 2, 2); plot(t, y2)
subplot(2, 2, 3); plot(t, y3)
subplot(2, 2, 4); plot(t, y4)
12/08/21 S.P.Simon-EEE-NITT 35
GRAPHICS
Axes
clf
t = -pi:pi/100:pi;
y = sin(t);
plot(t, y)
grid on
axis([-pi pi -1 1])
xlabel('\pi \leq \itt \leq \pi')
ylabel('sin(t)')
plot3(x,y,z)
plots x,y,z in 3-dimentional space
t=(0:pi/10:8*pi);
x=exp(-t/20).*cos(t);
y=exp(-t/20).*sin(t);
z=t;
figure(1); plot3(x,y,z);
xlabel('x'); ylabel('y'); zlabel('z');
view(Az,El)
Specifies the viewing point in 3-dimentional plot
12/08/21 S.P.Simon-EEE-NITT 37
GRAPHICS
meshgrid(x,y)
generates domain for plotting
x=(-5:0.2:5);
y=(-5:0.2:5);
[X,Y] = meshgrid(x,y);
Z= X .* exp(-X.^2 - Y.^2);
size(X); size(Y); size(Z);
mesh(Z)
plots colored meshed 3-D graph
mesh(Z); mesh(X,Y,Z);
12/08/21 S.P.Simon-EEE-NITT 38
GRAPHICS
surf
plots 3-d surface graphs
surf(X,Y,Z)
contour
plots 3-d surface graphs
[cs,h]=contour(Z)
clabel(cs,h);
colorbar;
12/08/21 S.P.Simon-EEE-NITT 39
SIMULINK
INTRODUCTION TO SIMULINK
12/08/21 S.P.Simon-EEE-NITT 40
SIMULINK
STARTING SIMULINK
Type the following at the Matlab command prompt
simulink
The Simulink library will appear
Note that you can also start Simulink by clicking
on the icon on the Matlab toolbar
Select
File – New Model
To create a new area (model) to build signals
and systems
12/08/21 S.P.Simon-EEE-NITT 41
SIMULINK
SIGNALS IN SIMULINK
There are two main libraries for manipulating
signals in Simulink:
Sources: generate a signal
Sinks: display, read or store a signal
12/08/21 S.P.Simon-EEE-NITT 42
SIMULINK
12/08/21 S.P.Simon-EEE-NITT 43
TOOLBOXES
12/08/21 S.P.Simon-EEE-NITT 44
TOOLBOXES
12/08/21 S.P.Simon-EEE-NITT 45
TOOLBOXES
12/08/21 S.P.Simon-EEE-NITT 46
12/08/21 S.P.Simon-EEE-NITT 47