Anda di halaman 1dari 44

Introduction Tutorial

to
MATLAB
Part 1 For
MENG 375

AUC_MENG 375

Prof. Dr. Maki K. Habib

Introduction
MATLAB stands for Matrix Laboratory
The window below appears when MATLAB starts

exit MATLAB with the command exit or quit


AUC_MENG 375

Prof. Dr. Maki K. Habib

Matrices can be introduced into MATLAB


in several different ways:
Entered by an explicit list of elements,
Generated by built-in statements
functions,
Created in M-files,
Loaded from external data files.

AUC_MENG 375

Prof. Dr. Maki K. Habib

and

(1)Math functions and matrix


operations:
We deal with data in MATLAB as matrices.
For example,
An array of data X= 1, 0, 9, 11, 5 is a 1x5 matrix,
A scalar number 9 is an 1x1 matrix.

AUC_MENG 375

Prof. Dr. Maki K. Habib

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

Prof. Dr. Maki K. Habib

How to define a column Matrix:


>>Y=[1; 3; 5; 7; 8] %define column Matrix 5x1

%Here is the Matlab echo:


Y= 1
3
5
7
8
AUC_MENG 375

Prof. Dr. Maki K. Habib

How to define a 2 dimensional Matrix


>>Y= [1 2 3; 3 6 7; 11 4 7]
%define 3x3 matrix where each row is separated
%by semicolon
%Here is the Matlab echo
Y=
1 2
3 6
11 4
AUC_MENG 375

3
7
7
Prof. Dr. Maki K. Habib

How to get the size of a Matrix:


>> Y=[1; 3; 5; 7; 8];
% Define column Matrix 5x1

>> size(Y)
% get the size of the Matrix
% Here is the Matlab echo

ans=
5 1
% 5 1 => means 5 rows and 1 column

Note: ans means answer


AUC_MENG 375

Prof. Dr. Maki K. Habib

This table summarizes most Matrices


Example
operations: Operation Description
Addition

+
Subtract

Multiply

*
Note:Matrix multiplication review is shown in the next
AUC_MENG 375
Prof. Dr. Maki K. Habib
slide

Review on matrix multiplication

Note: To multiply two matrices the number of columns of


the first matrix must be equal to number of rows in the
other matrix
AUC_MENG 375

Prof. Dr. Maki K. Habib

Operation Description

.*

AUC_MENG 375

Example

Element by >>C = A.*B


element C= 2 8 18
0 15 42
multiplicatio
63 64 0
n

Matrix
transpose >>C = A

Prof. Dr. Maki K. Habib

C= 1 4
2 5
3 6

7
8
0

Element
-by./
element
division

Element
-by.^
element
power
AUC_MENG 375

Prof. Dr. Maki K. Habib

The inverse of matrix A can be obtained


by the command:
>>inv(the name of the matrix)
Ex:

>>inv(A)
%Matlab command to get the inverse

AUC_MENG 375

Prof. Dr. Maki K. Habib

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

% multiply a[1x5] d[5x1] to give e[1x1]


AUC_MENG 375
Prof. Dr. Maki K. Habib
e= 72

How to get the roots of a Polynomial:


EXAMPLE 2:
We want to get the roots of the polynomial
x4 + 2x3 + 3x2 + 4x + 5=0
The command roots computes the roots of the
polynomial.

>> c=[1 2 3 4 5];


% we write the coefficients of the polynomial
% the row matrix c = [1 2 3 4 5] represents the
%polynomial x4 + 2x3 + 3x2 + 4x + 5=0

>> 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

How to get a Polynomials from its roots:

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

%the output is the coefficient of the polynomial x^2-1


%the coefficient of x^2 is 1, coefficient of x is 0 ,
%and the constant is -1
AUC_MENG 375

Prof. Dr. Maki K. Habib

Partial-fraction expansion function


Example function:
B( s ) s 4 + 8s 3 + 16 s 2 + 9 s + 6
=
A( s )
s 3 + 6 s 2 + 11s + 6
6
4
3
= s+2+
+
+
s + 3 s + 2 s +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

Important matrix functions:


Inverse:
inv(M)
Rank:
rank(M)
Determinant: det(M)
null matrix: M = []
n by m matrix of zeros: M = zeros(n,m)
n by m matrix of ones: M = ones(n,m)
Identity matrix n by n: M = eye(n)

(2) 2d Plotting and 3d Plotting:


MATLAB
Command
Defining
array of
numbers

Function

Syntax: start : step size : end


Ex:
0 :
0.1 : 5
So this defines the array
0,0.1,0.2,0.3..,4.8,4.9,5
Note: this is used in plotting to specify
the domain ( independent variable )
Note: for the function y = f(x)
The domain: the values of the independent variable x
The range: values of y
AUC_MENG 375

