Scientific Computing (Gauss Method Elimination)

Samratdas1991
Posted by Samratdas1991 under Others category on | Views : 2910
///////////////////////////////////////////////////////////////////////////////////
// //
// OPEN LICENCE //
// //
///////////////////////////////////////////////////////////////////////////////////
// //
// GAUSS_METHOD //
// (C) Copyright 2008-2012 by SAMRAT DAS (BE/6103/08) //
// B.I.T Mesra (Deoghar Campus) from C.S.E. Dept //
// Inc. All Rights Reserved. //
// //
///////////////////////////////////////////////////////////////////////////////////
/******************************************************************************************************************
* wap to solve the following system of equation by gaussian elimination method *
* *
* 4x+y+z = 0 *
* x+4y-2z = 0 *
* 3x+2y-4z = 0 *
* *
*******************************************************************************************************************/


import java.util.Scanner;
import java.lang.*;
class gauss
{
public static void main(String args [])
{
Scanner input = new Scanner (System.in);
int i,j;
float a[][] = new float [3][3];
float b[] = new float [3];
float x1 = 0,x2 = 0,x3 = 0;
System.out.println("Enter the cofficient of the Matrix : ");
for(i = 0;i<3;i++)
{
for(j = 0;j<3;j++)
{
System.out.println("\nEnter the Eliments of the Matrix of" +i+j);
a[i][j] = input.nextFloat();
}
}
System.out.println("*****The Entered Matrix is *******");
for(i = 0;i<3; i++)
{
for(j = 0; j<3;j++)
{
System.out.print("\t"+a[i][j]);
}
System.out.print("\n");
}
for(i = 0; i<3;i++)
{
for(j = 0; j<3; j++)
{
a[1][j] = a[1][j] - (a[0][j]*(a[1][0]/a[0][0]));
a[2][j] = a[2][j]-(a[0][j]*(a[2][0]/a[0][0]));
}}
System.out.println("\n@@@@@ The new matrix is @@@@@ \n");

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print("\t" + a[i][j]);
}
System.out.print("\n");
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
a[2][j]=a[2][j]-(a[1][j]*(a[2][1]/a[1][1]));
}
}
System.out.println("+++++The final matrix is+++++");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print("\t"+a[i][j]);
}
System.out.print("\n");
}
System.out.println("\nEnter the fourth Column of Matrix :");
for(i = 0; i<3; i++)
{
System.out.println("Enter the value Matrix : ");
b[i] = input.nextFloat();
}
for(i = 0; i<3; i++)
{
for(j = 0; j<3; j++)
{
x3=(b[2]/a[2][2]);
x2=((b[1]-(a[1][2]*x3))/a[1][1]);
x1=(b[0]-(a[0][2]*x3)-(a[0][1]*x2)/a[0][0]);
}
}
System.out.println("\n\t\t````THE ROOTS ARE``````` \n");
System.out.println("\n\t x1 = " + x1);
System.out.println("\n\t x2 = " + x2);
System.out.println("\n\t x3 = " + x3);
}
}

Comments or Responses

Login to post response