SOR迭代法的实现:
def SOR(A,b,x,e,N,w):
xr=[i for i in x]
# 迭代次数为N次
for k in range(1,N+1):
R=0
# 算出每一行的增量
for i in range(len(x)):
sum1=0
for j in range(len(x)):
sum1+=A[i][j];
R1=w*(b[i]-sum1)/A[i][i]
R1=abs(R1)
if R1>R:
R=R1
xr[i]=xr[i]+R1
if R<=e:
for i in range(len(xr)):
print(xr[i],end=" ")
print("已达到最大迭代次数")
for i in range(len(xr)):
print(xr[i],end=" ")
A=[[4,-1,2],[-1,-5,1],[2,1,6]]
b=[1,2,3]
x=[0,0,0]
e=0.1
N=10
w=1.46
SOR(A,b,x,e,N,w)