Во-первых, позвольте мне сказать вам, что код
while(1):
code...
break
не имеет смысла. Поскольку это то же самое, что и
code...
Согласно Википедия , вы можете вычислить следующее приближение собственного вектора по следующей формуле:
Ось iteracje
и aproks
- плохой выбор для диаграммы рассеяния, потому что у нас есть векторы. Как мы можем представить вектор точкой? Мы можем выбрать следующий подход:
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('ggplot')
def metoda_potegowa(A,X0,iterations):
fig, ax = plt.subplots(figsize=(15,10))
b_k = X0.copy()
for i, _ in enumerate(range(iterations)):
# calculate the product between A and b_k
Ab_k = np.dot(A, b_k)
# calculate the norm
Ab_k_norm = np.linalg.norm(Ab_k)
# get next aproximation of eigenvector
b_k = Ab_k / Ab_k_norm
# plot the current iteration i
plt.scatter(list(range(1, len(b_k)+1)), b_k.ravel(), label=i)
plt.xlabel('index')
plt.ylabel('value')
# use colors to distinguish iteration
plt.legend()
plt.show()
return b_k
Затем вы можете вызвать функцию с помощью iterations = 20
и построить график результата:
a = np.array([[1, 0, 3], [0, 2, 0], [3, 0, 1]])
x0 = np.array([1,1,1]).transpose()
eigenvector_20 = metoda_potegowa(a,x0,20)