Intro to Matlab 2011
-
Upload
islam-ashraf -
Category
Documents
-
view
389 -
download
0
Transcript of Intro to Matlab 2011
![Page 1: Intro to Matlab 2011](https://reader035.fdocuments.net/reader035/viewer/2022081413/54774643b4af9f81108b474d/html5/thumbnails/1.jpg)
1
Introduction to MATLABLec. Ass: Howida youssry
Pattern Recognition (CS451)
![Page 2: Intro to Matlab 2011](https://reader035.fdocuments.net/reader035/viewer/2022081413/54774643b4af9f81108b474d/html5/thumbnails/2.jpg)
MATLAB
MATLAB is a program for doing numerical computation. It was originally designed for solving linear algebra type problems using matrices. It’s name is derived from MATrix LABoratory.
MATLAB has since been expanded and now has built-in functions for solving problems requiring data analysis, signal processing, optimization, and several other types of scientific computations. It also contains functions for 2-D and 3-D graphics and animation.
![Page 3: Intro to Matlab 2011](https://reader035.fdocuments.net/reader035/viewer/2022081413/54774643b4af9f81108b474d/html5/thumbnails/3.jpg)
MATLAB
The MATLAB environment is command oriented somewhat like UNIX. A prompt appears on the screen and a MATLAB statement can be entered. When the <ENTER> key is pressed, the statement is executed, and another prompt appears.
If a statement is terminated with a semicolon ( ; ), no results will be displayed. Otherwise results will appear before the next prompt.
The following slide is the text from a MATLAB screen.
![Page 4: Intro to Matlab 2011](https://reader035.fdocuments.net/reader035/viewer/2022081413/54774643b4af9f81108b474d/html5/thumbnails/4.jpg)
![Page 5: Intro to Matlab 2011](https://reader035.fdocuments.net/reader035/viewer/2022081413/54774643b4af9f81108b474d/html5/thumbnails/5.jpg)
![Page 6: Intro to Matlab 2011](https://reader035.fdocuments.net/reader035/viewer/2022081413/54774643b4af9f81108b474d/html5/thumbnails/6.jpg)
![Page 7: Intro to Matlab 2011](https://reader035.fdocuments.net/reader035/viewer/2022081413/54774643b4af9f81108b474d/html5/thumbnails/7.jpg)
![Page 8: Intro to Matlab 2011](https://reader035.fdocuments.net/reader035/viewer/2022081413/54774643b4af9f81108b474d/html5/thumbnails/8.jpg)
![Page 9: Intro to Matlab 2011](https://reader035.fdocuments.net/reader035/viewer/2022081413/54774643b4af9f81108b474d/html5/thumbnails/9.jpg)
![Page 10: Intro to Matlab 2011](https://reader035.fdocuments.net/reader035/viewer/2022081413/54774643b4af9f81108b474d/html5/thumbnails/10.jpg)
MATLAB
To get started, type one of these commands: helpwin, helpdesk, or demo
» a=5;
» b=a/2
b =
2.5000
»
![Page 11: Intro to Matlab 2011](https://reader035.fdocuments.net/reader035/viewer/2022081413/54774643b4af9f81108b474d/html5/thumbnails/11.jpg)
MATLAB Variable Names
Variable names ARE case sensitive
Variable names can contain up to 63 characters (as of MATLAB 6.5 and newer)
Variable names must start with a letter followed by letters, digits, and underscores.
![Page 12: Intro to Matlab 2011](https://reader035.fdocuments.net/reader035/viewer/2022081413/54774643b4af9f81108b474d/html5/thumbnails/12.jpg)
MATLAB Special Variables
ans Default variable name for results
pi Value of eps Smallest incremental number
inf Infinity
NaN Not a number e.g. 0/0
i and j i = j = square root of -1
realmin The smallest usable positive real number
realmaxThe largest usable positive real number
![Page 13: Intro to Matlab 2011](https://reader035.fdocuments.net/reader035/viewer/2022081413/54774643b4af9f81108b474d/html5/thumbnails/13.jpg)
MATLAB Math & Assignment Operators
Power ^ or .^ a^b or a.^b
Multiplication * or .* a*b or a.*b
Division / or ./ a/b or a./b
or \ or .\ b\a or b.\a
NOTE: 56/8 = 8\56
- (unary) + (unary)
Addition + a + b
Subtraction - a - b
Assignment = a = b (assign b to a)
![Page 14: Intro to Matlab 2011](https://reader035.fdocuments.net/reader035/viewer/2022081413/54774643b4af9f81108b474d/html5/thumbnails/14.jpg)
Other MATLAB symbols
>> prompt
. . . continue statement on next line
, separate statements and data
% start comment which ends at end of line
; (1) suppress output
(2) used as a row separator in a matrix
: specify range
![Page 15: Intro to Matlab 2011](https://reader035.fdocuments.net/reader035/viewer/2022081413/54774643b4af9f81108b474d/html5/thumbnails/15.jpg)
MATLAB Matrices
MATLAB treats all variables as matrices. For our purposes a matrix can be thought of as an array, in fact, that is how it is stored.
Vectors are special forms of matrices and contain only one row OR one column.
Scalars are matrices with only one row AND one column
![Page 16: Intro to Matlab 2011](https://reader035.fdocuments.net/reader035/viewer/2022081413/54774643b4af9f81108b474d/html5/thumbnails/16.jpg)
MATLAB Matrices
A matrix with only one row AND one column is a scalar. A scalar can be created in MATLAB as follows:
» a_value=23
a_value =
23
![Page 17: Intro to Matlab 2011](https://reader035.fdocuments.net/reader035/viewer/2022081413/54774643b4af9f81108b474d/html5/thumbnails/17.jpg)
![Page 18: Intro to Matlab 2011](https://reader035.fdocuments.net/reader035/viewer/2022081413/54774643b4af9f81108b474d/html5/thumbnails/18.jpg)
![Page 19: Intro to Matlab 2011](https://reader035.fdocuments.net/reader035/viewer/2022081413/54774643b4af9f81108b474d/html5/thumbnails/19.jpg)
![Page 20: Intro to Matlab 2011](https://reader035.fdocuments.net/reader035/viewer/2022081413/54774643b4af9f81108b474d/html5/thumbnails/20.jpg)
![Page 21: Intro to Matlab 2011](https://reader035.fdocuments.net/reader035/viewer/2022081413/54774643b4af9f81108b474d/html5/thumbnails/21.jpg)
![Page 22: Intro to Matlab 2011](https://reader035.fdocuments.net/reader035/viewer/2022081413/54774643b4af9f81108b474d/html5/thumbnails/22.jpg)
![Page 23: Intro to Matlab 2011](https://reader035.fdocuments.net/reader035/viewer/2022081413/54774643b4af9f81108b474d/html5/thumbnails/23.jpg)
![Page 24: Intro to Matlab 2011](https://reader035.fdocuments.net/reader035/viewer/2022081413/54774643b4af9f81108b474d/html5/thumbnails/24.jpg)
![Page 25: Intro to Matlab 2011](https://reader035.fdocuments.net/reader035/viewer/2022081413/54774643b4af9f81108b474d/html5/thumbnails/25.jpg)
![Page 26: Intro to Matlab 2011](https://reader035.fdocuments.net/reader035/viewer/2022081413/54774643b4af9f81108b474d/html5/thumbnails/26.jpg)
![Page 27: Intro to Matlab 2011](https://reader035.fdocuments.net/reader035/viewer/2022081413/54774643b4af9f81108b474d/html5/thumbnails/27.jpg)
![Page 28: Intro to Matlab 2011](https://reader035.fdocuments.net/reader035/viewer/2022081413/54774643b4af9f81108b474d/html5/thumbnails/28.jpg)
![Page 29: Intro to Matlab 2011](https://reader035.fdocuments.net/reader035/viewer/2022081413/54774643b4af9f81108b474d/html5/thumbnails/29.jpg)
![Page 30: Intro to Matlab 2011](https://reader035.fdocuments.net/reader035/viewer/2022081413/54774643b4af9f81108b474d/html5/thumbnails/30.jpg)
![Page 31: Intro to Matlab 2011](https://reader035.fdocuments.net/reader035/viewer/2022081413/54774643b4af9f81108b474d/html5/thumbnails/31.jpg)
Plot function
>> a=[ 3 4 5;3 6 7;6 7 8]
a =
3 4 5 3 6 7 6 7 8
>> plot(a)
![Page 32: Intro to Matlab 2011](https://reader035.fdocuments.net/reader035/viewer/2022081413/54774643b4af9f81108b474d/html5/thumbnails/32.jpg)
Reading Data from files MATLAB supports reading an entire file and creating a matrix
of the data with one statement.
>> load mydata.dat; % loads file into matrix.
% The matrix may be a scalar, a vector, or a
% matrix with multiple rows and columns. The
% matrix will be named mydata.
>> size (mydata) % size will return the number
% of rows and number of
% columns in the matrix
>> length (myvector) % length will return the total
% no. of elements in myvector
![Page 33: Intro to Matlab 2011](https://reader035.fdocuments.net/reader035/viewer/2022081413/54774643b4af9f81108b474d/html5/thumbnails/33.jpg)
Plotting with MATLAB MATLAB will plot one vector vs. another. The first one will
be treated as the abscissa (or x) vector and the second as the ordinate (or y) vector. The vectors have to be the same length.
MATLAB will also plot a vector vs. its own index. The index will be treated as the abscissa vector. Given a vector “time” and a vector “dist” we could say:
>> plot (time, dist) % plotting versus time
>> plot (dist) % plotting versus index
![Page 34: Intro to Matlab 2011](https://reader035.fdocuments.net/reader035/viewer/2022081413/54774643b4af9f81108b474d/html5/thumbnails/34.jpg)
Plotting with MATLAB There are commands in MATLAB to "annotate" a plot to put
on axis labels, titles, and legends. For example:
>> % To put a label on the axes we would use:
>> xlabel ('X-axis label')
>> ylabel ('Y-axis label')
>> % To put a title on the plot, we would use:
>> title ('Title of my plot')
![Page 35: Intro to Matlab 2011](https://reader035.fdocuments.net/reader035/viewer/2022081413/54774643b4af9f81108b474d/html5/thumbnails/35.jpg)
Plotting with MATLAB
Vectors may be extracted from matrices. Normally, we wish to plot one column vs. another. If we have a matrix “mydata” with two columns, we can obtain the columns as a vectors with the assignments as follows:
>> first_vector = mydata ( : , 1) ; % First column
>> second_vector = mydata ( : , 2) ; % Second one
>> % and we can plot the data
>> plot ( first_vector , second_vector )
![Page 36: Intro to Matlab 2011](https://reader035.fdocuments.net/reader035/viewer/2022081413/54774643b4af9f81108b474d/html5/thumbnails/36.jpg)
Some Useful MATLAB commands
who List known variables whos List known variables plus their size help >> help sqrt Help on using sqrt lookfor >> lookfor sqrt Search for
keyword sqrt in m-files what >> what a: List MATLAB files in a: clear Clear all variables from work space clear x y Clear variables x and y from work space clc Clear the command window
![Page 37: Intro to Matlab 2011](https://reader035.fdocuments.net/reader035/viewer/2022081413/54774643b4af9f81108b474d/html5/thumbnails/37.jpg)
Some Useful MATLAB commands
what List all m-files in current directory dir List all files in current directory ls Same as dir type test Display test.m in command window delete test Delete test.m cd a: Change directory to a: chdir a: Same as cd pwd Show current directory which test Display directory path to ‘closest’
test.m
![Page 38: Intro to Matlab 2011](https://reader035.fdocuments.net/reader035/viewer/2022081413/54774643b4af9f81108b474d/html5/thumbnails/38.jpg)
A Useless, But Interesting, MATLAB command
why In case you ever needed a reason
![Page 39: Intro to Matlab 2011](https://reader035.fdocuments.net/reader035/viewer/2022081413/54774643b4af9f81108b474d/html5/thumbnails/39.jpg)
MATLAB Relational Operators MATLAB supports six relational operators.
Less Than <
Less Than or Equal <=
Greater Than >
Greater Than or Equal >=
Equal To ==
Not Equal To ~=
![Page 40: Intro to Matlab 2011](https://reader035.fdocuments.net/reader035/viewer/2022081413/54774643b4af9f81108b474d/html5/thumbnails/40.jpg)
MATLAB Logical Operators
MATLAB supports three logical operators.
not ~ % highest precedence
and & % equal precedence with or
or | % equal precedence with and
![Page 41: Intro to Matlab 2011](https://reader035.fdocuments.net/reader035/viewer/2022081413/54774643b4af9f81108b474d/html5/thumbnails/41.jpg)
MATLAB Logical Functions MATLAB also supports some logical functions.
xor (exclusive or) Ex: xor (a, b)
Where a and b are logical expressions. The xor operator evaluates to true if and only if one expression is true and the other is false. True is returned as 1, false as 0.
any (x) returns 1 if any element of x is nonzero
all (x) returns 1 if all elements of x are nonzero
isnan (x) returns 1 at each NaN in x
isinf (x) returns 1 at each infinity in x
finite (x) returns 1 at each finite value in x
![Page 42: Intro to Matlab 2011](https://reader035.fdocuments.net/reader035/viewer/2022081413/54774643b4af9f81108b474d/html5/thumbnails/42.jpg)
Matlab Selection Structures
An if - elseif - else structure in MATLAB.
Note that elseif is one word.
if expression1 % is true
% execute these commands
elseif expression2 % is true
% execute these commands
else % the default
% execute these commands
end
![Page 43: Intro to Matlab 2011](https://reader035.fdocuments.net/reader035/viewer/2022081413/54774643b4af9f81108b474d/html5/thumbnails/43.jpg)
MATLAB Repetition Structures
A for loop in MATLAB for x = array for ind = 1:100
b(ind)=sin(ind/10) end
Alternative: x=0.1:0.1:10; b=sin(x); - Most of the loops can be avoided!!!
A while loop in MATLAB while expression while x <= 10
% execute these commands end
![Page 44: Intro to Matlab 2011](https://reader035.fdocuments.net/reader035/viewer/2022081413/54774643b4af9f81108b474d/html5/thumbnails/44.jpg)
Scalar - Matrix Addition» a=3;
» b=[1, 2, 3;4, 5, 6]
b =
1 2 3
4 5 6
» c= b+a % Add a to each element of b
c =
4 5 6
7 8 9
![Page 45: Intro to Matlab 2011](https://reader035.fdocuments.net/reader035/viewer/2022081413/54774643b4af9f81108b474d/html5/thumbnails/45.jpg)
Scalar - Matrix Subtraction
» a=3;
» b=[1, 2, 3;4, 5, 6]
b =
1 2 3
4 5 6
» c = b - a %Subtract a from each element of b
c =
-2 -1 0
1 2 3
![Page 46: Intro to Matlab 2011](https://reader035.fdocuments.net/reader035/viewer/2022081413/54774643b4af9f81108b474d/html5/thumbnails/46.jpg)
Scalar - Matrix Multiplication
» a=3;
» b=[1, 2, 3; 4, 5, 6]
b =
1 2 3
4 5 6
» c = a * b % Multiply each element of b by a
c =
3 6 9
12 15 18
![Page 47: Intro to Matlab 2011](https://reader035.fdocuments.net/reader035/viewer/2022081413/54774643b4af9f81108b474d/html5/thumbnails/47.jpg)
Scalar - Matrix Division
» a=3;
» b=[1, 2, 3; 4, 5, 6]
b =
1 2 3
4 5 6
» c = b / a % Divide each element of b by a
c =
0.3333 0.6667 1.0000
1.3333 1.6667 2.0000