The Programming Project: B.Tech
Showing posts with label B.Tech. Show all posts
Showing posts with label B.Tech. Show all posts

Thursday, July 18, 2013

Gauss-Jordan Elimination Process

Solution of system of linear equation by Gauss-Jordan elimination process. #include<stdio.h>


#include<malloc.h>
#include<stdlib.h>
int main()
{
double *coeff[15],*b,x1,x2,x3,*solution,temp,str,temp1;
int i,j,ROW,k,nz,p,q;
printf("\n Enter the value of row OR coloumn:");
scanf("%d",&ROW);
for(i=0;i<ROW;i++)
    coeff[i]=(double *)malloc(ROW*sizeof(double));
solution=(double *)malloc((ROW+1)*sizeof(double));
b=(double *)malloc(ROW*sizeof(double));
printf("\n Enter the elements of the coefficient matrix(A):\n");
for(i=0;i<ROW;i++)
    {
    for(j=0;j<ROW;j++)
          scanf("%lf",(coeff[i]+j));
    }
printf("\n Enter the elements of the constant matrix:\n");
for(i=0;i<ROW;i++)
    {
    printf("\n Enter the %dth value:",i+1);
    scanf("%lf",(b+i));
       }
for(i=0;i<ROW;i++)
    coeff[i][ROW]=b[i];
printf("\n The Augumented (A|B) matrix is:\n");
for(i=0;i<ROW;i++)
    {
    for(j=0;j<=ROW;j++)
        printf("%lf ",*(coeff[i]+j));
    printf("\n");
        }

for(i=0;i<ROW-1;i++)
    {
    nz=i;
    if(*(coeff[i]+i)==0.0)
        {
        do
             {
              nz++;
              if(nz>=ROW)
                  {
                  printf("\n Not solvable by Gauss Jordan:\n");
                  exit(1);
                  }
              }while(*(coeff[nz]+i)==0.0);
             printf("\nnz=%d",nz);
    for(k=0;k<=ROW;k++)
        {
        temp=*(coeff[nz]+k);// temp1=b[nz];
        *(coeff[nz]+k)=*(coeff[i]+k);// b[nz]=b[i];
        *(coeff[i]+k)=temp;// b[i]=temp1;
            }
             }
          /* printf("\n \n The Augumented (A|b) matrix after Row operations is:\n");
        for(p=0;p<ROW;p++)
            {
            for(q=0;q<ROW;q++)
                printf("%lf ",*(coeff[p]+q));
            printf("\n");
                }*/
        for(k=0;k<ROW-i-1;k++)
            {
            temp=0.0;
                   str=coeff[i+1+k][i];
                  //printf("\n str=%lf",str);
                  //printf("\n pivot=%lf",coeff[i][i]);
                  for(j=0;j<=ROW;j++)
                {
                       temp=coeff[i+k+1][j];
                       //printf(" temp=%lf ",temp);
                       if(str==0.0)
                           continue;
                //temp=temp-((str/coeff[i][i])*coeff[i][j]); temp1=temp1-((str/coeff[i][i])*I[i][j]);
                temp=temp/str;
                //temp=temp-((temp/str)*coeff[i][i]); temp1=temp1-((str/coeff[i][i])*I[i][j]);
                temp=temp*coeff[i][i];
                temp=temp-coeff[i][j];
                //printf(" [temp=%lf] ",temp);
                 coeff[i+k+1][j]=temp;
                          }
                   }
     
         
    }
printf("\n The Augumented (A|B) matrix after Row operations is:\n");
for(i=0;i<ROW;i++)
    {
    for(j=0;j<=ROW;j++)
        printf("%lf ",*(coeff[i]+j));
    printf("\n");
        }
for(i=ROW;i>0;i--)
    {
    if(i==ROW)
        solution[i]=(*(coeff[i-1]+i)/ *(coeff[i-1]+i-1));
    else
        {
        temp=0.0;
        nz=ROW-i;
        j=ROW;
        for(k=1;k<=nz;k++)
            {
            temp += coeff[i-1][j-1]*solution[j];
            //printf("coeff[%d,%d]*solution[%d]\n",i-1,j-1,j);
            j--;
            }
        temp=coeff[i-1][ROW]-temp;
        solution[i]=(temp/coeff[i-1][i-1]);
        }
    }
printf("\n The solution by Gauss-Jordan elimination process is:\n");
for(i=1;i<=ROW;i++)
    printf("x[%d]=%lf ",i,solution[i]);
printf("\n");
return 0;
}

 


       
           


 

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;
}