输入各种特征值聚堆与分散时的矩阵,并应用共轭梯度法,观察迭代次数与聚堆情况的关系。
因为对角矩阵的对角线元素为其特征值,则用对角矩阵讨论较为方便
代码
import numpy as np
def cg(x0, A, b):
r0 = np.dot(A, x0) - b
p0 = -r0
rk = r0
pk = p0
xk = x0
t = 0 #记录迭代次数
while np.linalg.norm(rk) >= 1e-6:
rr = np.dot(rk.T, rk)
ak = rr / np.dot(np.dot(pk.T, A), pk)
xk = xk + ak * pk
rk = rk + ak * np.dot(A, pk)
bk = np.dot(rk.T, rk) / rr
pk = -rk + bk * pk
t += 1
return xk, t
#输入列表,生成以列表为对角元素的对角矩阵
def Diagonal_matrix(D):
n = len(D)
diag = np.zeros((n,n))
for i in range(n):
diag[i][i] = D[i]
return diag
#矩阵对角线元素
D_1 = [1, 1, 1, 1, 1, 6, 7, 8, 9, 10]
D_2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
D_3 = [0.8, 0.9, 1, 1.1, 1.2, 6, 7, 8, 9, 10]
D_4 = [1 - 2*1e-7, 1 - 1e-7, 1, 1 + 1e-7, 1 + 2*1e-7, 6, 7, 8, 9, 10]
D_5 = [1, 1, 1, 2, 2, 2, 3, 3, 3, 10]
#初始值
x0 = np.zeros((10,1))
b = np.ones((10,1))
#生成对角矩阵
diag1 = Diagonal_matrix(D_1)
diag2 = Diagonal_matrix(D_2)
diag3 = Diagonal_matrix(D_3)
diag4 = Diagonal_matrix(D_4)
diag5 = Diagonal_matrix(D_5)
#共轭梯度法迭代
x_1, n_1 = cg(x0, diag1, b)
x_2, n_2 = cg(x0, diag2, b)
x_3, n_3 = cg(x0, diag3, b)
x_4, n_4 = cg(x0, diag4, b)
x_5, n_5 = cg(x0, diag5, b)
n = [n_1, n_2, n_3, n_4, n_5]
#输出
for i in range(5):
print('矩阵',i + 1 ,'的迭代次数为: ', n[i])<br type="_moz">
矩阵1,前5个元素聚堆且都为相同元素
矩阵2,特征值分散
矩阵3,前5个特征值聚堆,但是最大差为0.4 ,而cg法精度为1e-6
矩阵4,前5个特征值聚堆,且相差最大小于1e-6
矩阵5,三聚堆
输出:
分析:
以上就是python共轭梯度法特征值迭代次数讨论的详细内容,更多关于python共轭梯度法迭代的资料请关注python博客其它相关文章!
标签:numpy
Powered By python教程网 鲁ICP备18013710号
python博客 - 小白学python最友好的网站!