Gauss-seidel迭代法的思想及C语言编程

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

Gauss-seidel迭代法1.1Gauss-seidel迭代法的算法知识(1)Gauss-seidel迭代法的来由Gauss-seidel迭代法是对Jacobi迭代法的改进,在Jacobi迭代的分量形式中,为了在每一次迭代计算不同的分量时,可以利用前面已经计算出来的分量,就将Jacobi迭代的分量形式修改为:1,1(1)()()()()131121123111111111112,1(1)(1)()()()232212213122222222221,1(1)(111,1nkkkkknnnnkkkkknnnnkknnnaaaabxxxxxaaaaaaaaabxxxxxaaaaaaxxa−+−−++−−+−−−=−−−−−+=−−−−−+=−1,21,21,1)(1)(1)()1221,11,11,11,1,2,1(1)(1)(1)(1)(1)121221nnnnnkkknnnnnnnnnnnnnnnkkkkknnnnnnnnnnnnnnnnaaabxxxaaaaaaaabxxxxxaaaaa−−−−+++−−−−−−−−−−−−+++++−−⎧⎪⎪⎪⎪⎪⎪⎨⎪⎪−−−−+⎪⎪⎪=−−−−−+⎪⎩1(1)(1)()11()/inkkkiiijjijjiijjixbaxaxa−++==+=−−∑∑即这样,每一个计算出来的分量(1)kix+马上就可以用于计算下一个分量(1)1kix++,这种方法比Jacobi迭代法收敛得快。(2)Gauss-seidel迭代法的矩阵形式的推导由1,1(1)()()()()131121123111111111112,1(1)(1)()()()232212213122222222221,1(1)(111,1nkkkkknnnnkkkkknnnnkknnnaaaabxxxxxaaaaaaaaabxxxxxaaaaaaxxa−+−−++−−+−−−=−−−−−+=−−−−−+=−1,21,21,1)(1)(1)()1221,11,11,11,1,2,1(1)(1)(1)(1)(1)121221nnnnnkkknnnnnnnnnnnnnnnkkkkknnnnnnnnnnnnnnnnaaabxxxaaaaaaaabxxxxxaaaaa−−−−+++−−−−−−−−−−−−+++++−−⎧⎪⎪⎪⎪⎪⎪⎨⎪⎪−−−−+⎪⎪⎪=−−−−−+⎪⎩(1)(1)11121,11(1)(1)212222(1)(1)1,11,21,111(1)(1)12,100000kknkkkknnnnnnkknnnnnnnnaaaxxaaxxaaaxxaaaaxx++++++−−−−−−++−⎛⎞⎛⎞−−⎛⎞⎛⎞⎜⎟⎜⎟⎜⎟⎜⎟−⎜⎟⎜⎟⎜⎟⎜⎟⎜⎟⎜⎟⎜⎟⎜⎟⇔=+⎜⎟⎜⎟⎜⎟⎜⎟−−⎜⎟⎜⎟⎜⎟⎜⎟⎜⎟⎜⎟⎜⎟⎜⎟−−−⎝⎠⎝⎠⎝⎠⎝⎠##%%##()1111()22,122()11,1()(1)(1)()(1)()(1)1()1000()()()knknnknnnnknnkkkkkkkbaxbaaxbaxbxDxLxUxbDLxUxbxDLUxDLb−−−−−++++−−−⎛⎞⎛⎞⎛⎞⎜⎟⎜⎟⎜⎟−−⎜⎟⎜⎟⎜⎟⎜⎟⎜⎟⎜⎟+⎜⎟⎜⎟⎜⎟−⎜⎟⎜⎟⎜⎟⎜⎟⎜⎟⎜⎟⎝⎠⎝⎠⎝⎠⇔=++⇔−=+⇔=−+−#%###11=(),()JJBxgBDLUgDLb−−=+−=−其中1.2Gauss-seidel迭代法的C语言编程下面是Gauss-seidel迭代的C语言编程实例,程序功能是求解线性方程组Ax=b,要求迭代精度小于10-6。其中123832204111,,33631236xAxxbx−⎡⎤⎡⎤⎡⎤⎢⎥⎢⎥⎢⎥=−==⎢⎥⎢⎥⎢⎥⎢⎥⎢⎥⎢⎥⎣⎦⎣⎦⎣⎦#includestdio.h#includemath.h#includeiostream.h#defineN3doubleCompare(doublea[N],doubleb[N]){doublec=0;inti;for(i=0;i=N-1;i++)c+=fabs(a[i]-b[i]);returnc;}voidGauss_seidel(doubleA[N][N],doublex[N],doubleb[N],doubleprecesion){inti,j,k;doublex2[N],x3[N],sum;for(i=0;i=N-1;i++){x2[i]=x[i];x3[i]=x[i];}k=1;//k为迭代次数while(1){for(i=0;i=N-1;i++){sum=0;for(j=0;j=N-1;j++){if(j!=i)sum+=A[i][j]*x2[j];}x[i]=(b[i]-sum)/A[i][i];x2[i]=x[i];}//输出每一次迭代的结果printf(第%d次迭代:\n,k);printf(x3=);for(i=0;i=N-1;i++)printf(%lf,x3[i]);printf(\n);printf(x=);for(i=0;i=N-1;i++)printf(%lf,x[i]);printf(\n);//判断是否达到迭代精度if(Compare(x3,x)=precesion){printf(达到迭代精度的方程组的解为:\n);printf(x=);for(i=0;i=N-1;i++)printf(%lf,x[i]);printf(\n);break;}else{for(i=0;i=N-1;i++)x3[i]=x[i];k++;continue;}}}voidmain(){doubleA[N][N]={{8,-3,2},{4,11,-1},{6,3,12}},x[N]={0},b[N]={20,33,36};Gauss_seidel(A,x,b,1e-6);}

1 / 3
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功