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. The C 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:\n\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]);
}
}
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=%f\t",i,x[i]); /* x1, x2, x3 are the required solutions*/
}
return(0);
}
Input/Output:
Also see,
Gauss Elimination MATLAB Program
Gauss Elimination Algorithm/Flowchart
Numerical Methods Tutorial Compilation
The source code for Gauss Elimination in C needs to compiled in Code::Blocks. The program is tested and is bug-free. If you have any queries regarding Gauss Elimination Method or its program aforementioned, bring them up from the comments box.
thanks for the coding.:)
Great work bro…
i want programing of Bairstow’s method. If available .
fabulously solved!!
your are great thank you
Please, what is meaning of: x[n]=A[n][n+1]/A[n][n]; … I don’t get it
Please, what is meaning of: x[n]=A[n][n+1]/A[n][n]; … I don’t get it
Can you also include the function of partial pivoting in this program?
your program is not working for some special matrices , u should improve the program in such a way that it has to work for all types of matrices…
Really? What do you mean by some special matrices?
E.g. matrix, where at some point element on the main diagonal will be equal 0.
You should try https://en.wikipedia.org/wiki/Elementary_matrix#Operations