很简单,类似简单迭代法。
1 import numpy as np 2 # Jacobi迭代法 3 # 测试数据 增广矩阵 4 data = [[10,-1,-2, 7.2], [-1,10,-2,8.3], [-1,-1,5, 4.2]] 5 print(np.array(data)) 6 # 取初始值为0,0,0 7 x = [[0] for i in range(len(data))] 8 _x = [[0] for i in range(len(data))] 9 print(np.array(x)) 10 # 开始迭代 11 print("开始迭代:") 12 for n in range(10): 13 print("round{}:".format(n)) 14 for i in range(len(data)): 15 _sum = data[i][-1] / data[i][i] 16 for j in range(len(data[0])-1): 17 if i != j: 18 _sum -= (data[i][j] / data[i][i]) * x[j][0] # 用-=来移项 19 _x[i][0] = _sum 20 for i in range(len(x)): 21 x[i][0] = _x[i][0] 22 print(x)
over。