Small discrepancy in eigenvectors between NumPy and MATLAB [duplicate]

那年仲夏 提交于 2021-02-16 18:07:16

问题


I have stiffness matrix and mass matrix. I want to calculate my structure vibration shapes and period (eigenvalue/vector) so I am using NumPy for this. Eigenvalues are the same as those given by MATLAB, but when I compare the eigenvectors with those given by MATLAB I find some small (smaller than 1E-10) differences.

Why is this, and how can I make the two results equal?

I tried to increase precision of NumPy but it didn't work.

import numpy as np

#S Stiffness Matrix
#M Mass Matrix

w, f = np.linalg.eig(np.linalg.inv(M)@S)

Here are my results

First eigenvector from NumPy:

0.0
0.0
0.0
0.631781280460724
-1.4298382510485397e-09
-8.755329688057342e-26
0.7392387400169079
7.709528714838357e-10
1.3560471632542145e-24 # Different sign here
0.23319197867341496
1.88087901696399e-09
-4.7286506166123194e-17 # Different sign here

First eigenvector from MATLAB:

0
0
0
6.317812804607240e-01
-1.429838251048596e-09
-8.755233867348009e-26
7.392387400169076e-01
7.709528714837307e-10
-2.624482888541565e-24 % Different sign here
2.331919786734153e-01
1.880879016947830e-09
8.178753965460107e-17 % Different sign here

回答1:


MATLAB is not Python, so their implementations of the algorithm will be different. With MATLAB's version being closed source, it's impossible to say exactly how, except for employees of The MathWorks.

Additionally, these two vectors are so close together, that they fall within machine precision. (The difference is approximately 4e-24, with MATLAB's eps being 1e-16). Therefor, for all practical purposes these two matrices can be considered to be equal. If you want higher precision, you'll most likely need some kind of symbolic or vpa() solver.

The NumPy np.linalg.eig() documentation has as last example in the documentation:

Be careful about round-off error!

a = np.array([[1 + 1e-9, 0], [0, 1 - 1e-9]])
# Theor. e-values are 1 +/- 1e-9
w, v = LA.eig(a)
w; v
array([ 1.,  1.])
array([[ 1.,  0.],
      [ 0.,  1.]])


来源:https://stackoverflow.com/questions/54666304/small-discrepancy-in-eigenvectors-between-numpy-and-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!