Sie sind auf Seite 1von 6

Matlab Exercises: IP Summer School at UW

Mark Hubenthal
June 2010

1 Basic Matrix Manipulation


A = zeros(10,15) Creates a 10 by 15 zero matrix
A = eye(10) Creates a 10 by 10 identity matrix
A(2,:) Returns row 2 of the matrix A
A(:,4) Returns column 4 of the matrix A
A(r,c) This notation refers to the element of
row r and column c within matrix A.
A(r,:) This notation refers to all of the elements of
row r in matrix A.
A(:,c) This notation refers to all of the elements of
column c in matrix A.
A(m1 : m2 , n1 : n2 ) Returns the submatrix of A between
rows m1 and m2 and columns n1 and n2 .
linspace(x0 , x1 , n) Returns a vector starting at x0 and ending
at x1 with n evenly spaced points
transpose(A) Returns the transpose of A
A’ Returns the conjugate transpose of A

Example 1.
>> A = eye(10);
>> for i=1:6
v(i) = i;
end
>> B = diagonal(v);
>> A(2:7, 2:7) = B;

1
1.1 Useful Matrix Functions
det(A) determinant of the square matrix A
inv(A) inverse of the square matrix A
norm(A) largest singular value of A
rank(A) provides an estimate of the number of linearly independent
rows or columns of a matrix A
trace(A) sum of the diagonal elements of A,
which is also the sum of the eigenvalues of A

1.2 Operations
A+B entrywise addition of matrices
A*B matrix multiplication
A.*B entrywise multiplication of matrices
A∧ n matrix exponentiation only for integers n
A.∧ r entrywise exponentiation of an array defined for r ∈ C
A/B matrix division (think of it as AB −1 )
A./B entrywise division of matrices
A\b solve the system of equations Ax = b

Remark 2. It is always faster to use A\b to solve the system Ax = b rather


than use inv(A)*b.

Remark 3. Since Matlab is a script based language (not compiled), it


doesn’t handle loops as efficiently as other languages like C++. So whenever
possible, it is desirable to avoid them. For example, try to use matrix opera-
tions to carry out an algorithm for all matrix entries at once rather than use
two ”for” loops to cycle through the entries individually.

1.3 Special Matrices


eye(N) the N by N identity matrix
magic(N) an N by N matrix constructed from the integers 1 through N 2
ones(M,N) an M by N matrix of ones
rand(N) an N by N matrix containing pseudo-random values drawn from
the standard uniform distribution on the open interval (0, 1)
zeros(M,N) an M by N matrix of zeros

2
2 Some Loops
The two most basic and common loops to use in Matlab are ”for” loops and
”if” statements. Anyone who has previous experience with another program-
ming language such as C, Fortran, Java, etc, will probably find the syntax
to be familiar. The two examples given should be sufficient to describe their
use:
for i=1:N
A(i) = i∧ 2;
end
if( x > 0 )
y = 7;
else
y=11;
end

2.1 Exercises
1. Create a 10 by 10 matrix A whose (i, j)th entry is equal to i ∗ j in two
ways: using a for loop; using an outer product of two vectors.
2. Set up a discrete 2-D polar space in the variables (r, θ) parameterized
by two vectors, one for r and one for θ. Specifically, have r range from 0 to
1 with stepsize 0.01 and θ range from 0 to 2π with 100 divisions.
3. Using nearest neighbor interpolation and the values of the function
f (x, y) = xy = r2 sin θ cos θ on the polar grid from probem 2, approximate
the values of f to the unit disk at points on the cartesian grid spanned by X
= [0:0.01:1]’ in the x and y-directions.

3 FFT
Given N -vectors x and X, the functions Y = fft(x) and y = ifft(X) im-
plement the transform and inverse transform pair given for vectors of length

3
N by:

X(k) =
X x(j)ω
N
(j−1)(k−1)
N

X X(k)ω
j=1
N
−(j−1)(k−1)
x(j) = (1/N ) N
k=1

where
ωN = e−2πi/N
is an N th root of unity.
Usage:
Y = fft(X)
Y = fft(X,n)
Y = fft(X,[],dim)
Y = fft(X,n,dim)
Taken from Matlab documentation:

Y = fft(X) returns the discrete Fourier transform (DFT) of the


vector X, computed with a fast Fourier transform (FFT) algo-
rithm. If X is a matrix, fft returns the Fourier transform of each
column of the matrix.
If X is a multidimensional array, fft operates on the first nonsin-
gleton dimension.
Y = fft(X,n) returns the n-point DFT. If the length of X is less
than n, X is padded with trailing zeros to length n. If the length
of X is greater than n, the sequence X is truncated. When X
is a matrix, the length of the columns are adjusted in the same
manner.
Y = fft(X,[],dim) and Y = fft(X,n,dim) applies the FFT op-
eration across the dimension dim.

Remark 4. Take note of constants as different definitions of the Fourier


transform may or may not have a factor of some power of 2π included. So
be careful when implementing any algorithms involving the DFT.

Remark 5. For a 2-dimensional DFT see the commands fft2 and ifft2.

4
3.1 Example: Using the 1-D FFT
1
Consider the function f (x) = 1+x 2. We would like to plot f and its 1-
dimensional discrete Fourier transform on a reasonable grid. For a one-
variable function, a grid will just be a N by 1 or 1 by N vector. As stated
in the Matlab documentation, there is more than one command to compute
the discrete fourier transform of f , each having a different level of specificity
with the respect to various parameters. But first, let’s format our data on a
grid.
X = linspace(-1,1,1024);
Y = 1./(1+ X.∧ 2);
We have just created a 1024-point grid with values evenly spaced between
-100 and 100 and created the corresponding vector Y of function values at
each point on the grid (take note of the dots used to denote entry wise opera-
tions on an array). To compute the 1-dimensional discrete fourier transform
of f (x) of length 1024 with respect to this grid we simply input Yhat =
fft(Y);
Now try R = ifft(Yhat); to verify that R = Y . In particular, instead
of inspecting R − Y directly, just put norm(R - Y); and note the result is
on the order of 10( − 16).
Finally, we plot the discrete fourier transform of f (x) with the command
plot( abs(Yhat));. You can also pick out certain components of Yhat, say
the first 100 entries, by inputting plot(abs(Yhat(1:100)));.

3.2 Exercises
4. Consider the function
¨1 |t| < 1
«
g(t) = .
0 otherwise

Compute the g on the grid X = linspace(-1,1,4096) (call this set of func-


tion values Y) and set ghat = fft(Y). Then cut off the frequency content of
ghat by taking only the first 128 entries of ghat and calling it ghattilde.
Finally, compute the inverse discrete fourier transform of length 4096 of
ghattilde and compare the plot of its real part to that of Y . You can
also compute norm(Y - real(ifft(ghattilde))).
5. Using the same setup as in problem 4, define ghatthresh as the vector
obtained from ghat by zeroing out any entries with magnitude less than

5
some fixed positive number thresh. Start with thresh = 0.25 and use
the command ( abs(ghat) > thresh ) to get a vector T with T(j) = 0 if
abs(ghat(j)) <= thresh and 1 otherwise.
Finally, compute the inverse discrete fourier transform of ghatthresh
and compare its real part to g. Try with different values of thresh.

4 Conclusion
There are many more useful bits of information you might want to know
about MATLAB depending on your motivation. If you have a question about
a command, it’s usually helpful to look at the mathworks webpage:
http://www.mathworks.com/access/helpdesk/help/techdoc/index.html

Das könnte Ihnen auch gefallen