Java Program to Implement Gauss Seidel Method

This is java program to find the solution to the linear equations of any number of variables. The class provides a simple implementation of the Gauss-Seidel method. If the matrix isn’t diagonally dominant the program tries to convert it(if possible) by rearranging the rows.

Here is the source code of the Java Program to Implement Gauss-Seidel Method. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. //This class provides a simple implementation of the GaussSeidel method for solving systems of linear equations.
  2. //If the matrix isn't diagonally dominant the program tries to convert it(if possible) by rearranging the rows.
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. import java.io.PrintWriter;
  8. import java.util.Arrays;
  9. import java.util.StringTokenizer;
  10.  
  11. public class Gauss_Seidel 
  12. {
  13.     public static final int MAX_ITERATIONS = 100;  
  14.     private double[][] M;
  15.     public Gauss_Seidel(double [][] matrix) { M = matrix; }
  16.  
  17.     public void print()
  18.     {
  19.         int n = M.length;
  20.         for (int i = 0; i < n; i++) 
  21.         {
  22.             for (int j = 0; j < n + 1; j++)
  23.                 System.out.print(M[i][j] + " ");
  24.             System.out.println();
  25.         }
  26.     }
  27.  
  28.     public boolean transformToDominant(int r, boolean[] V, int[] R)
  29.     {
  30.         int n = M.length;
  31.         if (r == M.length) 
  32.         {
  33.             double[][] T = new double[n][n+1];
  34.             for (int i = 0; i < R.length; i++) 
  35.             {
  36.                 for (int j = 0; j < n + 1; j++)
  37.                     T[i][j] = M[R[i]][j];
  38.             }
  39.  
  40.             M = T;
  41.  
  42.             return true;
  43.         }
  44.  
  45.         for (int i = 0; i < n; i++) 
  46.         {
  47.             if (V[i]) continue;
  48.  
  49.             double sum = 0;
  50.  
  51.             for (int j = 0; j < n; j++)
  52.                 sum += Math.abs(M[i][j]);
  53.  
  54.             if (2 * Math.abs(M[i][r]) > sum) 
  55.             { // diagonally dominant?
  56.                 V[i] = true;
  57.                 R[r] = i;
  58.  
  59.                 if (transformToDominant(r + 1, V, R))
  60.                     return true;
  61.  
  62.                 V[i] = false;
  63.             }
  64.         }
  65.  
  66.         return false;
  67.     }
  68.  
  69.     public boolean makeDominant()
  70.     {
  71.         boolean[] visited = new boolean[M.length];
  72.         int[] rows = new int[M.length];
  73.  
  74.         Arrays.fill(visited, false);
  75.  
  76.         return transformToDominant(0, visited, rows);
  77.     }
  78.  
  79.     public void solve()
  80.     {
  81.         int iterations = 0;
  82.         int n = M.length;
  83.         double epsilon = 1e-15;
  84.         double[] X = new double[n]; // Approximations
  85.         double[] P = new double[n]; // Prev
  86.         Arrays.fill(X, 0);
  87.  
  88.         while (true) 
  89.         {
  90.             for (int i = 0; i < n; i++) 
  91.             {
  92.                 double sum = M[i][n]; // b_n
  93.  
  94.                 for (int j = 0; j < n; j++)
  95.                     if (j != i)
  96.                         sum -= M[i][j] * X[j];
  97.  
  98.         // Update x_i to use in the next row calculation
  99.                 X[i] = 1/M[i][i] * sum;
  100.             }
  101.  
  102.             System.out.print("X_" + iterations + " = {");
  103.             for (int i = 0; i < n; i++)
  104.                 System.out.print(X[i] + " ");
  105.             System.out.println("}");
  106.  
  107.             iterations++;
  108.             if (iterations == 1) 
  109.                 continue;
  110.  
  111.             boolean stop = true;
  112.             for (int i = 0; i < n && stop; i++)
  113.                 if (Math.abs(X[i] - P[i]) > epsilon)
  114.                     stop = false;
  115.  
  116.             if (stop || iterations == MAX_ITERATIONS) break;
  117.             P = (double[])X.clone();
  118.         }
  119.     }
  120.  
  121.     public static void main(String[] args) throws IOException
  122.     {
  123.         int n;
  124.         double[][] M;
  125.  
  126.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  127.         PrintWriter writer = new PrintWriter(System.out, true);
  128.  
  129.         System.out.println("Enter the number of variables in the equation:");
  130.         n = Integer.parseInt(reader.readLine());
  131.         M = new double[n][n+1];
  132.         System.out.println("Enter the augmented matrix:");
  133.  
  134.         for (int i = 0; i < n; i++) 
  135.         {
  136.             StringTokenizer strtk = new StringTokenizer(reader.readLine());
  137.  
  138.             while (strtk.hasMoreTokens())
  139.                 for (int j = 0; j < n + 1 && strtk.hasMoreTokens(); j++)
  140.                     M[i][j] = Integer.parseInt(strtk.nextToken());
  141.         }
  142.  
  143.  
  144.         Gauss_Seidel gausSeidel = new Gauss_Seidel(M);
  145.  
  146.         if (!gausSeidel.makeDominant()) 
  147.         {
  148.             writer.println("The system isn't diagonally dominant: " +
  149.                      "The method cannot guarantee convergence.");
  150.         }
  151.  
  152.         writer.println();
  153.         gausSeidel.print();
  154.         gausSeidel.solve();
  155.     }
  156. }

