Subscribe to A Kid's scribblings

## Implementing Gauss-Elimination Method to solve simultaneous linear equations

The steps in Gaussian elimination can be summarized as follows:

Stage 1: (Forward Elimination Phase)

1. Search the ﬁrst column of [A|b] from the top to the bottom for the ﬁrst non-zero
entry, and then if necessary, the second column (the case where all the coeﬃcients
corresponding to the ﬁrst variable are zero), and then the third column, and so on. The
entry thus found is called the current pivot.

2. Interchange, if necessary, the row containing the current pivot with the ﬁrst row.

3. Keeping the row containing the pivot (that is, the ﬁrst row) untouched, subtract
appropriate multiples of the ﬁrst row from all the other rows to obtain all zeroes below
the current pivot in its column.

4. Repeat the preceding steps on the submatrix consisting of all those elements which
are below and to the right of the current pivot.

Stage 2: (Back Substitution Phase)

In the case of a consistent system, if xj
is a free variable, then it can be set equal to a
parameter sj which can assume arbitrary values. If xj
is a basic variable, then we solve for xj
in terms of xj+1, . . . , xm, starting from the last basic variable and working our
way up row by row.

```import java.io.*;
public class Gauss_Elimination {
public static void main(String args[])throws Exception
{
System.out.println("No of variables to be dealt with ::");
double v[][]=new double[n][n+1];
int i,j;
for(i=0;i<n;i++)
{
System.out.println("Equation ::"+(i+1)+"\n");
for(j=0;j<n;j++)
{
System.out.println("Enter the coefficient of variable ::"+(j+1));
}
System.out.println("\nEnter the value of the right hand side of equation ::"+(i+1));
}
gauss(v,n,n+1);
}
public static void gauss(double v[][],int r,int c)
{
double t[]=new double[c];
int i=0,j=0,k=0,m=0,n=0,x=0;
double temp=0;
for(i=0;i<r-1;i++)
{
temp=v[i][i];
for(j=0;j<c;j++)
{
t[j]=v[i][j]/temp;
v[i][j]=t[j];
}
k=0;
for(m=i+1;m<r;m++)
{
temp=v[m][x];
for(n=0;n<c;n++)
{
v[m][n]=v[m][n]-(t[n]*temp);
}
}
for(k=0;k<c;k++)
t[k]=0;
temp=0;
x++;
}
for(i=r-1;i>0;i--)
{
v[i][c-1]=v[i][c-1]/v[i][i];
v[i][i]=0;
for(j=i-1;j>=0;j--)
{
v[j][c-1]=v[j][c-1]-(v[j][i]*v[i][c-1]);
v[j][i]=0;
}
}
System.out.println();
for(i=0;i<r;i++)
{
System.out.println("The value of term "+(i+1)+" is "+v[i][c-1]);
}
}
}

```

S M T W T F S
12345
6789101112
13141516171819
2021222324 25 26
2728293031