线性方程組的求解
雅可比迭代法的公式如下:
def Jacobi(a, u): #u为精度
n = len(a) #行数
n1 = len(a[0]) #列数
count = 0 #迭代次数
x_k = [0 for i in range(n)] #第k次迭代后的x的值
x_k1 = [0 for i in range(n)] #第k+1次迭代后的x的值
while(1):
for i in range(n):
atmp = 0
for j in range(i):
atmp += a[i][j] * x_k[j]
btmp = 0
for k in range(i+1, n):
btmp += a[i][k] * x_k[k]
x_k1[i] = (a[i][n1-1] - atmp -btmp) / a[i][i]
#迭代结束条件 1-范数作为结束条件
if max(x_k1) - max(x_k) < u:
break
else:
count += 1
for i in range(n):
x_k[i] = x_k1[i]
return x_k1, count
if __name__ == "__main__":
paremeters=[[10,-1,-2,7.2], [-1,10,-2,8.3], [-1,-1,5,4.2]]
print(Jacobi(paremeters,0.000005))