Tuesday 20 September 2011

Gauss Jordan Method


/*Program for solution of system of linear solutions by Gauss Jordan method*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
  int n,i,j,k,l;
  float a[10][10],b[10],t;
  clrscr();
  printf(“Give no. of variables ”);
  scanf(“%d”, &n);
  for( i =0;i<= n; i++)
  {
     for(j=0;j<n;j++)  {
         printf(“%d ,%d   th coefficient wanted ” , i+1 ,j+1);
         scanf(“%f ”, &a[i][j]);
      }
      printf(“%d th constant wanted ”, i+1);
      scanf(“%f ”, &b[i]);
   }
/* Solution started*/
 for(i=0;i<= n; i++)
 {
     t = fabs(a[i][i]);
     k = i ;
     for(j=i+1;j<n; j++)
        if (fabs ( a[j][i] >t))
        {
          k = j;
          t = fabs ( a[j][i] );
         }
         if( t< 1e - 5){
            printf(“Almost singular matrix, solution not possible\n”);
            exit(0);
         }
         if (i!= k)
            for(l=0;l<n;l++){
                t = a[i][l];
                a[i][l] = a[k][l];
                a[k][l]= t;
                t = b[i] ;
                b[i]= b[l];
                b[l] = t;
             }
            t = a[i][i];
            for(l=0;l<=n;l++)  a[i][l]/=t;
b[i]/=t;
for( j=0;j<n;j++)
if( j!= i)
{        
                t= a[i][i] ;
                for(l=0;l<n;l++)  a[i][l]- = t*a[i][l];    
                 b[j]-= t*b[i];
              }
       }
    printf(“ Solution are \n”)  ;
    for( i=0 ; i< n ; i++)
    printf(“ x[%d]= %f \n”,i, b[i]);
    getch();
  }

No comments:

Post a Comment