求助!!!雅克比迭代法求线性方程组,怎么实现不了啊??????究竟是哪里出错了????
#include <stdio.h>#include <math.h>
#include <malloc.h>
void Jacobidiedai(int n, double *a, double *b,double *x)
{
int i,j;
double *x0,m=0,eps=0;
x0= (double *) malloc(n*sizeof(double));
for(i=0;i<n;i++)
x0[i]=x[i];
while(1)
{
for(i=0;i<n;i++)
{
for(j=0;j<n;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=0;i<n;i++)
{
if(eps<fabs(x[i]-x0[i]))
eps=fabs(x[i]-x0[i]);
}
if(eps<1e-3)
return;
for(i=0;i<n;i++)
x0[i]=x[i];
}
free(x0);
}
void main()
{
int i,j,n;
double *a;
double *b;
double *x;
printf("please enter n\n");
scanf("%d",&n);
a = (double *)malloc(sizeof(double)*n*n);
b = (double *)malloc(sizeof(double)*n);
x = (double *)malloc(sizeof(double)*n);
memset(x,0,n);
printf("please enter a[i][j]\n");
for (i=0;i<n*n;i++) scanf("%f",&a[i]);
printf("please enter b[j]\n");
for (j=0;j<n;j++) scanf("%f",&b[j]);
Jacobidiedai(n,a,b,x);
for(i=0;i<n;i++)
printf("x[%d]=%f\n",i,x[i]);
}