Prof. Dr. Maki K. Habib

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

Prof. Dr. Maki K. Habib

Markers used in plot command:


Used in
Color plot
Command
Yellow
Red
Green
Blue
Black
White
Magena
Cyan
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

Prof. Dr. Maki K. Habib

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

>> xlabel(t); ylabel(y) ; grid;


>> gtext(1/(t-exp(t)^3+sin(t))
AUC_MENG 375

Prof. Dr. Maki K. Habib

Example 5 output:

AUC_MENG 375

Prof. Dr. Maki K. Habib

EXAMPLE 5:
Two or more plots can be shown on the

same graph as shown in this example.


In this example, an additional function
"y2 is to be shown with function y1
plotted on the same graph.

AUC_MENG 375

Prof. Dr. Maki K. Habib

>>x=0:0.01:1; %array starting from 0 to 1 with step 0.01


>>y1=x;% y1 is equal to x, a straight line with slope 45
>>y2=x.^2;
>>plot(x,y1,bo',x,y2,gx')
>>grid;
>>xlabel('x')
>>ylabel('y1,y2')
>>title(Example4)

Note:
Markers are used in the "plot" function for the
purpose of identification.
AUC_MENG 375

Prof. Dr. Maki K. Habib

Commands used for 3d plotting:


MATLAB
Command

Function

plot3

Syntax: plot3(x,y,z)
Plot 3d graphs
It plots x versus y and z

view

Syntax: view([1 0 0])


To show the graph from another view
Ex:view([1 0 0])=>Show view parallel to
x axis, ie: perpendicular to y-z plane

AUC_MENG 375

Prof. Dr. Maki K. Habib

Different uses of view command:


view([0 1 0])

view([1 0 0])

AUC_MENG 375

Prof. Dr. Maki K. Habib

view([0 0 1])

3D plotting using plot3 command:


A 3-D plot is shown in the following example,
where two functions sin(4t) , cos(4t) are plotted
as parameter t increases from 0 to 5 at an
increment of 0.01.

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:

Note: we can get different views using the view


command as illustrated below
AUC_MENG 375

Prof. Dr. Maki K. Habib

Add the following line to previous program:


>>view([1 0 0])

>>view([0 1 0])

AUC_MENG 375

Prof. Dr. Maki K. Habib

Commands used for 3d plotting:


MATLAB
Function
Command
meshgrid Syntax: [x,y]=meshgrid(array of
number1,array of number2)
meshgrid function is used to transform
the domain for plotting using the surf
command.
surf

AUC_MENG 375

Syntax: surf(f)
Where f is function to be plotted

Prof. Dr. Maki K. Habib

Meshgrid and surf commands:


At each intersection point between array X and array Y the
function Z is being plotted

AUC_MENG 375

Prof. Dr. Maki K. Habib

3d plotting using surf command :


To generate a 3-D plot using surf command, you
first need to establish the domains of the "x and
y using meshgrid command.
Note:
The meshgrid function is used to transform the
domain for plotting using the surf command.

EXAMPLE 7:
In this example, we want to plot the function:
z=3sin(y) * cos(x)
AUC_MENG 375

Prof. Dr. Maki K. Habib

>>[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

Prof. Dr. Maki K. Habib

Example 7 output:

AUC_MENG 375

Prof. Dr. Maki K. Habib

(3)How to write in an M-file:


We use m-file to:
save set of commands to be executed in a single
step, and
write and save MATLAB functions.
Note :
We can use any text editor program and save the file
as .m extension
AUC_MENG
MENG 476 and
375MENG 375

Prof. Dr. Maki K. Habib

Restrictions when saving the m-file:

We can give any name to the m-file


But:
No spaces are allowed
No dashes or commas or : or

AUC_MENG 375

Prof. Dr. Maki K. Habib

Steps:
(1) click on the new m-file icon as shown:

=> The window below will appear:

AUC_MENG 375

Prof. Dr. Maki K. Habib

Note :
You can specify the current working directory where
the file is saved:

(2) Write the required lines then save and run


the program

AUC_MENG 375

Prof. Dr. Maki K. Habib

Here is a simple program written in an m-file

EXAMPLE 8:
A = [1 2; 3 4] %define 2x2 matrix
C=A.*A %perform element by element
multiplication

Save and run the program


Heres the output:
A =1 2
3 4
C =1 4
9 16
Note:the output appears in the command window.
AUC_MENG 375

Prof. Dr. Maki K. Habib

(4)How to write a MATLAB function.


To define any function here is the syntax:
function [output 1, output 2] = functionname(input1,input2)

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

Prof. Dr. Maki K. Habib

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

Prof. Dr. Maki K. Habib

Anda mungkin juga menyukai