Matlab presentation

Download Report

Transcript Matlab presentation

Matlab Presentation
By Patrick Gloster
28/7/08
Some basics
• Matlab is designed to easily manipulate
vectors and matrices
• You can define a vector by [4;2;6]
• You can define a transposed vector by
[4,2,6]
• You can define a matrix with
[4,2,6;3,2,1;5,3,5]
More basics
• Beware when multiplying matrices in
Matlab
• If you have two matrices A and B, A*B will
perform the matrix multiplication. If instead
you want to multiply element-wise, use
A.*B
What I use Matlab for
•
•
•
•
Writing functions
Creating simulations
Symbolic algebra
Minimizations using Matlab’s inbuilt
functions
Writing functions
• This is done by creating an M-file
• Example:
function out=fun(x,y)
out=x*y-2
• In the command window writing fun(2,5)
and pressing return will give 8
Simulations
• These just consist of generating random
numbers to put into my functions, and
examining the results
• There are two types of random number
generators; rand and randn
• You can generate an n-m array of random
numbers by writing rand(n,m)
Symbolic algebra
• You can define symbolic variables in
Matlab by writing syms x y
• If you then write f=x/y you can use the subs
function to substitute in numbers for x and y
• subs(f, [x,y], [1,2]) will return 0.5
Problems
• Symbolic algebra becomes tricky in Matlab when you
want to define a very large number of symbolic variables
• You don’t want to write syms x1 x2 x3…x300 by hand
• To do this you need
n = 300;
x = sym(zeros(n));
for k = 1:numel(x)
x(k) = sym(sprintf('x%d', k));
end
Matlab’s inbuilt functions
• Matlab has a huge number of functions built
in already, including several minimization
functions
• If possible it is best to use them; they’re fast
• If you can’t use these try to avoid for loops
as this slows down your function
Tips
• The Matlab help menu is very useful, it has
examples covering all of the basics and many
more advanced problems
• If that doesn’t work there is a fantastic website
http://www.mathworks.com/matlabcentral/
• It is usually enough just to search for your
problem on the forum, otherwise you can join
yourself and ask
Cool things in Matlab
• You can create a magic square by just
typing magic(n) where n is the size of the
square you want
• 3D plots look awesome
• Try typing:
load handel;
player=audioplayer(y,Fs);
play(player,[1
(get(player,'SampleRate')*3)])