简体   繁体   English

MATLAB中对称矩阵的复特征向量

[英]Complex eigenvectors of a symmetric matrix in MATLAB

I am facing an issue when using MATLAB eig function to compute the eigenvalues and eigenvectors of a symmetric matrix. 当使用MATLAB eig函数计算对称矩阵的特征值和特征向量时,我遇到了一个问题。

The matrix D is 矩阵D是
10x10 10×10
all diagonal elements = 0.45 所有对角元素= 0.45
all off-diagonal elements = -0.05 所有非对角元素= -0.05

When using [vec, val] = eig(D) some of the resulting eigenvectors contain complex numbers (ie 0.3384 + 0.0052i). 当使用[vec,val] = eig(D)时,一些得到的特征向量包含复数(即0.3384 + 0.0052i)。 I have searched online and I found two related posts on similar issue, but did not help me in finding a solution. 我在网上搜索过,我在类似的问题上发现了两个相关帖子,但没有帮助我找到解决方案。

So I tried the same subroutine in Python numpy (numpy.linalg.eigh(D)) and it gave me all real eigenvalues and eigenvectors. 所以我在Python numpy(numpy.linalg.eigh(D))中尝试了相同的子程序,它给了我所有真正的特征值和特征向量。 The results from Python are correct as I was able to verify my final results with a published paper. Python的结果是正确的,因为我能够用已发表的论文验证我的最终结果。

My question is what causes MATLAB to give complex eigenvalues and eigenvectors for a symmetric matrix? 我的问题是什么原因导致MATLAB为对称矩阵赋予复杂的特征值和特征向量? Is there a way around it? 有办法解决吗? I can certainly re-write my algorithm in Python, but I would rather avoid that. 我当然可以用Python重写我的算法,但我宁愿避免这种情况。

Note: if I try 4x4 matrix with all diagonal elements = 0.375 and all off-diagonal elements = -0.125 then MATLAB eig(D) gave all real eigenvalues and eigenvectors. 注意:如果我尝试4x4矩阵,所有对角线元素= 0.375并且所有非对角线元素= -0.125,则MATLAB eig(D)给出所有真实特征值和特征向量。

Thanks in advance for any advice on this issue. 提前感谢您对此问题的任何建议。

Follow up. 跟进。 The code used to generate D and the eigenvalues/vectors: 用于生成D和特征值/向量的代码:

    P = eye(10) - 1/10;
    delta = 1 - eye(10);
    A = -0.5 * delta;

    D = P*A*P;
    [vec val] =eig(D)

I was able to solve the problem using single precision. 我能够使用单精度解决问题。

  P = eye(10) - 1/10;
  delta = 1 - eye(10);
  A = -0.5 * delta;

  D = P*A*P;
  D = single(D)
  [vec val] =eig(D)

The results now are correct. 结果现在是正确的。 Thank you all for taking the time responding to my question and thanks for all your suggestions. 谢谢大家花时间回答我的问题并感谢你的所有建议。 This is really more of a workaround than a solution. 这实际上是一种解决方法而不是解决方案。 I still do not know why double precision causes complex eigenvectors. 我仍然不知道为什么双精度会导致复杂的特征向量。

Doing

D = 0.5 * eye(10) - 0.05 * ones(10);
eig(D)

I get 我明白了

ans =
     -2.08166817117217e-17
                       0.5
                       0.5
                       0.5
                       0.5
                       0.5
                       0.5
                       0.5
                       0.5
                       0.5

which is not too bad. 这不是太糟糕。 The first eigenvalue in the result should obviously be zero, so there's a rounding error, but otherwise the result is as expected. 结果中的第一个特征值显然应该为零,因此存在舍入误差,否则结果与预期一致。 Due to the same issue of limited numerical precision I guess there could eventually be very small complex parts, too, but actually Matlab's eig should detect symmetry and produce only real-valued eigenvalues. 由于数值精度有限的相同问题,我猜最终可能会出现非常小的复杂部分,但实际上Matlab的eig应检测对称性并仅产生实值特征值。

How exactly did you generate your matrix D ? 你究竟是如何生成矩阵D Maybe it has only approximately the structure you are describing? 也许它只有你描述的大致结构?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM