Wednesday, 23 April 2014

Program to solve simultaneous equation using by Guess elimination method

#include<stdio.h>
#include<conio.h>
#include<math.h>
//#include<stdlib.h>

int main()
{
int n,i,j,m,k,p,q;
float e,a[10][10],x[10],temp,max,sum,u;
//clrscr();
printf("enter the number of equations :");
scanf("%d",&n);
printf("\nenter the co-efficients of equations\n");
for(i=1;i<=n;i++)
 for(j=1;j<=(n+1);j++)
  scanf("%f",&a[i][j]);
printf("\nenter the value of prescribed precision :");
scanf("%f",&e);
printf("\nentered co-efficient matrix is\n");
for(i=1;i<=n;i++)
{
 for(j=1;j<=(n+1);j++)
  printf("%.2f\t",a[i][j]);
 printf("\n");
}
    //Lower Triangularisation Steps//
for(k=1;k<=(n-1);k++)
   //Pivotal Condensation Steps//
{
  max=fabs(a[k][k]);
  p=k;
  for(m=(k+1);m<=n;m++)
    if(max < fabs(a[m][k]))
    {
      max=fabs(a[m][k]);
      p=m;
    }
  if(max <= e)
  {
    printf("\nill conditional equation\n");
    return(0);
  }
  if (p!=k)
    for(q=k;q<=(n+1);q++)
    {
      temp=a[p][q];
      a[p][q]=a[k][q];
      a[k][q]=temp;
    }
    //End of Pivoting Steps.
  for(i=(k+1);i<=n;i++)
  {
    u=a[i][k]/a[k][k];
    for(j=k;j<=(n+1);j++)
      a[i][j]=a[i][j]-(u*a[k][j]);
  }
}
  printf("\nLower Triangularised matrix is\n");
  for(i=1;i<=n;i++)
  {
    for(j=1;j<=(n+1);j++)
      printf("%.2f\t",a[i][j]);
    printf("\n");
  }
     //End Lower Triangularisation Steps//

     //Backward Substitution Steps //
  x[n]=a[n][n+1]/a[n][n];
  for(i=(n-1);i>=1;i--)
  {
    sum=0;
    for(j=i+1;j<=n;j++)
      sum=sum+a[i][j]*x[j];
    x[i]=(a[i][j]-sum)/a[i][i];
  }
  printf("\nsolution vector is\n");
  for(i=1;i<=n;i++)
    printf("x[%d]=%.2f\n",i,x[i]);

  getch();

}

No comments:
Write comments

Featured post

List of Universities in Karnataka offering M.Sc Computer Science

The post-graduate programme in Computer Science (M.Sc Computer Science) contains two academic years duration and having a four semesters....

Popular Posts

Copyright @ 2011-2016