# include<stdio.h>
# include<math.h>
# define N 3
main(){
float NF2(float *x,float *y);
float A[N][N]={{10,-1,-2},{-1,10,-2},{-1,-1,5}};
float b[N]={7.2,8.3,4.2},sum=0;
float x[N]= {0,0,0},y[N]={0},x0[N]={};
int i,j,n=0;
for(i=0;i<N;i++)
{
x[i]=x0[i];
}
for(n=0;;n++){
//计算下一个值
for(i=0;i<N;i++){
sum=0;
for(j=0;j<N;j++){
if(j!=i){
sum=sum+A[i][j]*x[j];
}
}
y[i]=(1/A[i][i])*(b[i]-sum);
//sum=0;
}
//判断误差大小
if(NF2(x,y)>0.01){
for(i=0;i<N;i++){
x[i]=y[i];
}
}
else
break;
}
printf("经过%d次雅可比迭代解出方程组的解:\n",n+1);
for(i=0;i<N;i++){
printf("%f ",y[i]);
}
}
//求两个向量差的二范数函数
float NF2(float *x,float *y){
int i;
float z,sum1=0;
for(i=0;i<N;i++){
sum1=sum1+pow(y[i]-x[i],2);
}
z=sqrt(sum1);
return z;
}