乘幂法

import numpy as np

z0 = np.mat([1, 1, 1])
z0 = z0.T
err = 1
A = np.mat([[-1,2,1],
            [2,-4,1],
            [1,1,-6]], dtype=float)
m_r = []
k = 0
while err > 0.0000001:
    y = A * z0
    y1 = y.copy()
    y1 = abs(y1)
    a = y1.argmax()
    z0 = y/y[a]
    print(k, end=' ')
    print(y[a], end=' ')
    print(z0.T, end=' ')
    print(' ')
    m_r.append(y[a])
    k = k + 1
    if k > 1:
        err = abs(y[a] - m_r[-2])
print('最大特征值为,', y[a])
print('最大特征向量为为,', z0.T)
print('Ax=', end='')
print((A*z0).T)
print('λx=', end='')
print(y[a]*z0.T)
print('Ax-λx=', (A*z0).T-y[a]*z0.T)

乘幂法_第1张图片
乘幂法_第2张图片

你可能感兴趣的:(数值分析,特征值与特征向量)