您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > 雅可比迭代法解线性方程组
雅可比迭代法解线性方程组#includestdio.h#includemath.h#includealloc.hJacobidiedai(n,a,b,x)intn;double*a,*b,*x;{inti,j;double*x0,m=0,eps;x0=malloc(n*sizeof(double));for(i=0;in;i++)x0[i]=x[i];while(1){for(i=0;in;i++){for(j=0;jn;j++)m+=a[i*n+j]*x0[j];m-=a[i*n+i]*x0[i];x[i]=(b[i]-m)/a[i*n+i];m=0;}for(i=1;in;i++){eps=fabs(x[0]-x0[0]);if(epsfabs(x[i]-x0[i]))eps=fabs(x[i]-x0[i]);}if(eps1e-3)return1;for(i=0;in;i++)x0[i]=x[i];}free(x0);}main(){inti;doublea[3][3]={{5,2,1},{-1,4,2},{2,-3,10}},b[3]={-12,20,3},x[3]={0,0,0};Jacobidiedai(3,a,b,x);for(i=0;i3;i++)printf(x[%d]=%f\n,i,x[i]);}
本文标题:雅可比迭代法解线性方程组
链接地址:https://www.777doc.com/doc-4864646 .html