c program gauss elimination method

In engineering and science, the solution of linear simultaneous equations is very important. Different analysis such as electronic circuits comprising invariant elements, a network under steady and sinusoidal condition, output of a chemical plant and finding the cost of chemical reactions in such plants require the solution of linear simultaneous equations.
In Gauss-Elimination method, these equations are solved by eliminating the unknowns successively. TheC program for Gauss elimination method reduces the system to an upper triangular matrix from which the unknowns are derived by the use of backward substitution method.
Pivoting, partial or complete, can be done in Gauss Elimination method. So, this method is somewhat superior to the Gauss Jordan method. This approach, combined with the back substitution, is quite general. It is popularly used and can be well adopted to write a program for Gauss Elimination Method in C.
For this, let us first consider the following three equations:
a1x + b1y + c1z = d1
a2x + b2y + c2z = d2
a3x + b3y + c3z = d3
Assuming a1 ≠ 0, x is eliminated from the second equation by subtracting (a2/ a1) times the first equation from the second equation. In the same way, the C code presented here eliminates x from third equation by subtracting (a3/a1) times the first equation from the third equation.
Then we get the new equations as:
a1x + b1y + c1z = d1
b’2y + c’2z = d’2
c’’3z = d’’3
The elimination procedure is continued until only one unknown remains in the last equation. After its value is determined, the procedure is stopped. Now, Gauss Elimination in C uses back substitution to get the values of x, y and z as:
z=  d’’3 / c’’3
y=(d’2 – c’2z) / b’2
x=( d1- c1z- b1y)/ a1

Source Code for Gauss Elimination Method in C:

#include<stdio.h>
int main()
{
    int i,j,k,n;
    float A[20][20],c,x[10],sum=0.0;
    printf(“nEnter the order of matrix: “);
    scanf(“%d”,&n);
    printf(“nEnter the elements of augmented matrix row-wise:nn”);
    for(i=1; i<=n; i++)
    {
        for(j=1; j<=(n+1); j++)
        {
            printf(“A[%d][%d] : “, i,j);
            scanf(“%f”,&A[i][j]);
        }
    }
    for(j=1; j<=n; j++) /* loop for the generation of upper triangular matrix*/
    {
        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];
                }
            }
        }
    }
    x[n]=A[n][n+1]/A[n][n];
    /* this loop is for backward substitution*/
    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][n+1]-sum)/A[i][i];
    }
    printf(“nThe solution is: n”);
    for(i=1; i<=n; i++)
    {
        printf(“nx%d=%ft”,i,x[i]); /* x1, x2, x3 are the required solutions*/
    }
    return(0);
}
OUTPUT:
Enter the order of matrix: 3
Enter the elements of augmented matrix row-wise:
A[1][1] : 1
A[1][2] : 1
A[1][3] : 1
A[1][4] : 9
A[2][1] : 2
A[2][2] : -3
A[2][3] : 4
A[2][4] : 13
A[3][1] : 3
A[3][2] : 4
A[3][3] : 5
A[3][4] : 40
The solution is: 
x1=1.000000
x2=3.000000
x3=5.000000

Leave a Comment

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

Open chat