Advertisement
alvinfnaldi

EliminasiGauss

Apr 11th, 2021
1,187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.49 KB | None | 0 0
  1. import java.util.Scanner;
  2.    
  3.    public class EliminasiGauss {
  4.  
  5.       public static void main(String[] args) {
  6.  
  7.      Scanner in = new Scanner(System.in);
  8.    
  9.      double[][] A;              // The coefficients
  10.      double[]   b;              // The RHS
  11.      double     pivot, factor;
  12.      int        n;              // Number of variables
  13.    
  14.      int i, j, k;
  15.    
  16.      System.out.print("Number of unknows : ");
  17.      n = in.nextInt();          // Read in problem size
  18.    
  19.      A = new double[n][n];      // Reserve space for coefficient matrix      
  20.      b = new double[n];         // Reserve space for RHS vector
  21.  
  22.      /* -----------------------------------
  23.         Read in the rows, one row at a time
  24.         ----------------------------------- */
  25.          
  26.      for (i = 0; i < n; i++) {
  27.         // Read in row i's coefficients
  28.         for (j = 0; j < n; j++)
  29.         {
  30.         System.out.print("A["+i+"]["+j+"] = ");
  31.         A[i][j] = in.nextDouble();
  32.         }
  33.    
  34.         // Read in row i's RHS
  35.         System.out.print("B["+i+"] \t= ");
  36.         b[i] = in.nextDouble();
  37.             System.out.println();
  38.      }
  39.    
  40.      /* -------------------------------
  41.         The Gaussian elimination method
  42.         ------------------------------- */
  43.          
  44.      for (i = 0; i < n; i++) {
  45.             // Process row i
  46.    
  47.         /* ----------------
  48.            (A) Select pivot
  49.            ---------------- */
  50.         pivot = A[i][i];
  51.    
  52.         /* -------------------
  53.            (B) Normalize row i
  54.            ------------------- */
  55.         for (j = 0; j < n; j++)
  56.            A[i][j] = A[i][j] / pivot;
  57.    
  58.         b[i] = b[i] / pivot;
  59.    
  60.         /* ---------------------
  61.            (C) Sweep using row i
  62.            --------------------- */
  63.         for (k = 0; k < n; k++) {
  64.                
  65.            if ( k != i ) {
  66.                    
  67.           factor = A[k][i];
  68.    
  69.           for (j = 0; j < n; j++)
  70.              A[k][j] = A[k][j] - factor*A[i][j];
  71.    
  72.           b[k] = b[k] - factor*b[i];
  73.            }
  74.         }
  75.      }    
  76.          
  77.      System.out.println("Solution : ");
  78.    
  79.      for (i = 0; i < n; i++)
  80.         System.out.println("x" + (i+1) + " = " + b[i]);
  81.          /* -------------------------------------
  82.         Soal dari ppt :        
  83.             27 x1 +  6 x2 -    x3 = 85  .....(1a)
  84.              6 x1 + 15 x2 +  2 x3 = 72  .....(1b)
  85.                x1 +    x2 + 54 x3 = 110 .....(1c)
  86.         ------------------------------------- */        
  87.       }
  88.    }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement