#include <stdio.h>
#include <math.h>
#define nd 20
double x[nd], a[nd][nd], b[nd];
int n,i,j,k;
void eliminate();
void substitute();
void eliminate(){
double factor;
for(k=1;k<=n-1;k=k+1){
for(i=k+1;i<=n;i=i+1){
factor=a[i][k]/a[k][k];
for(j=k+1;j<=n;j=j+1){
a[i][j]=a[i][j]-factor*a[k][j];
}
b[i]=b[i]-factor*b[k];
}
}
}//end of eliminate
void substitute(){
double sum;
x[n]=b[n]/a[n][n];
for(i=n-1;i>=1;i=i-1){
sum=0;
for(j=i+1;j<=n;j=j+1){
sum=sum+a[i][j]*x[j];
}
x[i]=(b[i]-sum)/a[i][i];
}
}//end of substitute
int main() {
printf("\n방정식 수 n=");
scanf("%d", &n);
printf("계수행렬 A(i,j)을 입력하시오\n");
for(i=1;i<=n;i=i+1)
{
printf("A(%d,j) j=1,..n=",i);
for(j=1;j<=n;j=j+1) scanf("%lf",&a[i][j]);
}
printf("상수행렬 b(i)를 입력하시오\n");
printf("b(i) i=1,,,n=");
for(i=1;i<=n;i=i+1) scanf("%lf",&b[i]);
printf("\n Input data 출력");
for(i=1;i<=n;i=i+1)
{
printf("\n");
for(j=1;j<=n;j=j+1)
{ printf("%10.5f", a[i][j]);
}
printf("%10.5f",b[i]);
}
printf("\n");
eliminate();
substitute();
printf("\n 계산결과 출력");
for(i=1;i<=n;i=i+1)
{
printf("\n x(%d)=%10.6f",i,x[i]);
}
printf("\n");
}// end of main
'언어 > Coding' 카테고리의 다른 글
Gauss Seidel (0) | 2015.12.21 |
---|---|
Jacobi (0) | 2015.12.21 |
Least(최소 제곱법) (0) | 2015.12.21 |
Thomas(Tridia) (0) | 2015.12.21 |
LUCrout (LU분해,해 구하기, 역행렬 구하기) (0) | 2015.12.21 |