ECE204
Spring 2002
By Jianwei(Jay) Liu
Outline
• General discussion of MATLAB
– Matrix Laboratory
– How to access to HELP
• Simple calculations and graphs
• For, while, if, and relations
• M-files
• Miscellaneous
– Output format
– Colon notation
– Efficiency in Matlab
Access to Help
• Command line:
help <c o m m a n d - n a m e >
• Example:
help
help pi
help fuzzy
help ans
help help
help elfun % “elfun--elementary math functions.''
Simple calculations and graphs
(1)
• Input vectors, scalars and matrices.
– Examples:
• >> a = 2
• >> x = [1;2;3]
• >> A = [1 2 3;4 5 6;7 8 0]
• F o r e x a m p l e , t h e m a x i m u m e n t r y i n a m a t r i x A i s g i v e n b y m a x ( m a x ( A ))
r a t h e r t h a n m a x ( A). H i n t : T r y t h e “ h e l p m a x ” f i r s t a n d t h e n f o l l o w t h e
illustrations.
Simple calculations and graphs
(5)
• Matrix functions
– Much of MATLAB's power comes from its matrix functions. The most
useful ones are:
eig eigenvalues and eigenvectors
chol cholesky factorization ,
svd singular value decomposition , inv inverse
lu LU factorization , qr QR factorization
hess hessenberg form schur schur decomposition
rref reduced row echelon form expm matrix exponential
sqrtm matrix square root poly characteristic
polynomial
det determinant size size
norm 1-norm, 2-norm, F-norm, infinity-norm
cond condition number in the 2-norm rank rank
• Graphs 2.5
– Examples: 2
1.5
• >> x = [1;2;3;4;5];
1
• >> plot(x,y) or 0
1 1.5 2 2.5 3 3.5 4 4.5 5
• >> plot(x,y,‘*')
– subplot 3
– title 2.5
– 3D graphs
2
1.5
• Parametric Plots 1
• >> t = (0:2*pi/100:2*pi)';
0.5
• > plot(cos(t),sin(t))
0
1 1.5 2 2.5 3 3.5 4 4.5 5
• >> axis('square')
• Subplot
0.5 0.5
– >> t = (0:.1:2*pi)';
0 0
– > > s u b p l o t( 2 , 2 , 1 )
– >> plot(t,sin(t)) -0.5 -0.5
– > > s u b p l o t( 2 , 2 , 2 ) -1 -1
0 2 4 6 8 0 2 4 6 8
– >> plot(t,cos(t))
– > > s u b p l o t( 2 , 2 , 3 ) 500 1
100 0.2
0 0
0 2 4 6 8 0 2 4 6 8
Simple calculations and graphs
(8)
• 3D plots
– >> x = (0:2*pi/20:2*pi)';
– >> y = (0:4*pi/40:4*pi)';
• Next, ``spread'' these grids into two dimensions using meshgrid:
– >> z = cos(X).*cos(2*Y);
• Having created the matrix containing the samples of the function, the surface can be graphed using
either the mesh or the surf commands (see Figures respectively):
if relation
statements
end
– example:
if n < 0
parity = 0;
elseif rem (n,2) == 0
parity = 2;
else
parity = 1
end
• A = [1 2 3 4
5 6 7 8];
– t h e n t h e M A T L A B s t a t e m e n t data w i l l c a u s e t h e a s s i g n m e n t g i v e n i n
data.m t o b e c a r r i e d o u t .
• An M-file can also reference other M-files, including referencing itself
recursively.
M-file (3)
• Function files
–?
•Your Job!
– Hint: pay attentions to the [input…]
and [output…]
Miscellaneous - Output format
• While all computations in MATLAB are performed in double precision, the format of
the displayed output can be controlled by the following commands.
• format short fixed point with 4 decimal places(the default)
• format long fixed point with 14 decimal places
• format short e scientific notation with 4 decimal places
• format long e s c i e n t i f i c n o t a t i o n w i t h 1 5 d e c i m a l p l a c e s
• Once invoked, the chosen format remains in effect until changed.
• T h e c o m m a n d format compact will suppress most blank lines allowing
more information to be placed on the screen or page. It is independent of
the other format commands.
Miscellaneous - Colon Notation (1)
• Vectors and submatrices are often used in MATLAB to achieve fairly complex data
manipulation effects. "Colon notation" ( w h i c h i s u s e d b o t h t o g e n e r a t e v e c t o r s a n d
reference submatrices) and subscripting by vectors are keys to efficient manipulation
of these objects. Creative use of these features permits one to minimize the use of
loops (which slows down MATLAB) and to make code simple and readable. Special
effort should be made to become familiar with them .
• The expression 1:5 is actually the row vector [1 2 3 4 5]. The numbers need not be
integers nor the increment one. For example,
• x = [0.0:0.1:2.0]';
• y = sin(x);
• [x y]
• T r y i t ! N o t e t h a t s i n c e sin o p e r a t e s e n t r y - w i s e , i t p r o d u c e s a v e c t o r y f r o m t h e v e c t o r
x.
Miscellaneous - Colon Notation (2)
• The colon notation can be used to access submatrices of a matrix. For example,
A (1:4,3) is the column vector consisting of the first four entries of the third column of
A.
A colon by itself denotes an entire row or column:
• If x i s a n n - v e c t o r , w h a t i s t h e e f f e c t o f t h e s t a t e m e n t x = x(x:-1:1) ?
Miscellaneous - Efficiency in Matlab
• User-defined Matlab functions are interpreted, not compiled. This means roughly
that when an m-file is executed, each statement is read and then executed, rather than
the entire program being parsed and compiled into machine language . For this reason,
Matlab programs can be much slower than programs written in a language such as
Fortran or C.
• In order to get the most out of Matlab, it is necessary to use built-in functions and
operators whenever possible (so that compiled rather than interpreted code is
executed). For example, the following two command sequences have the same effect:
• >> t = (0:.001:1)';
• >> y=sin(t);
• and
• >> t = (0:.001:1)';
• >> for i=1:length(t)
• y(i) = sin(t(i));
• end
• However, on my computer, the explicit for-loop takes 46 times as long as the
vectorized sine function.
Comparing efficiency of
a l g o r i t h m s : f l o p s a n d etim e
• Two measures of the efficiency of an algorithm are the number of floating point operations
(flops) performed and the elapsed time.
• T h e M A T L A B f u n c t i o n flops keeps a running total of the flops performed. The
c o m m a n d flops(0) (not flops = 0!) will reset flops to 0. Hence entering flops(0)
im m e d i a t e l y b e f o r e e x e c u t i n g a n a l g o r i t h m a n d flops im m e d i a t e l y a f t e r g i v e s t h e f l o p
count for the algorithm.
• t = c l o c k ; x = A \ b ; t i m e = e t i m e( c l o c k , t )
• You may wish to compare this time---and flop count---with that for solving the system
u s i n g x = i n v ( A )* b ; . T r y i t ! O t h e r s r e l a t e d c o m m a n d s a r e tic a n d t o c .
• It should be noted that, on timesharing machine, etime may not be a reliable
measure of the eficiency of an algorithm since the rate of execution depends on how
busy the computer is at that particular time when you execute your program.