Gauss Jordan Method in C

Gauss Jordan Method in C

Gauss-Jordan Method is an algorithm that can be used to solve linear equations and to find the inverse of any invertible matrix.

Let’s take a example of system of linear equation to solve it using Gauss-Jordan method :-

                                                   2x+8y+2z = 14

                                                    x+6y-z      = 13

                                                    2x-y+2z    =  5

Method to solve Gauss Jordon

  1. Define the augmented matrix from linear equations.
  2. Swap rows if necessary to obtain a non-zero number in the 1st row and first column.
  3. Use the row operation to get 1 as a entry in the first row and 1st column.
  4. Use row operations to make all other entities as zeros in column one.
  5. swap rows if necessary to obtain a non-zero number in the 2nd row and 2nd column.
  6. Use a row operation to make this entry 1.
  7. Use row operations to make all other entities as zero in column two.
  8. Repeat step 5 for row 3 and column 3. Continue moving along the main diagonal until you reach the last row, or until the number is zero.
  9. The final matrix is called the reduced row-echelon form.

C program to solve system of linear equations by using Gauss Jordan Method :-

/* C Program: Gauss-Elimination Method */
#include<stdio.h>
#include<conio.h>
int main()
{
    float a[10][10],x[10], temp;
    int i,j,k, n;
    printf("\nEnter the Number of equations : ");
    scanf("%d",&n);
    printf("\nThe Number of Equations are : %d\n",n);
    printf("\nNow Enter augmented (coefficients of equ.) matrix\n");
    for(i=1;i<=n;i++)
        for(j=1;j<=n+1;j++)
        scanf("%f",&a[i][j]);
    printf("\nAugmented (coefficients of equ.) matrix\n");
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n+1;j++)
        printf("%6.0f",a[i][j]);
        printf("\n");
    }

    /* Applyying Gauss Jordan Elimination  */
    for(i=1;i<=n;i++)
        for(j=1;j<=n;j++)
        {
        if(i!=j)
           temp = a[j][i]/a[i][i];
        for(k=1;k<=n+1;k++)
            a[j][k]=a[j][k]-(temp*a[i][k]);
        }
    /* obtained solution  */
    for(i=1;i<=n;i++)
       x[i]= a[i][n+1]/a[i][i];

    printf("\nSolution of the equation is :\n");
    for(i=1;i<=n;i++)
        printf("x[%d] = %7.3f\n",i,x[i]);

    return 0;
}

 

OUTPUT :-

 

 
Enter the Number of equations : 3

The Number of Equations are : 3

Now Enter augmented (coefficients of equ.) matrix
2
8
2
14
1
6
-1
13
2
-1
2
5

Augmented (coefficients of equ.) matrix
     2     8     2    14
     1     6    -1    13
     2    -1     2     5

Solution of the equation is :
x[1] =   5.000
x[2] =   1.000
x[3] =  -2.000

This Post Has 2 Comments

  1. SETODJI

    Thanks

  2. SETODJI

    It is very fine

Leave a Reply