艾特肯加速方法加速乘幂法收敛

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
print('乘幂法:')
while err > 0.00000000001:
    y = A * z0
    y1 = y.copy()
    y1 = abs(y1)
    a = y1.argmax()
    z0 = y/y[a]
    if k%10==0:
        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])
z0 = np.mat([1, 1, 1])
z0 = z0.T
err = 1
k = 0
m_r1 =[]
print('艾特肯加速方法:')
while err > 0.00000000001:
    y = A * z0
    if k > 1:
        m = m_r[k] - (m_r[k]-m_r[k-1])**2/(m_r[k]-2*m_r[k-1]+m_r[k-2])
    else:
        m = m_r[k]
    k = k + 1
    z0 = y/m
    m_r1.append(m)
    if k > 2:
        err = abs(m_r1[-1]-m_r1[-2])
    if k % 10==0:
        print(k, ' ', m, ' ', z0.T)

艾特肯加速方法加速乘幂法收敛_第1张图片

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