476 56518 MVGR Matlab Tutorial
476 56518 MVGR Matlab Tutorial
by
Dr. M. Venu Gopala Rao, Ph.D,
M.Tech, F.I.E.T.E, L.M.I.S.T.E, I.S.O.I., I.A.E., S.S.I.
What is Matlab?
Matlab is basically a high level language which has
many specialized toolboxes for making things easier
for us
Matlab
High Level
Languages such as
C, Pascal etc.
Assembly
Matlab
m-files
functions
Input
Output
capability
Command
Line
Command
execution like DOS
command window
mat-files
Data
storage
/
loading
Physical Problem
Mathematical Models
Solving Mathematical Equations
Introduction
MATLAB (short for MATrix LABoratory) is a powerful computing
environment that handles anything from simple arithmetic to
advanced data analysis.
At its core is the matrix as its basic data type.
Combined with extensive maths and graphics functions,
complicated calculations can be carried out by specifying only a
few simple instructions.
MATLAB can be used to do anything your scientific calculator
can do, and more.
The installations in the computer labs include tool boxes that
include functions for electrical engineering related tasks, such
as signal processing and system analysis.
Introduction. . . .
The installations in the computer labs include tool boxes that
include functions for electrical engineering related tasks, such
as signal processing and system analysis.
Introduction . . .
Writing your own functions is much like programming in
other languages, except that you have the full resources of
MATLABs functions at your disposal, making for very
compact code.
The MATLAB-6 and above environment has several windows
at your disposal.
Command Window: The main window is the command window,
where you will type all your commands, for small programs.
Desktop Tools
Command Window
type commands
Workspace
view program variables
clear to clear
double click on a variable to see it in the Array Editor
Command History
view past commands
save a whole session using diary
Launch Pad
access tools, demos and documentation
Matlab Screen
Command Window
type commands
Current Directory
View folders and m-files
Workspace
View program variables
Double click on a variable
to see it in the Array Editor
Command History
view past commands
save a whole session
using diary
Command History
* A command history, which contains a list of all the
commands that you have typed in the command
window.
* This makes it easy to cut and paste earlier lengthy
commands, saving you the effort of typing it over
again.
* This window can also display the contents of the
current directory.
The Toolbar
Second Method
The second method is used for creating vectors with
equally spaced elements:
t = 0: 0.1:10;
creates a 1x101 vector with the elements 0, .1, .2, .
3,...,10. Note that the middle number defines the
increment.
If only two numbers are given, then the increment
is set to a default of 1:
For ex. k = 0:10; creates a 1x11 vector with the
elements 0, 1, 2, ..., 10.
Arithmetic Operations
When applying addition, subtraction, multiplication and division
between a scalar (that is a single number) and a vector we use
+, -, *, and / respectively.
Let
Then
a + b gives
a - b gives
In Matlab a
a+2 gives
Algebraic manipulations
Scalar Calculations. The common arithmetic operators used in
spreadsheets and program-ming languages such as BASIC are used in
Matlab'.
In addition a distinction is made between right and left division. The
arithmetic operators are
pi 3.14159265
j imaginary unit, 1
i same as j
Special Matrices
null matrix:
M = [ ];
nxm matrix of zeros:
M = zeros(n,m);
nxm matrix of ones:
M = ones(n,m);
nxn identity matrix:
M = eye(n);
Getting Help
The Help pull-down menu gives you access to all the help pages
included with MATLAB.
Evaluating one-dimensional
functions in Matlab
To evaluate one-dimensional functions in Matlab we need to
specify the points at which we would like our function to be
evaluated at.
We can use either the built-in colon notation to specify the
points t=-1:0.5:1
t = -1.0000 -0.5000 0 0.5000 1.0000
or use linspace( )
t=linspace(-2,4,5) % generates 5 points
% between -2 and 4
t = -2.0000 -0.5000 1.0000 2.5000 4.0000
or use logspace( )
Evaluating two-dimensional
functions in Matlab
To evaluate two-dimensional functions in Matlab we use the
built-in function meshgrid ( ) with the ranges for x and y;
eg:
[x,y]=meshgrid(0.1:0.1:0.3, 0.2:0.1:0.4)
yields the matrices x and y given by:
x=
0.1 0.2 0.3
0.1 0.2 0.3
0.1 0.2 0.3
y=
0.2 0.2 0.2
0.3 0.3 0.3
0.4 0.4 0.4
We may then apply any binary operation to x and y provided that we
preceed each operation with a period.
For example x.*y represents the matrix where each element of x
multiplies each element of y.
Similarly, exp(x.^2+y.^2) represents the matrix resulting from
applying exp( ) to the sum of the squares of each of the elements of x
and y.
Flow Control
if
for
while
break
.
Control Structures
If Statement Syntax
if (Condition_1)
Matlab Commands
elseif (Condition_2)
Matlab Commands
elseif (Condition_3)
Matlab Commands
else
Matlab Commands
end
Control Structures
Some Dummy Examples
for i=1:100
Some Matlab Commands;
end
for i=Index_Array
Matlab Commands
end
for j=1:3:200
Some Matlab Commands;
end
for m=13:-0.2:-21
Some Matlab Commands;
end
for k=[0.1 0.3 -13 12 7 -9.3]
Some Matlab Commands;
end
Control Structures
While Loop Syntax
while (condition)
Matlab Commands
end
Dummy Example
while ((a>3) & (b==5))
Some Matlab Commands;
end
Simple plot
Matlab Graphics
x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y)
xlabel('x = 0:2\pi')
ylabel('Sine of x')
title('Plot of the
Sine Function')
Multiple Graphs
t = 0:pi/100:2*pi;
y1=sin(t);
y2=sin(t+pi/2);
plot(t,y1,t,y2)
grid on
Multiple Plots
t = 0:pi/100:2*pi;
y1=sin(t);
y2=sin(t+pi/2);
subplot(2,2,1)
plot(t,y1)
subplot(2,2,2)
plot(t,y2)
Another useful command is axis, which lets you control the size of the
axes. If I've already created the plot with both sine and cosine
functions, I can resize it by typing
>> axis([-5, 15, -3, 3]);
which changes the plotting window so it looks like this:
k=0:20;
y=binopdf(k,20,0.5);
stem(k,y)
Plotting
plot
stem
grid
xlabel
ylabel
title
subplot
figure
pause
linear plot
discrete plot
add grid lines
add X-axis label
add Y-axis label
add graph title
divide figure window
create new figure window
wait for user response
Random Numbers
x=rand(100,1);
stem(x);
hist(x,100)
Thank You
END