-2

[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:

  1. find the eigenvectors
  2. get the two largest eigenvectors
  3. write a useful function to fit into the script.

I would be very grateful for any help given. Thanks!

chappjc
  • 30,359
  • 6
  • 75
  • 132
  • 1
    Hint: `eig` can have _two_ outputs; see the documentation: http://www.mathworks.es/es/help/matlab/ref/eig.html – Luis Mendo Dec 02 '13 at 22:29
  • outside matlab there are BLAS libraries, which is "a family" of libraries offering a defined set of linear algebra operations. http://en.wikipedia.org/wiki/Basic_Linear_Algebra_Subprograms . my hint is about documenting yourself about the algorithms used in BLAS libraries and always start from the definition of the given property, not from the property itself, for example don't think about symmetric matrices, but think when and why a matrix is symmetric. – user2485710 Dec 02 '13 at 22:34
  • 1
    A link to the official documentation (which should be provided with you copy of Matlab as well) has already been posted. Once you've read it, try some more and you are welcome to come back with questions on specific issues. – fuesika Dec 02 '13 at 22:35

1 Answers1

3

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 for eigs, which allows you to request k eigenvectors according to sigma:

eigs(A,k,sigma)

where sigma can be:

'lm' Largest magnitude (default).

'sm' Smallest magnitude. Same as sigma = 0.

For real symmetric problems, the following are also options:

'la' Largest algebraic ('lr' )

'sa' Smallest algebraic ('sr' ) 'be' Both ends (one more from high end if k is odd)

Using eigs with the k syntax should be marginally easier than eig, but either will work.

See this page for how to define a function.

chappjc
  • 30,359
  • 6
  • 75
  • 132
  • Very helpful! If I were to use the format using eigs(A, k, sigma) will give me the two largest eigenvalues, so if I were to assign [A B] = eigs(A,k,sigma) would that give me a matrix where the colums are the k largest eigenvectors? – Heath Huffman Dec 02 '13 at 23:01
  • @HeathHuffman Yes, the columns of `A` would be the `k` largest eigenvectors, and `B` would be the corresponding eigenvalues. – chappjc Dec 03 '13 at 00:20