Tuesday, February 01, 2011

Gauss Elimination Method


/*      The following C program implements Gaussian Elimination method for finding
         the solution of a set of linear simultaneous equations. The number of equations
         and the coefficient matrix are read as input. The determinant of the coefficient
         matrix is also evaluated. The results are printed properly.        */


#include<stdio.h>
#include<math.h>
#include<conio.h>
#define maximum 10
#define minvalue 0.0005


void main()
{
 float augmentedmatrix[maximum][maximum] ;  
  /* 2D array declared to store augmented co-efficient matrix */
 float temporary, r ;      
 float solutions[maximum] ;  
      /* 1D array declared to store solutions of the simultaneous equations */

 int i,j,k, noofequations, temp,minus=0;              /* declaring counter variables */

 clrscr();

 printf("\n===========================================");
 printf("\n        GAUSSIAN  ELIMINATION  METHOD");
 printf("\n===========================================");

 printf("\n Enter the number of total equations to be provided as input : \n");
 scanf("%d",&noofequations);

 /* storing augmented co-efficient matrix as a matrix of dimension
    (noofequations)x(noofequations+1) in 2D array */

 printf("\n Enter the augmented co-efficient matrix : \n");
 for(i=0; i<noofequations; i++)
  for(j=0; j<=noofequations; j++)
            scanf("%f",&augmentedmatrix[i][j]) ;

 /* converting augmented matrix into upper triangular form */

 for(j=0; j<noofequations; j++)
 {
   temp=j;

   /* finding maximum coefficient of Xj in last (noofequations-j) equations */

   for(i=j+1; i<noofequations; i++)
            if(augmentedmatrix[i][j]>augmentedmatrix[temp][j])
                        temp=i;

   if(fabs(augmentedmatrix[temp][j])<minvalue)
             {
              printf("\n Coefficients are too small to deal with !!!");
              goto end;
             }

     /* swapping row which has maximum coefficient of Xj */

    if(temp!=j)
    {
            minus++;
            for(k=0; k<=noofequations; k++)
            {
            temporary=augmentedmatrix[j][k] ;
            augmentedmatrix[j][k]=augmentedmatrix[temp][k] ;
            augmentedmatrix[temp][k]=temporary ;
            }
     }

   /* performing row operations to form required upper triangular matrix */

   for(i=j+1; i<noofequations; i++)
   {
            r=augmentedmatrix[i][j];
            for(k=0; k<=noofequations; k++)
              augmentedmatrix[i][k]-=(augmentedmatrix[j][k]/augmentedmatrix[j][j])*r ;
   }

}

 /* Display augmented coefficient matrix */

 printf("\n");
 for(i=0; i<noofequations; i++)
 {
    for(j=0; j<=noofequations; j++)
            printf("  %4.2f",augmentedmatrix[i][j]) ;
    printf("\n");
 }

 /* finding and displaying determinant of coefficient matrix */

 temporary=1.0 ;
 for(i=0; i<noofequations; i++)
            temporary*=augmentedmatrix[i][i] ;       /* multiplying diagonal elements only */

 if(minus%2==0)                                            /* when number of row exchanges is even */ 
    printf("\n Determinant of coefficient matrix = %.2f", temporary) ;
 else
    printf("\n Determinant of coefficient matrix = %.2f", -temporary) ;

  /* using back substitution to get solutions */

 for(i=0;i<noofequations; i++)
  solutions[i]=augmentedmatrix[i][noofequations] ;

 for(i=noofequations-1, k=0; i>=0; i--,k++)
 {
  for(j=k; j>0; j--)
            solutions[i]-=augmentedmatrix[i][noofequations-j]*solutions[noofequations-j] ;
 solutions[i]/=augmentedmatrix[i][i] ;
 }

 printf("\n\n\n");

 /* printing solutions */

 for(i=0; i<noofequations; i++)
  printf("  X%d  =  %4.2f \n", i, solutions[i]) ;

 end: getch() ;

 }
SAMPLE OUTPUT:

Solving the following set of linear simultaneous equations by Gaussian elimination:

   x0 + 5x1 + 3x2 = 10
x0 + 3x1 + 2x2 = 5
2x0 + 4x1 – 6x2 = -4



5 comments:

  1. hi,don't really know much in c++,kindda of a stater and i'm really interested in getting into it.I ran these codes on C++ and didn't really work,got some errors like "converting to int from float".i don't really know how you can help review the codes so they could actually help when you copy and paste directly from this site.thanks

    ReplyDelete
  2. do you have source code of this in Java? :D

    ReplyDelete
  3. you will get plenty in the net .. but as the logic is same this code will run in java too, just paste the main function, in the main method(java), do some necessary corrections, like replace printf by system.out.println, scanf etc .. rest is straightforward .. thnku :)

    ReplyDelete
  4. Because their seeds have more anti-nutrients than others, but
    most other fruit contains far more sugar than is recommended in the paleo food diet diet and come to
    your own decision.

    my web site - paleo vegetarian

    ReplyDelete
  5. Another young woman, Brenda, who was married to a Mexican/Hispanic
    or dating one, you will receive positive results. Marital rape is gaining a lot of work but to" stop scaring people and give them a time limit to give you the best. When he logs in, on the front of the home environment.

    Have a look at my site 21 day detox

    ReplyDelete

Do you think this information useful or was not up to the mark? Comment if you have any advices or suggestions about the post.