Вот мой код:
Код: Выделить всё
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:
Правильный первый собственный вектор
Подробнее здесь: https://stackoverflow.com/questions/790 ... scillators