0
votes

So essentially what the problem is the eig function in Matlab and Python are giving me different things. I am reproducing data from a paper in order to confirm my numerical method is correct (So I know the answers- have them via Matlab)

I have tried eigh, still no improvement.

Below is the data matrix used:

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)

The data is a 9x8 matrix, x is a 8x8 matrix, y is a 1x8 array, A is 8x8, C is 8x8, co is 1x8 array.

In Matlab the eigenvalues are an 1x8 array of complex eigenvalues. In Python, I get 1x8 array filled with 7 zeros and 1 integer.

I expect to plot the eigenvalues and they should sit on the unit circle, this I've done on Matlab.

C matrix- matlab and python (both look like this)

Python eigenvalues

Matlab eigenvalues

1
Check the condition number of your matrix; I bet it's huge, making the eigenproblem very ill-posed.Andras Deak
I've re-created your matrix C in Octave, and get the same eigenvalues as you get in MATLAB. Then I've copy-pasted that array to a Python script, and again get the same eigenvalues. Maybe double-check that the array C that you create in Python is the same you have in MATLAB?Cris Luengo

1 Answers

0
votes

The array C you create in Python does not correspond to the one you have in MATLAB.

If I modify your Python code as follows, I get the same array C and the same eigenvalues:

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