#include"stdio.h"<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
#include"math.h"
#include"alloc.h"
GaussSeidel(n,a,b,x)
int n;
double *a,*b,*x;
{
int i,j;
double t,u,m=0,eps;
while(1)
{
eps=0;
for(i=0;i<n;i++)
{
t=x[i];
for(j=0;j<n;j++)
if(j!=i)
m+=a[i*n+j]*x[j];
x[i]=(b[i]-m)/a[i*n+i];
m=0;
u=x[i];
if(fabs(t-u)>eps)
eps=fabs(t-u);
}
if(eps<1e-3) return 1;
}
}
main()
{
int i;
double a[3][3]={{5,2,1},{-1,4,2},{2,-3,10}},b[3]={-12,20,3},x[3]={0,0,0};
GaussSeidel(3,a,b,x);
for(i=0;i<3;i++)
printf("x[%d]=%f\n",i,x[i]);
}
转载于:https://blog.51cto.com/lucky/19406