title: Gaussian Elimination Java
body: <p>I have this example matrix:</p>
  
  <pre><code>[4,1,3]
  [2,1,3]
  [4,-1,6]
  </code></pre>
  
  <p>and i want to solve exuotions:</p>
  
  <pre><code>4x1+1x2+3x3=v
  2x1+1x2+2x3=v
  4x1-1x2+6x3=v
  
  x1+x2+x3=1
  </code></pre>
  
  <p>it will be:  4x1+1x2+3x3 = 2x1+1x2+2x3 = 4x1-1x2+6x3</p>
  
  <p>-2x1+x2-5x3 =0</p>
  
  <p>and I use the code:</p>
  
  <pre><code>import java.util.*;
  
  public class GaussianElimination {
  
      // This is the problem we solved in class
      private static double[][] problem1 = {
          // x = 1, y = 2, z = 3
          { 1,  2, 3, 14 },  // 1x + 2y + 3z = 14
          { 1, -1, 1,  2 },  // 1x - 1y + 1z = 2
          { 4, -2, 1,  3 }   // 4x - 2y + 1z = 3
      };
  
  
  
      public static void solve(double[][] c, int row) {
          int rows = c.length;
          int cols = rows + 1;
          // 1. set c[row][row] equal to 1
          double factor = c[row][row];
          for (int col=0; col&lt;cols; col++)
              c[row][col] /= factor;
  
          // 2. set c[row][row2] equal to 0
          for (int row2=0; row2&lt;rows; row2++)
              if (row2 != row) {
                  factor = -c[row2][row];
                  for (int col=0; col&lt;cols; col++)
                      c[row2][col] += factor * c[row][col];
              }
      }
  
      public static void solve(double[][] c) {
          int rows = c.length;
          for (int row=0; row&lt;rows; row++)
              solve(c,row);
      }
  
      public static void print(double[][] c) {
          int rows = c.length;
          int cols = rows + 1;
          for (int row=0; row&lt;rows; row++) {
              for (int col=0; col&lt;cols; col++)
                  System.out.printf("%5.1f ",c[row][col]);
              System.out.println();
          }
          System.out.println();
      }
  
      public static void printSolution(double[][] c) {
          int rows = c.length, cols = rows + 1;
          char variable = (char)((rows &gt; 3) ? ('z' - (rows-1)) : 'x');
          System.out.println("Solution:\n");
          for (int row=0; row&lt;rows; row++)
              System.out.printf("  %c = %1.1f\n",(char)variable++,c[row][cols-1]);
          System.out.println();
      }
  
      public static void doProblem(double[][] problem, String description) {
          System.out.printf("******* %s ********\n",description);
          System.out.println("Original Equations:");
          print(problem);
          solve(problem);
          System.out.println("Solved (reduced row echelon form):");
          print(problem);
          printSolution(problem);
      }
  
      public static void main(String[] args) {
          doProblem(problem1,"Problem 1 (from class)");
  
      }
  }
  </code></pre>
  
  <p>How do I set the matrix in <code>private static double[][] problem1</code> so that I get x1,x2,x3?</p>