# Writing a script for finding the largest and second largest eigenvectors of a symmetric matrix.

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 $$1$$. find the eigenvectors, $$2$$. get the two largest eigenvectors, and $$3$$. write a useful function to fit into the script. I would be very grateful for any help given. Thanks!

• This may be better suited for stackoverflow.com/questions/tagged/matlab – Henry Swanson Dec 2 '13 at 22:14
• I'll x-post it there. I appreciate it. – Heath Huffman Dec 2 '13 at 22:17
• Yeah, they'll probably know the specific syntax. If there's not a syntax for getting eigenvectors, you can look at the nullspace of $A - \lambda I$, where $\lambda$ is an eigenvalue. That'll give you the eigenvectors. The other two steps are just MATLAB syntax somehow. – Henry Swanson Dec 2 '13 at 22:21
• To reinforce(?) @HenrySwanson 's Comment, if the Question is about an approach (Matlab: full eigensolution followed by picking out the two largest eigenvalues) already decided upon, it would certainly be ripe for SO. A mathematical (but computational) question would be how to best approximate the two top eigenvalues without getting all of them (such questions are also handled at SciComp.SE). – hardmath Dec 2 '13 at 22:27
• It's the $\large\tt Power\ Method$ algorithm or $\large\tt Von\ Mises$ one which yields the eigenvalue of largest magnitude. Once you get this, you can "remove" that eigenvalue and repeat the algorithm for the next one. See ---> en.wikipedia.org/wiki/Power_iteration – Felix Marin Dec 2 '13 at 23:04
