所以本质上问题出在Matlab和Python中的eig函数给了我不同的东西。我正在从纸上复制数据,以确认我的数值方法是正确的(所以我知道答案-通过Matlab获得)
我尝试过eigh,但仍无改善。
下面是使用的数据矩阵:
2852 170.380000000000 77.3190000000000 -51.0710000000000 -191.560000000000 105.410000000000 240.950000000000 102.700000000000
2842 169.640000000000 76.6120000000000 -50.3980000000000 -191.310000000000 105.660000000000 240.850000000000 102.960000000000
2838.80000000000 176.950000000000 80.4150000000000 -51.5700000000000 -192.190000000000 104.870000000000 239.700000000000 104.110000000000
2837.40000000000 182.930000000000 88.4070000000000 -54.1410000000000 -194.460000000000 104.230000000000 238.760000000000 105.020000000000
2890.80000000000 167.270000000000 122 -67.7490000000000 -275.150000000000 160.960000000000 248.010000000000 95.9470000000000
2962.10000000000 113.910000000000 177.060000000000 -98.9930000000000 -259.270000000000 80.7860000000000 262.890000000000 80.9180000000000
3013.90000000000 72.9740000000000 225.260000000000 -135.700000000000 -233.520000000000 0.0469300000000000 272.110000000000 71.5160000000000
3026.50000000000 112.420000000000 243.020000000000 -169.460000000000 -218.060000000000 0.0465190000000000 271.250000000000 71.8280000000000
3367.10000000000 -0.310680000000000 479.870000000000 0.494350000000000 -0.603940000000000 -0.147820000000000 282.700000000000 -64.1680000000000
import scipy.io as sc
import math as m
import numpy as np
from numpy import diag, power
from scipy.linalg import expm, sinm, cosm
import matplotlib.pyplot as plt
import pandas as pd
###########################. Import Data from Excel Sheet.
###################################
df = pd.read_excel('DataCompanionMatrix.xlsx', header=None)
data = np.array(df)
###########################. FUNCTION DEFINE.
#################################################
m = data.shape[0]
n = data.shape[1]
x = data[0:-1,:]
y = data[-1,:]
A = np.dot(x,np.transpose(x))
xx = np.dot(x,np.transpose(y))
Co_values = np.dot(np.linalg.pinv(A),xx)
C = np.zeros((n,n))
for i in range(0,n-1):
C[i,i-1] = 1
C[:,n-1] = Co_values
eigV,eigW = np.linalg.eig(C)
print(eigV)
数据是9x8矩阵,x是8x8矩阵,y是1x8数组,A是8x8,C是8x8,co是1x8数组。
在Matlab中,特征值是1x8的复杂特征值数组。在Python中,我得到1x8数组,其中填充了7个零和1个整数。
我希望绘制特征值,它们应该位于单位圆上,这是我在Matlab上完成的。
答案 0 :(得分:0)
在Python中创建的数组C
与在MATLAB中创建的数组不对应。
如果我按如下方式修改您的Python代码,则会得到相同的数组C
和相同的特征值:
C = np.zeros((n,n))
for i in range(0,n-1):
C[i+1,i] = 1 # This is where the differences are!
C[:,n-1] = Co_values