JAVA, Python, C++ and C programs for students. Here you will also find solutions to boards papers of ISC Computer Science Practical and CBSE Computer Science. ISC and ICSE JAVA PROGRAMS
Showing posts with label B.Tech. Show all posts
Showing posts with label B.Tech. Show all posts
Thursday, July 18, 2013
LU Factorisation
Solution of system of linear equation by LU factorisation.
#include<stdio.h>
#include<malloc.h>
void LUfactorisation(double *A[10],double *L[10],double *U[10], int ROW);
void Solution1(double *A[10],double *L[10],double *U[10],double *b,int ROW);
int main()
{
double *A[10],*L[10],*U[10],*b;
int ROW,i,j;
printf("\n Enter the order of the matrix:");
scanf("%d",&ROW);
for(i=0;i<=ROW;i++)
{
A[i]=(double *)malloc((ROW+1)*sizeof(double));
L[i]=(double *)malloc((ROW+1)*sizeof(double));
U[i]=(double *)malloc((ROW+1)*sizeof(double));
}
b=(double *)malloc((ROW+1)*sizeof(double *));
printf("\n Enter the elements of the matrix A:");
for(i=1;i<=ROW;i++)
for(j=1;j<=ROW;j++)
{
printf("\n Enter the element at position A[%d][%d]:",i,j);
scanf("%lf",A[i]+j);
}
printf("\n Enter the elements of the coloumn matrix b:");
for(i=1;i<=ROW;i++)
{
printf("\n Enter the element at position b[%d]:",i);
scanf("%lf",(b+i));
}
printf("\n The matrix A is:\n");
for(i=1;i<=ROW;i++)
{
for(j=1;j<=ROW;j++)
printf("%lf ",*(A[i]+j));
printf("\n");
}
for(i=1;i<=ROW;i++)
for(j=1;j<=ROW;j++)
{
if(i>j)
*(U[i]+j)=0.0;
else
*(L[i]+j)=0.0;
}
for(i=1;i<=ROW;i++)
*(L[i]+i)=1.0;
LUfactorisation(A,L,U,ROW);
printf("\n The matrix L is:\n");
for(i=1;i<=ROW;i++)
{
for(j=1;j<=ROW;j++)
printf("%lf ",*(L[i]+j));
printf("\n");
}
printf("\n The matrix U is:\n");
for(i=1;i<=ROW;i++)
{
for(j=1;j<=ROW;j++)
printf("%lf ",*(U[i]+j));
printf("\n");
}
Solution1(A,L,U,b,ROW);
return 0;
}
void Solution1(double *A[10],double *L[10],double *U[10],double *b,int ROW)
{
double *Y,*X,temp=0.0;
int i,j,k,l;
Y=(double *)malloc((ROW+1)*sizeof(double));
X=(double *)malloc((ROW+1)*sizeof(double));
for(i=1;i<=ROW;i++)
{
if(i==1)
Y[i]=b[i]/L[i][i];
else
{
temp = 0.0;
k=i-1;
l=1;
while(l<=k)
{
temp = temp +(L[i][l]*Y[l]);#include<stdio.h>
#include<malloc.h>
void LUfactorisation(double *A[10],double *L[10],double *U[10], int ROW);
void Solution1(double *A[10],double *L[10],double *U[10],double *b,int ROW);
int main()
{
double *A[10],*L[10],*U[10],*b;
int ROW,i,j;
printf("\n Enter the order of the matrix:");
scanf("%d",&ROW);
for(i=0;i<=ROW;i++)
{
A[i]=(double *)malloc((ROW+1)*sizeof(double));
L[i]=(double *)malloc((ROW+1)*sizeof(double));
U[i]=(double *)malloc((ROW+1)*sizeof(double));
}
b=(double *)malloc((ROW+1)*sizeof(double *));
printf("\n Enter the elements of the matrix A:");
for(i=1;i<=ROW;i++)
for(j=1;j<=ROW;j++)
{
printf("\n Enter the element at position A[%d][%d]:",i,j);
scanf("%lf",A[i]+j);
}
printf("\n Enter the elements of the coloumn matrix b:");
for(i=1;i<=ROW;i++)
{
printf("\n Enter the element at position b[%d]:",i);
scanf("%lf",(b+i));
}
printf("\n The matrix A is:\n");
for(i=1;i<=ROW;i++)
{
for(j=1;j<=ROW;j++)
printf("%lf ",*(A[i]+j));
printf("\n");
}
for(i=1;i<=ROW;i++)
for(j=1;j<=ROW;j++)
{
if(i>j)
*(U[i]+j)=0.0;
else
*(L[i]+j)=0.0;
}
for(i=1;i<=ROW;i++)
*(L[i]+i)=1.0;
LUfactorisation(A,L,U,ROW);
printf("\n The matrix L is:\n");
for(i=1;i<=ROW;i++)
{
for(j=1;j<=ROW;j++)
printf("%lf ",*(L[i]+j));
printf("\n");
}
printf("\n The matrix U is:\n");
for(i=1;i<=ROW;i++)
{
for(j=1;j<=ROW;j++)
printf("%lf ",*(U[i]+j));
printf("\n");
}
Solution1(A,L,U,b,ROW);
return 0;
}
void Solution1(double *A[10],double *L[10],double *U[10],double *b,int ROW)
{
double *Y,*X,temp=0.0;
int i,j,k,l;
Y=(double *)malloc((ROW+1)*sizeof(double));
X=(double *)malloc((ROW+1)*sizeof(double));
for(i=1;i<=ROW;i++)
{
if(i==1)
Y[i]=b[i]/L[i][i];
else
{
temp = 0.0;
k=i-1;
l=1;
while(l<=k)
{
temp = temp +(L[i][l]*Y[l]);
l++;
}
Y[i]=(b[i]-temp)/L[i][i];
} // end of else
} // end of for
for(i=ROW;i>=1;i--)
{
if(i==ROW)
X[ROW]=Y[ROW]/U[ROW][ROW];
else
{
temp = 0.0;
k=i+1;
while(k<=ROW)
{
temp = temp +(U[i][k]*X[k]);
k++;
}
X[i]=(Y[i]-temp)/U[i][i];
} // end of else
} // end of outer for
printf("\n Solution of the given linear equation using LU factorisation:\n");
for(i=1;i<=ROW;i++)
printf(" x[%d] = %lf\n",i,X[i]);
return;
}
void LUfactorisation(double *A[10],double *L[10],double *U[10], int ROW)
{
int i,j,k,l;
double temp=0.0;
for(j=1;j<=ROW;j++)
{
for(i=1;i<=ROW;i++)
{
if(i<=j)
{
temp=0.0;
k=i-1;
l=1;
while(l <=k && i!=1)
{
temp = temp + (L[i][l]*U[l][j]);
l++;
}
U[i][j]=A[i][j]-temp;
} // end of if
else
{
temp=0.0;
k=j-1;
l=1;
while(l<=k)
{
temp = temp + L[i][l]*U[l][j];
l++;
}
L[i][j]=(A[i][j]-temp)/U[j][j];
} // end of else
} // end of inner for loop
} // end of outer for loop
return;
}
l++;
}
Y[i]=(b[i]-temp)/L[i][i];
} // end of else
} // end of for
for(i=ROW;i>=1;i--)
{
if(i==ROW)
X[ROW]=Y[ROW]/U[ROW][ROW];
else
{
temp = 0.0;
k=i+1;
while(k<=ROW)
{
temp = temp +(U[i][k]*X[k]);
k++;
}
X[i]=(Y[i]-temp)/U[i][i];
} // end of else
} // end of outer for
printf("\n Solution of the given linear equation using LU factorisation:\n");
for(i=1;i<=ROW;i++)
printf(" x[%d] = %lf\n",i,X[i]);
return;
}
void LUfactorisation(double *A[10],double *L[10],double *U[10], int ROW)
{
int i,j,k,l;
double temp=0.0;
for(j=1;j<=ROW;j++)
{
for(i=1;i<=ROW;i++)
{
if(i<=j)
{
temp=0.0;
k=i-1;
l=1;
while(l <=k && i!=1)
{
temp = temp + (L[i][l]*U[l][j]);
l++;
}
U[i][j]=A[i][j]-temp;
} // end of if
else
{
temp=0.0;
k=j-1;
l=1;
while(l<=k)
{
temp = temp + L[i][l]*U[l][j];
l++;
}
L[i][j]=(A[i][j]-temp)/U[j][j];
} // end of else
} // end of inner for loop
} // end of outer for loop
return;
}
Subscribe to:
Posts (Atom)