a[4][5]={4。,-1.,0.,2.,-1.0,-1,4,-1,0,-7.0,0,-1,4,-1,9.0,2,0,-1,4.0,0.0};
高斯消去法(通用程序):
# include & ltstdio.h & gt
# include & ltstdlib.h & gt
# include & ltmath.h & gt
float *ColPivot(float *c,int n);
void main()
{
int i,j;
float * x;
int n = 4;
float a[4][5]={4。,-1.,0.,2.,-1.0,-1,4,-1,0,-7.0,0,-1,4,-1,9.0,2,0,-1,4.0,0.0};
x=ColPivot(a[0],n);
for(I = 0;我& ltn;i++) printf("x[%d]=%f\n ",I,x[I]);
getch();
}
float *ColPivot(float *c,int n)
{
int i,j,t,k;
float *x,p;
x =(float *)malloc(n * size of(float));
for(I = 0;我& lt= n-2;i++)
{
k = I;
for(j = I+1;j & lt= n-1;j++)
if(fabs(*(c+j *(n+1)+I))& gt;(fabs(*(c+k *(n+1)+I)))k = j;
如果(k!=i)
for(j = I;j & lt= n;j++)
{
p = *(c+I *(n+1)+j);
*(c+I *(n+1)+j)= *(c+k *(n+1)+j);
*(c+k *(n+1)+j)= p;
}
for(j = I+1;j & lt= n-1;j++)
{
p =(*(c+j *(n+1)+I))/(*(c+I *(n+1)+I));
for(t = I;t & lt= n;t++)
*(c+j *(n+1)+t)-= p *(c+I *(n+1)+t));
}
}
for(I = n-1;我& gt=0;我-)
{
for(j = n-1;j & gt= I+1;j -)
(*(c+I *(n+1)+n))-= x[j]*(*(c+I *(n+1)+j));
x[I]= *(c+I *(n+1)+n)/(*(c+I *(n+1)+I));
}
返回x;
}
-
高斯賽德爾叠代法(通用程序);
# include & ltstdio.h & gt
# include & ltstdlib.h & gt
# include & ltmath.h & gt
#定義編號100
float * gausseidel(float * a,int n)
{
int i,j,nu = 0;
float *x,dx;
x =(float *)malloc(n * size of(float));
for(I = 0;我& lt= n-1;i++)
x[I]= 0.0;
做{
for(I = 0;我& lt= n-1;i++) {
浮動d = 0.0
for(j = 0;j & lt= n-1;j++)
d+= *(a+I *(n+1)+j)* x[j];
dx =(*(a+I *(n+1)+n)-d)/(*(a+I *(n+1)+I));
x[I]+= dx;
}
if(nu & gt;=N)
{
printf(" ITER divergence \ n ");
退出(1);
}
nu++;
}
while(fabs(dx)>1e-6);
返回x;
}
void main()
{
int I;
float * x;
float c[20]={4。,-1.,0.,2.,-1.0,-1,4,-1,0,-7.0,0,-1,4,-1,9.0,2,0,-1,4.0,0.0};
float * gausseidel(float *,int);
x =高斯賽德爾(c,4);
for(I = 0;我& lt=3;i++)
printf("x[%d]=%f\n ",I,x[I]);
getch();
}