Wednesday, August 3, 2016

Java Program to Implement Gaussian Elimination Algorithm


This is a Java Program to Implement Gaussian Elimination Algorithm. Gaussian elimination (also known as row reduction) is an algorithm for solving systems of linear equations.
Here is the source code of the Java Program to Implement Gaussian Elimination Algorithm. The Java program is successfully compiled and run on a Windows system.
  1. /**
  2.  ** Java Program to Implement Gaussian Elimination Algorithm
  3.  **/
  4.  
  5. import java.util.Scanner;
  6.  
  7. /** Class GaussianElimination **/
  8. public class GaussianElimination
  9. {
  10.     public void solve(double[][] A, double[] B)
  11.     {
  12.         int N = B.length;
  13.         for (int k = 0; k < N; k++) 
  14.         {
  15.             /** find pivot row **/
  16.             int max = k;
  17.             for (int i = k + 1; i < N; i++) 
  18.                 if (Math.abs(A[i][k]) > Math.abs(A[max][k])) 
  19.                     max = i;
  20.  
  21.             /** swap row in A matrix **/    
  22.             double[] temp = A[k]; 
  23.             A[k] = A[max]; 
  24.             A[max] = temp;
  25.  
  26.             /** swap corresponding values in constants matrix **/
  27.             double t = B[k]; 
  28.             B[k] = B[max]; 
  29.             B[max] = t;
  30.  
  31.             /** pivot within A and B **/
  32.             for (int i = k + 1; i < N; i++) 
  33.             {
  34.                 double factor = A[i][k] / A[k][k];
  35.                 B[i] -= factor * B[k];
  36.                 for (int j = k; j < N; j++) 
  37.                     A[i][j] -= factor * A[k][j];
  38.             }
  39.         }
  40.  
  41.         /** Print row echelon form **/
  42.         printRowEchelonForm(A, B);
  43.  
  44.         /** back substitution **/
  45.         double[] solution = new double[N];
  46.         for (int i = N - 1; i >= 0; i--) 
  47.         {
  48.             double sum = 0.0;
  49.             for (int j = i + 1; j < N; j++) 
  50.                 sum += A[i][j] * solution[j];
  51.             solution[i] = (B[i] - sum) / A[i][i];
  52.         }        
  53.         /** Print solution **/
  54.         printSolution(solution);
  55.     }
  56.     /** function to print in row    echleon form **/
  57.     public void printRowEchelonForm(double[][] A, double[] B)
  58.     {
  59.         int N = B.length;
  60.         System.out.println("\nRow Echelon form : ");
  61.         for (int i = 0; i < N; i++)
  62.            {
  63.                for (int j = 0; j < N; j++)
  64.                    System.out.printf("%.3f ", A[i][j]);
  65.                System.out.printf("| %.3f\n", B[i]);
  66.            }
  67.            System.out.println();
  68.     }
  69.     /** function to print solution **/
  70.     public void printSolution(double[] sol)
  71.     {
  72.         int N = sol.length;
  73.         System.out.println("\nSolution : ");
  74.         for (int i = 0; i < N; i++) 
  75.             System.out.printf("%.3f ", sol[i]);   
  76.         System.out.println();     
  77.     }    
  78.     /** Main function **/
  79.     public static void main (String[] args) 
  80.     {
  81.         Scanner scan = new Scanner(System.in);
  82.         System.out.println("Gaussian Elimination Algorithm Test\n");
  83.         /** Make an object of GaussianElimination class **/
  84.         GaussianElimination ge = new GaussianElimination();
  85.  
  86.         System.out.println("\nEnter number of variables");
  87.         int N = scan.nextInt();
  88.  
  89.         double[] B = new double[N];
  90.         double[][] A = new double[N][N];
  91.  
  92.         System.out.println("\nEnter "+ N +" equations coefficients ");
  93.         for (int i = 0; i < N; i++)
  94.             for (int j = 0; j < N; j++)
  95.                 A[i][j] = scan.nextDouble();
  96.  
  97.         System.out.println("\nEnter "+ N +" solutions");
  98.         for (int i = 0; i < N; i++)
  99.             B[i] = scan.nextDouble();
  100.  
  101.         ge.solve(A,B);
  102.     }
  103. }

No comments:

Post a Comment

Leave you comment here...