Reputation: 955
I have the following code snippet, trying to compute the eigen-decomposition of a real symmetric matrix
K = 3;
n = 10;
Z = rand(n,K);
B = rand(K,K);
B = 0.5*(B+B') + 3*eye(K);
W = Z*B*Z';
if issymmetric(W) && isreal(W)
[U,D] = eig(W)
end
This unfortunately seems to produce genuinely complex eigenvectors on MATLAB R2013a. I used to think that eig
should keep everything real for real symmetric matrices. Anyone has any idea why this happens?
PS. Changing to n = 6, for example, outputs something real.
Upvotes: 1
Views: 1019
Reputation: 21
MATLAB eig
usually returns real eigenvectors when the matrix is real and symmetric. Rounding errors can make Z*B*Z'
slightly unsymmetric. I don't know how issymmetric
is implemented (it's a built-in function), but maybe eig
doesn't use the same criterion to determine if a matrix is real and symmetric than issymmetric
.
A simple way to enforce that the matrix is numerically symmetric is doing (W+W')./2
. So eig((W+W')./2)
should return real values and vectors.
Upvotes: 1