NumPy与MATLAB之间的特征向量存在细微差异

NumPy与MATLAB之间的特征向量存在细微差异

本文介绍了NumPy与MATLAB之间的特征向量存在细微差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有刚度矩阵和质量矩阵.我想计算我的结构振动形状和周期(特征值/矢量),所以我为此使用了NumPy.特征值与MATLAB给出的特征值相同,但是当我将特征向量与MATLAB给出的特征值进行比较时,发现一些小的差异(小于1E-10).

这是为什么,如何使两个结果相等?

我试图提高NumPy的精度,但是没有用.

 将numpy导入为np#S刚度矩阵#M质量矩阵w,f = np.linalg.eig(np.linalg.inv(M)@S) 

这是我的结果

来自NumPy的第一个特征向量:

  0.00.00.00.631781280460724-1.4298382510485397e-09-8.755329688057342e-260.73923874001690797.709528714838357e-101.3560471632542145e-24#不同的符号在这里0.233191978673414961.88087901696399e-09-4.7286506166123194e-17#在这里不同的符号 

MATLAB中的第一个特征向量:

  0006.317812804607240e-01-1.429838251048596e-09-8.755233867348009e-267.392387400169076e-017.709528714837307e-10-2.624482888541565e-24%不同的符号在这里2.331919786734153e-011.880879016947830e-098.178753965460107e-17%不同的符号在这里 
解决方案

MATLAB不是Python,因此它们对算法的实现会有所不同.由于MATLAB的版本是封闭源代码,因此除了The MathWorks的员工外,无法确切地说出具体做法.

此外,这两个向量非常接近,以至于它们处于机器精度之内.(差异大约为 4e-24 ,与MATLAB的 eps 1e-16 ).因此,出于所有实际目的,这两个矩阵可以被认为是相等的.如果您想要更高的精度,则很可能需要某种符号或 vpa() 求解器.

NumPy np.linalg.eig() 文档的最后一个示例是:

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
解决方案

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:

这篇关于NumPy与MATLAB之间的特征向量存在细微差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 10:46