振荡器链的特征向量

问题描述 投票:0回答:1

我正在尝试使用 python 中的 numpy 查找并表示一系列振荡器(弹簧)的特征向量。问题是得到的结果并不完全是预期的结果。

这是我的代码:

import numpy as np
import matplotlib.pyplot as plt

def matrix_creation(dimension,h,k,l):
    #This function creates the tridiagonal matrix

    matrix=np.zeros((dimension,dimension)) #this specifies the size of the rows and columns


    matrix[0][0]=h
    matrix[0][1]=k
    matrix[dimension-1][dimension-1]=l
    matrix[dimension-1][dimension-2]=k

    for j in range(1,dimension-1):
        matrix[j][j-1]=h
        matrix[j][j]=k
        matrix[j][j+1]=l

    return matrix


def plot_eigenvectors(rest_position, eigenvectors, N):
    # Plot each eigenvector
    for i in range(4):
        plt.plot(rest_position, eigenvectors[:,i], label=f'Eigenvector {i+1}')
        plt.title('Eigenvectors at time t=0')
        plt.xlabel('Rest Position (x)')
        plt.ylabel('Eigenvector Value (y)')
        plt.legend()
        plt.grid(True)
        plt.show()
    




def main():
    N=50
    spring_tension=4
    mass=1
    h=-spring_tension/mass
    k=2*spring_tension/mass
    l=h


    matrix=matrix_creation(N,h,k,l)
    #print(np.linalg.eigvals(matrix))
    #to acces al the information we can use the following comand
    eigenvalues,eigenvectors=np.linalg.eig(matrix)
    rest_position=[a for a in range(1,N+1)]
    #plot_eigenvectors(rest_position,eigenvectors,N)

if __name__=="__main__":
    main()

在第一张图片中,我们可以看到运行我的代码的结果。使用我的代码表示第一个特征向量

这显然是错误的,因为我们应该有任何负值。我们可以在图 2 中看到正确解决方案的示例: 正确的第一特征向量

python linear-algebra physics eigenvector
1个回答
0
投票

这看起来不像振荡器的第一个特征向量,它看起来像一个高阶特征向量......

这可能是因为

np.linalg.eig
不对特征值/特征向量进行排序。

我假设“第一个”特征向量是指按特征值的递增顺序排序,在这种情况下您可能想要使用

eigenvalues,eigenvectors=np.linalg.eig(matrix)
eigenvalue_order = np.argsort(eigenvalues)
sorted_eigenvalues = eigenvalues[eigenvalue_order]
sorted_eigenvectors = eigenvectors[:, eigenvalue_order]
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.