[I posted this in math.stackexchange and was told it would be better suited here.]
For a final project in my linear algebra intro, I have been tasked with writing a script that finds the largest and the second largest eigenvectors of a symmetric matrix in MATLAB. For the best possible grade, it must include a function as well. So far, I have been able to get my script to verify that a matrix is symmetric, and am feeling a little bit stuck. I need some guidance for finishing this assignment, as my MATLAB experience is extremely limited.
Here is what I have so far:
prompt = 'Please input a symmetric matrix A.'
A = input(prompt);
if (A == A'),
eig(A)
else
disp('A is not a symmetric matrix. Please input a symmetric matrix.')
end
Note that the script hopefully verifies that A
is symmetric, and I have the eigenvalues for A
, but I am not sure where to go from here to:
- find the eigenvectors
- get the two largest eigenvectors
- write a useful function to fit into the script.
I would be very grateful for any help given. Thanks!
You have a solution for checking for a symmetric matrix.
For the eigenvectors, see the documentation for
eig
as suggested by Luis Mendo, but also the documentation foreigs
, which allows you to requestk
eigenvectors according tosigma
:where
sigma
can be:Using
eigs
with thek
syntax should be marginally easier thaneig
, but either will work.See this page for how to define a function.