[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
松弛迭代法是在雅可比迭代法和高斯——赛德尔迭代法的基础上,以w>0为松弛因子,建立迭代格式如下:
即
我们将线性方程组AX=b的系数矩阵A分解成一个对角矩阵D、一个下三角矩阵L和一个上三角矩阵D,即A=D-L-U,则有:
当w=1时,松弛迭代法即为高斯——赛德尔迭代法;当w>1时为超松弛迭代法,当w<1时为低松弛迭代法。
SOR方法收敛的必要条件是:0<w<2。
1. 松弛(SOR)迭代法的matlab代码
function [X0,err]=sor(A,b,X0,w,max1) %输入 -A代表线性方程组AX=b的系数矩阵 %
-b代表线性方程组AX=b右侧的数值 % -X0代表线性方程组AX=b进行松弛迭代法求解的迭代初值 % -w代表松弛因子 % -max1代表迭代的次数 %输出
-X0代表通过松弛迭代法求解线性方程组AX=b的解 [N,N]=size(A); L=-tril(A,-1); U=-triu(A,1); D=A+L+U;
B=inv(D-w*L)*((1-w)*D+w*U); f=inv(D-w*L)*w*b; for k=1:max1 X0=B*X0+f; end
err=abs(norm(A(:,:)*X0(:)-b(:),2))
在命令行窗口中输入:
>> A=[4 -1 1;4 -8 1;-2 1 5];
>> b=[7 -21 15]';
>> X0=[0 0 0]';
>> w=1.2;
>> max1=20;
>> sor(A,b,X0,w,max1)
最后得到的结果如下:
err =
2.3375e-09
ans =
2.0000
4.0000
3.0000
2. 松弛(SOR)迭代法的python代码
import numpy as np def sor(A,b,X0,w,max1): '''A代表线性方程组AX=b的系数矩阵
b代表线性方程组AX=b右边的部分 X0代表高斯—赛德尔迭代的初始值 w代表松弛因子 max1代表迭代的次数''' n=np.shape(A)[0]
L=-np.tril(A,-1) U=-np.triu(A,1) D=A+L+U
B=np.dot(np.linalg.inv(D-w*L),((1-w)*D+w*U)) f=np.dot(np.linalg.inv(D-w*L),w*b)
for i in range(max1): X0=np.dot(B,X0)+f
err=np.linalg.norm(np.dot(A,X0)-b,ord=2) return X0,err n=3 #线性方程组AX=b右边的部分
b=np.array([[7],[-21],[15]]) #线性方程组的系数矩阵
A=np.array([[4,-1,1],[4,-8,1],[-2,1,5]]) #迭代的初值 X0=np.array([[0],[0],[0]])
#松弛因子 w=1.2 #迭代的次数 max1=20 #进行松弛迭代法求解线性方程组AX=b的解 X,err=sor(A,b,X0,w,max1)
#输出由松弛迭代法求得的线性方程组AX=b的解 print("X={}\nerr={}".format(X,err))
最后的输出结果如下:
X=[[2.]
[4.]
[3.]]
err=2.3374567113095046e-09