计算方法用雅可比迭代法求解线性方程组上机报告|上机实验实习报告|计算机上机实习报告
源代码
//用雅可比迭代法求解线性方程组
10x(1)-x(2)-2x(3)=7.2
-x(1)+10x(2)+2x(3)=8.3
-x(1)-x(2)+5x(3)=4.2
////////////////////////
# include <math.h>
# define eps 0.5e-4 //给定精度//
# define n 3 //方程组的阶数//
static double aa[n][n]={{10,-1,-2},{-1,10,2},{-1,-1,5}};
static double bb[n]={7.2,8.3,4.2};
main()
{int i,j,NO;
double a[n+1][n+1],b[n+1],x[n+1];
int jcb();
for (i=1;i<=n;i++)
{for (j=1;j<=n;j++)
a[i][j]=aa[i-1][j-1];
b[i]=bb[i-1];
}
printf("\n please enter NO:");
scanf("%d",&NO); //输入最大迭代次数//
j=jcb(a,b,x,eps,NO); //调用jcb求解方程组//
if(j!=0) {for (i=1;i<=n;i++) printf("x[&d]=%f",i,x[i]);}
}
int jcb(a,b,x,eps,NO)
{int NO;
double a[][n+1],b[],x[],eps;
int i,j,k=0;
double x1[n+1],d,s,max;
for (i=1;i<=n;i++) x[i]=0;
do
{max=0.0;
for (i=1;i<=n;i++)
{s=0.0;
for (j=1;j<=n;j++) {if (j!=i) s=s+a[i][j]*x[j];}
x1[i]=(b[i]-s)/a[i][i];
d=fabs(x1[i]-x[i]);
if(max<d) max=d;
}
for(i=1;i<=n;i++) x[i]=x1[i]; k++;
}
while ((max>=eps)&&(k<NO));
printf("k=%d\n",k);
if (k>=NO) {printf("\n fail!");return(0);}
return(1);
}流程图请联系站长QQ752018766