#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define nd 20
double A[nd][nd],b[nd],L[nd][nd],U[nd][nd],b2[nd],x[nd],E[nd],xR[nd];
int n;
void LuCrout();
void LuCrout_Sol();
void Reverse();
void LUCrout() {
int i,j,k;
double sum;
for(i=1;i<=n;i++)
L[i][1]=A[i][1];
for(j=1;j<=n;j++)
U[1][j]=A[1][j]/L[1][1];
for(j=2;j<=n;j++) {
for(i=j;i<=n;i++) {
sum=0;
for(k=1;k<=j-1;k++) sum=sum+L[i][k]*U[k][j];
L[i][j]=A[i][j]-sum;
}
U[j][j]=1;
for(i=j+1;i<=n;i++) {
sum=0.0;
for(k=1;k<=j-1;k++) sum=sum+L[j][k]*U[k][i];
U[j][i]=(A[j][i]-sum)/L[j][j];
}
}
}//end of LuCrout
void LuCrout_Sol() {
int i,j;
double sum;
printf("해를 입력해주세요:");
for(i=1;i<=n;i++)
scanf("%lf",&b[i]);
for(i=1;i<=n;i++){
sum=0;
for(j=1;j<=i-1;j++)
sum=sum+L[i][j]*b2[j];
b2[i]=(b[i]-sum)/L[i][i];
printf("b^[%d]:%lf\n",i,b2[i]);
}
for(i=1;i<=n;i++)
x[i]=b2[i];
printf("\n");
for(i=n-1;i>=1;i--){
sum=0;
for(j=i+1;j<=n;j++){
sum=sum+U[i][j]*x[j];
//printf("sum:%lf\n",sum);
}
x[i]=b2[i]-sum;
printf("x[%d]:%lf\t sum:%lf\n",i,x[i],sum);
}
}//end of LuCrout_Sol
void Reverse() {
int i,j,a;
double sum;
for(a=0;a<n;a++){
printf("역행렬을 구합니다. 단위 행렬을 입력해주세요:");
for(i=1;i<=n;i++){
printf("E[%d]:",i);
scanf("%lf", &E[i]);
}
for(i=1;i<=n;i++){
sum=0;
for(j=1;j<=i-1;j++)
sum=sum+L[i][j]*b2[j];
b2[i]=(E[i]-sum)/L[i][i];
printf("b^[%d]:%lf\n",i,b2[i]);
}
printf("\n");
for(i=n;i>=1;i--){
sum=0;
for(j=i+1;j<=n;j++)
sum=sum+U[i][j]*x[j];
x[i]=b2[i]-sum;
}
for(i=1;i<=n;i++)
printf("x[%d]:%lf\n",i,x[i]);
}
}// end of Reverse
int main() {
int i,j;
double sum;
printf("\n 방정식의 수 n= ");
scanf("%d",&n);
printf("게수행렬A(i,j)을 입려가시오\n");
for(i=1;i<=n; i=i+1) {
for(j=1;j<=n;j=j+1){
printf("A(%d,%d) j=1....n=",i,j);
scanf("%lf", &A[i][j]);
}
}
LUCrout();
printf("\n [L] \n");
for(i=1; i<=n; i++) {
for(j=1;j<=n;j++)
printf("%f\t",L[i][j]);
printf("\n");
}
printf("\n [U] \n");
for(i=1;i<=n;i++) {
for(j=1;j<=n;j++)
printf("%f\t",U[i][j]);
printf("\n");
}
LuCrout_Sol();
//Reverse();
}//end of main
'언어 > Coding' 카테고리의 다른 글
Jacobi (0) | 2015.12.21 |
---|---|
GaussN (0) | 2015.12.21 |
Least(최소 제곱법) (0) | 2015.12.21 |
Thomas(Tridia) (0) | 2015.12.21 |
Newton Raphson Method (0) | 2015.12.21 |