Python 中的线性代数:计算 3x3 矩阵的特征向量

问题描述

我正在使用 Python 来导出与 3x3 矩阵中的特征值相关联的特征向量。我的代码返回正确的特征值但错误的特征向量。

A = np.array([[-2,-4,2],[-2,1,[4,2,5]])
print (A)
print ('-------------------------------------------------------')

eigenvalues,eigenvectors = np.linalg.eig(A) # must use this line of code exactly 
print(f'eigenvalues of matrix A are:{eigenvalues}')
print ('-------------------------------------------------------')
print(f'eigenvectors of matrix A are:{eigenvectors}')

enter image description here

例如,与值 6 关联的特征向量应该是 [1,6,16],而不是代码输出内容

解决方法

正确,可以通过第二个特征值和特征向量的特征向量/特征值条件来检查。

enter image description here

其中 u 是特征向量,lambda 是其特征值。 因此,我们将特征向量 v[:,1] 乘以 A,并检查它是否与将相同的特征向量乘以其特征值 w[1] 相同。

>>> w,v = np.linalg.eig(A)
# w contains the eigenvalues. 
# v contains the corresponding eigenvectors,one eigenvector per column. 
# The eigenvectors are normalized so their Euclidean norms are 1
>>> u = v[:,1]
>>> print(u)
[ 0.53452248,-0.80178373,-0.26726124]

>>> lam = w[1]
>>> lam
3.0

>>> print(np.dot(A,u))
[ 1.60356745 -2.40535118 -0.80178373]
>>> print(lam*u)
[ 1.60356745 -2.40535118 -0.80178373]