迭代法求方程组的解:
import numpy as np from numpy import * from common_libs import * import matplotlib.pyplot as plt #消元法求原方程组的解 A = mat([[8,-3,2],[4,11,-1],[6,3,12]]) b = mat([20,33,36]) result = linalg.solve(A,b.T) print(result) #迭代法求方程组的解 error = 1.0e-6 steps = 100 n=3 xk = zeros((n,1)) errorlist=[] B0 = mat([[0,3/8,-1/4],[-4/11,0,1/11],[-1/2,-1/4,0]]) f = mat([5/2,3,3]).T for k in range(steps): xk_1 = xk xk = B0*xk + f errorlist.append(linalg.norm(xk - xk_1)) if errorlist[-1] < error: print(k+1) break print(xk) matpts = zeros((2,k+1)) matpts[0] = linspace(1,k+1,k+1) matpts[1] = array(errorlist) drawScatter(plt,matpts) plt.show()