Você está na página 1de 3

#include<stdio.

h>
#include<conio.h>
#define max 100
void nhap(float a[max][max],int n)
{
float t;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{
printf("\nA[%d][%d]=",i,j);
scanf("%f",&a[i][j]);
}
printf("\n\n");
}
void xuat(float a[max][max],int n)
{
for(int i=0;i<n;i++)
{ for(int j=0;j<n;j++)
printf("%0.2f ",a[i][j]);
printf("\n\n");
}
}
void nhapMTB(float b[max],int n)
{
for(int i=0;i<n;i++)
{
printf("\nB[%d]=",i);
scanf("%f",&b[i]);
}
}
void xuatMTB(float b[max],int n)
{
for(int i=0;i<n;i++)
printf("\nb[%d]=%0.2f",i,b[i]);
}
void phanra(float a[max][max],float L[max][max],float U[max][max],int n){
int i,j,k;
float s;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
U[i][j]=0;
if(i==j)
L[i][j]=1;
else
L[i][j]=0;
}
for(j=0;j<n;j++)
{
for(i=0;i<=j;i++)
{
s=0;
for(k=0;k<i;k++)
s+=L[i][k]*U[k][j];
U[i][j]=a[i][j]-s;
}
for(i=j+1;i<n;i++)
{
s=0;
for(k=0;k<j;k++)
s+=L[i][k]*U[k][j];
if(U[j][j]!=0)
L[i][j]=(a[i][j]-s)/U[j][j];
}
}
}
//------------------------------------------------------//
int giai_LX(float a[max][max],float x[max],float b[max],int n)
{
for(int i=0;i<n;i++)
{
float s=0;
for(int j=0;j<i;j++)
s+=a[i][j]*x[j];
if(a[i][i]!=0)
x[i]=(b[i]-s)/a[i][i];
else
{
getch();
return 0;
}
}
return 1;
}
//------------------------------------------------------//
int giaiUX(float U[max][max],float x[max],float y[max],int n)
{
for(int i=n-1;i>=0;i--)
{
float s=0;
for(int j=n-1;j>i;j--)
s+=U[i][j]*x[j];
if(U[i][i]!=0)
x[i]=(y[i]-s)/U[i][i];
else
{
getch();
return 0;
}
}
return 1;
}
void giai_AX(float a[max][max],float x[max],float b[max],int n)
{
float L[max][max],U[max][max],y[max];
phanra(a,L,U,n);
giai_LX(L,y,b,n);
giaiUX(U,x,y,n);
}
float det(float a[max][max],int n)
{
float d=1,L[max][max],U[max][max];
phanra(a,L,U,n);
for(int i=0;i<n;i++)
d*=L[i][i]*U[i][i];
return d;
}
void tim_mt_nghichdao(float a[max][max],float b[max][max],int n)
{
int i,j;
float I[max],X[max];
for(j=0;j<n;j++){
for(i=0;i<n;i++)
if(i==j)
I[i]=1;
else
I[i]=0;
giai_AX(a,X,I,n);
for(i=0;i<n;i++)
b[i][j]=X[i];
}
}
//-------------------------------------------------------//
void main(){
clrscr();
int n;
float a[max][max],b[max],c[max][max],y[max],x[max],L[max][max],U[max][max];
printf("\n Nhap n:"); scanf("%d",&n);
nhap(a,n);
xuat(a,n);
printf("nhap ma tran he so B\n");
nhapMTB(b,n);
printf("\n-------------------\n");
phanra(a,L,U,n);
printf("\n Sau khi tach A=L*U");
printf("\n-------------------\n");
printf("\n Ma tran L:\n\n");
xuat(L,n);
printf("\n-------------------\n");
printf("\n Ma tran U:\n\n");
xuat(U,n);
printf("\n-------------------\n");
printf("\nGIAI LY=B || TIM Y");
giai_LX(L,y,b,n);
printf("\n");
if(giai_LX(L,y,b,n)==1)
xuatMTB(y,n);
else
printf("khong giai duoc!!!");
printf("\n--------------------\n");
giaiUX(U,x,y,n);
printf("\nGIAI UX=Y || TIM X.\n");
if(giaiUX(U,x,y,n)==1){
printf("\nNghiem cua phuong trinh:\n");
xuatMTB(x,n);
}
else
printf("\nkhong giai duoc!!!");
giai_LX(a,x,b,n);
tim_mt_nghichdao(a,c,n);
printf("\n\n");
printf("ma tran nghich dao la:\n\n");
xuat(c,n);
float kq= det(a,n);
printf("Det cua a la %f",kq);
getch();
}

Você também pode gostar