C Program for Gauss-Jordan Method

4 Min Read

The Gauss-Jordan method is used to analyze different systems of linear simultaneous equations that arise in engineering and science. This method finds its application in examining a network under sinusoidal steady state, output of a chemical plant, electronic circuits consisting invariant elements, and more.

The C program for Gauss-Jordan method is focused on reducing the system of equations to a diagonal matrix form by row operations such that the solution is obtained directly. Further, it reduces the time and effort invested in back-substitution for finding the unknowns, but requires a little more calculation. (see example)

The Gauss-Jordan method is simply a modification of the Gauss elimination method. The eliminations of the unknowns is performed not only in the equations below, but in those above as well. That is to say – unlike the elimination method, where the unknowns are eliminated from pivotal equation only, this method eliminates the unknown from all the equations.

The program of Gauss-Jordan Method in C presented here diagonalizes the given matrix by simple row operations. The additional calculations can be a bit tedious, but this method, overall, can be effectively used for small systems of linear simultaneous equations.

In the Gauss-Jordan C program, the given matrix is diagonalized using the following step-wise procedure.

  1. The element in the first column and the first row is reduced 1, and then the remaining elements in the first column are made 0 (zero).
  2. The element in the second column and the second row is made 1, and then the other elements in the second column are reduced to 0 (zero).
  3. Similarly, steps 1 and 2 are repeated for the next 3rd, 4th and following columns and rows.
  4. The overall diagonalization procedure is done in a sequential manner, performing only row operations.

Source Code for Gauss-Jordan Method in C:

#include<stdio.h>
int main()
{
    int i,j,k,n;
    float A[20][20],c,x[10];
    printf("\nEnter the size of matrix: ");
    scanf("%d",&n);
    printf("\nEnter the elements of augmented matrix row-wise:\n");
    for(i=1; i<=n; i++)
    {
        for(j=1; j<=(n+1); j++)
        {
            printf(" A[%d][%d]:", i,j);
            scanf("%f",&A[i][j]);
        }
    }
    /* Now finding the elements of diagonal matrix */
    for(j=1; j<=n; j++)
    {
        for(i=1; i<=n; i++)
        {
            if(i!=j)
            {
                c=A[i][j]/A[j][j];
                for(k=1; k<=n+1; k++)
                {
                    A[i][k]=A[i][k]-c*A[j][k];
                }
            }
        }
    }
    printf("\nThe solution is:\n");
    for(i=1; i<=n; i++)
    {
        x[i]=A[i][n+1]/A[i][i];
        printf("\n x%d=%f\n",i,x[i]);
    }
    return(0);
}

Input/Output:

Note: Let us consider a system of 10 linear simultaneous equations. Solving this by Gauss-Jordan method requires a total of 500 multiplication, where that required in the Gauss elimination method is only 333.

Therefore, the Gauss-Jordan method is easier and simpler, but requires 50% more labor in terms of operations than the Gauss elimination method. And hence, for larger systems of such linear simultaneous equations, the Gauss elimination method is the more preferred one. Find more information about the two methods here.

Also see,
Gauss Jordan Matlab Program
Gauss-Jordan Algorithm/Flowchart
Numerical Methods Tutorial Compilation

The source code for Gauss Jordan method in C language short and simple to understand. This code is to be compiled in Code::Blocks IDE. If you any queries or doubts regarding Gauss-Jordan Method – how it works and what algorithm it follows, discuss them in the comments section.

Share This Article
15 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

English
Exit mobile version