Subscribe to A Kid's scribblings        RSS Feed
-----

Implementing Gauss-Elimination Method to solve simultaneous linear equations

Icon Leave Comment
The steps in Gaussian elimination can be summarized as follows:

Stage 1: (Forward Elimination Phase)

1. Search the first column of [A|b] from the top to the bottom for the first non-zero
entry, and then if necessary, the second column (the case where all the coefficients
corresponding to the first 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 first row.

3. Keeping the row containing the pivot (that is, the first row) untouched, subtract
appropriate multiples of the first 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
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("No of variables to be dealt with ::");
        int n=Integer.parseInt(br.readLine());
        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));
                v[i][j]=Double.parseDouble(br.readLine());
            }
            System.out.println("\nEnter the value of the right hand side of equation ::"+(i+1));
            v[i][j]=Double.parseDouble(br.readLine());
        }
        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]);
        }
    }
}

0 Comments On This Entry

 

Recent Comments

October 2014

S M T W T F S
   1234
567891011
12131415161718
1920212223 24 25
262728293031 

0 user(s) viewing

0 Guests
0 member(s)
0 anonymous member(s)