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. The program output is also shown below.

  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. }

Gaussian Elimination Algorithm Test
 
 
Enter number of variables
3
 
Enter 3 equations coefficients
2 1 -1
-3 -1 2
-2 1 2
 
Enter 3 solutions
8
-11
-3
 
Row Echelon form :
-3.000 -1.000 2.000 | -11.000
0.000 1.667 0.667 | 4.333
0.000 0.000 0.200 | -0.200
 
 
Solution :
2.000 3.000 -1.000

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.