高斯消元法

#include
#define N 5


void main()
{
int n ; 
float aug[N][N+1];
int i,j;
printf("请输入矩阵的阶数:  \n");
scanf("%d",&n);
printf("请输入增广矩阵:  \n");
for ( i=0;  i for ( j=0; j scanf("%f",&aug[i][j]);
int p,q;
for(j=0;j {   //高斯矩阵G
float G[N][N];
float I[N][N];
for ( i=0;  i    for (  p=0; p {
G[i][p] = 0;
if(i==p)
    I[i][p] = 1;
else
I[i][p] = 0;
}


for (i=j+1;i {
G[i][j] = aug[i][j];
}
for ( i=0;  i    for (  p=0; p G[i][p] = I[i][p] - G[i][p]/aug[j][j];
//开始消元,化阶梯矩阵
        for( q=0;q     for ( i=0;  i {
    float S = 0.0;
       for ( p=0; p    S = S + G[q][p]*aug[p][i];
    aug[q][i] = S;
}
}
//回代
float x[N];
x[n-1] = aug[n-1][n]/aug[n-1][n-1];
for (i = n-2 ; i>=0 ; i--)
{
float S = 0.0;
for (j=i+1;j {
S =S +aug[i][j]*x[j];
}
x[i] = (aug[i][n]-S)/aug[i][i];
}
for(i = 0;i printf("%f\t",x[i]);
printf("\n");
}

你可能感兴趣的:(基础)