Professional Documents
Culture Documents
--------------------------------------
* Aim: To solve simultaneous linear equations using Gauss Elimination
*
* Program developed by Sourabh Raj Jaiswal
*/
import java.io.*;
class Assignment_13
{
static void sol(double G[][], double A[] )
{
int n = A.length;//storring the size of array
int max,i,j,k,l;
double div,t,sum;
for(k=0;k<n;k++)
{
max =k;
for(i=k+1;i<n;i++)
{
if(Math.abs(G[i][k])>Math.abs(G[max][k]))
{
max=i;
}
}
//swapping row in A matrix
double tmp[] = G[k];
G[k] = G[max];
G[max] = tmp;
for(i = k+1;i<n;i++)
{
div = G[i][k]/G[k][k];
A[i] = A[i] - (div*A[k]);
for(j=k;j<n;j++)
{
G[i][j] = G[i][j] - div*G[k][j];
}
}
}
//back substitution
double ans[] = new double[n];
for(i=n-1;i>=0;i--)
{
sum = 0.0;
for(j = i+1;j<n;j++)
{
sum = sum + G[i][j] * ans[j];
}
ans[i] = (A[i]-sum)/G[i][i];
}
l=ans.length;
System.out.println("Solution: ");
for(i =0;i<n;i++)
{
System.out.printf("%.2f " ,ans[i]);
System.out.print("\t");
}
}
}while(error==1);
double A[] = new double[n];
double G[][] = new double[n][n];
System.out.println("Enter the coefficients");
for(int i = 0; i<n;i++)
{
System.out.println("Enter coefficients of equation "+(i+1));
for(int j =0;j<n;j++)
{
G[i][j] = Double.parseDouble(br.readLine());
}
}
for(int i = 0;i<n;i++)
{
System.out.println("Enter the constant term of equation "+(i+1));
A[i] = Double.parseDouble(br.readLine());
}
display(G,A);
sol(G,A);
}
}