Output:

$ javac Gauss_Seidel.java
$ java Gauss_Seidel
Enter the number of variables in the equation:
2
Enter the augmented matrix:
1 2 3
6 5 4
 
6.0 5.0 4.0 
1.0 2.0 3.0 
X_0 = {0.6666666666666666 1.1666666666666667 }
X_1 = {-0.30555555555555564 1.652777777777778 }
X_2 = {-0.7106481481481481 1.855324074074074 }
X_3 = {-0.8794367283950617 1.9397183641975309 }
X_4 = {-0.9497653034979425 1.9748826517489713 }
X_5 = {-0.9790688764574759 1.9895344382287379 }
X_6 = {-0.9912786985239483 1.9956393492619742 }
X_7 = {-0.9963661243849785 1.9981830621924892 }
X_8 = {-0.9984858851604077 1.9992429425802039 }
X_9 = {-0.9993691188168363 1.999684559408418 }
X_10 = {-0.9997371328403484 1.999868566420174 }
X_11 = {-0.9998904720168117 1.9999452360084058 }
X_12 = {-0.999954363340338 1.999977181670169 }
X_13 = {-0.9999809847251406 1.9999904923625702 }
X_14 = {-0.9999920769688085 1.9999960384844042 }
X_15 = {-0.9999966987370034 1.9999983493685016 }
X_16 = {-0.9999986244737512 1.9999993122368755 }
X_17 = {-0.9999994268640631 1.9999997134320315 }
X_18 = {-0.9999997611933598 1.9999998805966799 }
X_19 = {-0.9999999004972331 1.9999999502486165 }
X_20 = {-0.9999999585405137 1.9999999792702567 }
X_21 = {-0.999999982725214 1.999999991362607 }
X_22 = {-0.9999999928021724 1.9999999964010862 }
X_23 = {-0.999999997000905 1.9999999985004524 }
X_24 = {-0.999999998750377 1.9999999993751885 }
X_25 = {-0.9999999994793237 1.9999999997396618 }
X_26 = {-0.9999999997830514 1.9999999998915257 }
X_27 = {-0.9999999999096048 1.9999999999548024 }
X_28 = {-0.9999999999623352 1.9999999999811675 }
X_29 = {-0.9999999999843061 1.999999999992153 }
X_30 = {-0.9999999999934606 1.9999999999967302 }
X_31 = {-0.9999999999972751 1.9999999999986375 }
X_32 = {-0.9999999999988646 1.9999999999994322 }
X_33 = {-0.9999999999995268 1.9999999999997633 }
X_34 = {-0.9999999999998028 1.9999999999999014 }
X_35 = {-0.9999999999999176 1.9999999999999587 }
X_36 = {-0.9999999999999656 1.9999999999999827 }
X_37 = {-0.9999999999999855 1.9999999999999927 }
X_38 = {-0.9999999999999938 1.999999999999997 }
X_39 = {-0.9999999999999973 1.9999999999999987 }
X_40 = {-0.9999999999999988 1.9999999999999993 }
X_41 = {-0.9999999999999993 1.9999999999999996 }

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

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.