to
MATLAB
Part 1 For
MENG 375
AUC_MENG 375
Introduction
MATLAB stands for Matrix Laboratory
The window below appears when MATLAB starts
AUC_MENG 375
and
AUC_MENG 375
Basic operations :
How to define a row matrix (1xn):
>>X=[1 0 9 11 5] % define row Matrix 1x5
% each element can be separated by spaces as shown or by
%commas , .
% Here is The MATLAB echo
X=
1 0 9 11 5
NOTES :
(1) The MATLAB will treat all entries after the
% sign as a comment.
(2) To stop the echo add ";"at the end of the line
>> X=[1 0 9 11 5];
AUC_MENG 375
3
7
7
Prof. Dr. Maki K. Habib
>> size(Y)
% get the size of the Matrix
% Here is the Matlab echo
ans=
5 1
% 5 1 => means 5 rows and 1 column
+
Subtract
Multiply
*
Note:Matrix multiplication review is shown in the next
AUC_MENG 375
Prof. Dr. Maki K. Habib
slide
Operation Description
.*
AUC_MENG 375
Example
Matrix
transpose >>C = A
C= 1 4
2 5
3 6
7
8
0
Element
-by./
element
division
Element
-by.^
element
power
AUC_MENG 375
>>inv(A)
%Matlab command to get the inverse
AUC_MENG 375
Example 1
>>a=[1 3 7 2 0]
a=1 3 7 2 0
>>b=[0 3 7 7 8]
b=0 3 7 7 8
% we initialized two matrices a and b
>>c=a.*b % element by element multiplication
c = 0 9 49 14 0
>>d=b' % d is a matrix contains the transpose of b
d= 0
3
7
7
8
>>e=a*d
>> roots(c)
ans=
AUC_MENG 375
0.2878 + 1.4161i
0.2878 - 1.4161i
-1.2878 + 0.8579i
Prof. Dr. Maki K. Habib
-1.2878 - 0.8579i
EXAMPLE 3:
We want to get the polynomial x^2-1 from its roots
-1 , 1
We use the command poly that computes the
polynomial (x-1)(x+1)=0
>> a=[-1 1] %The row matrix represents the roots
a = -1 1
>> poly(a) %The command poly(a) computes the
%polynomial (x-1)(x+1)=0
ans = 1
-1
num = [1 8 16 9 6]
den = [1 6 11 6]
[r,p,k] = residue(num,den)
r =
-6.0000
Constants -4.0000
3.0000
Roots
Residue
p =
-3.0000
-2.0000
-1.0000
k =
1
Function
MATLAB
Command
Plot
Function
Syntax: Plot (variable1,variable2,
marker);
plot variable1 versus variable2
Note: Markers are optional
Note: Markers table is shown next
The plot command can be used to plot two
superimposed plots on the same graph
Syntax:
Plot(variable1,variable2,variable3,variable4)
plot variable1 versus variable2 and variable3
versus 4 on the same graph
AUC_MENG 375
y
r
g
b
k
w
m
c
Line style
Point
circle
X-mark
Plus
Star
Solid
Dotted
Dashdotted
Prof. Dr. Maki K. Habib
Used in
plot
Command
.
O
X
+
*
:
-.
MATLAB
Command
Xlabel
Ylabel
Title
AUC_MENG 375
Function
Syntax: Xlabel(name)
Labels x-axis with the name in between
the quotes
Note: name is a string defined between
the quotes
Syntax: Ylabel(name)
Labels y-axis with the name in between
the quotes
Syntax: title(name)
Labels the graph with the name in
between the quotes
Prof. Dr. Maki K. Habib
MATLAB
Function
Command
grid
Syntax: grid
Adds grid to the graph
Note: grid command has no arguments
gtext
AUC_MENG 375
Syntax: gtext(name)
Labels the graph line with the name in
between the quotes
EXAMPLE 4:
To plot the function:
>> t=0:0.1:5;
% The range of t from 0 to 5 with an increment of 0.1
>> y=1./(t-exp(t)).^3+sin(t);
% defining function y
>> plot(t,y);
% plot t on the horizontal axis versus y on the vertical axis
>> title( Plot of Example); %adds title to the graph
Example 5 output:
AUC_MENG 375
EXAMPLE 5:
Two or more plots can be shown on the
AUC_MENG 375
Note:
Markers are used in the "plot" function for the
purpose of identification.
AUC_MENG 375
Function
plot3
Syntax: plot3(x,y,z)
Plot 3d graphs
It plots x versus y and z
view
AUC_MENG 375
view([1 0 0])
AUC_MENG 375
view([0 0 1])
EXAMPLE 6:
>>t=0:0.01:5; % array from 0 to 5 with step 0.01
>>plot3(t,sin(4*t),cos(4*t));
% plot command plot x versus y versus z
>>xlabel('t');
>>ylabel('sin(4*t)');
>>zlabel('cos(4*t)');
>>grid;
AUC_MENG
375
Prof. Dr. Maki K. Habib
Example 6 output:
>>view([0 1 0])
AUC_MENG 375
AUC_MENG 375
Syntax: surf(f)
Where f is function to be plotted
AUC_MENG 375
EXAMPLE 7:
In this example, we want to plot the function:
z=3sin(y) * cos(x)
AUC_MENG 375
>>[X,Y]=meshgrid(-pi:pi/50:pi,-pi:pi/50:pi)
% generate two arrays from pi to pi with step pi/50
% generate x-y domain where for function z can be plotted
>>Z=3*sin(Y).*cos(X);
>>surf(Z);
%The surfcommand has to be written after meshgrid
%command.
% the function z is plotted at each intersection point of these
%two arrays using the surf command.
>>title(Mesh example) %title the graph
>>Xlabel(x-axis); %label X-axis
>>ylabel(y-axis); %label Y-axis
>>zlabel(z-axis); %label Z-axis
>>grid;
AUC_MENG 375
Example 7 output:
AUC_MENG 375
AUC_MENG 375
Steps:
(1) click on the new m-file icon as shown:
AUC_MENG 375
Note :
You can specify the current working directory where
the file is saved:
AUC_MENG 375
EXAMPLE 8:
A = [1 2; 3 4] %define 2x2 matrix
C=A.*A %perform element by element
multiplication
Notes:
The function is written in an m-file
The function is being called from the command
window by its name as follows..
>>functionname(argument1,argument2)
AUC_MENG 375
EXAMPLE 9:
An m-file function named add_sub that has 2 inputs
x, y and 1 output r, this function performs addition,
Subtraction as follows..
function r=add_sub(x,y)
r(1)=x+y;
r(2)=x-y;
Calling the function will directly give the output
>> add_sub(10,5)
ans =
15 5
AUC_MENG 375