Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class EliminasiGauss {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- double[][] A; // The coefficients
- double[] b; // The RHS
- double pivot, factor;
- int n; // Number of variables
- int i, j, k;
- System.out.print("Number of unknows : ");
- n = in.nextInt(); // Read in problem size
- A = new double[n][n]; // Reserve space for coefficient matrix
- b = new double[n]; // Reserve space for RHS vector
- /* -----------------------------------
- Read in the rows, one row at a time
- ----------------------------------- */
- for (i = 0; i < n; i++) {
- // Read in row i's coefficients
- for (j = 0; j < n; j++)
- {
- System.out.print("A["+i+"]["+j+"] = ");
- A[i][j] = in.nextDouble();
- }
- // Read in row i's RHS
- System.out.print("B["+i+"] \t= ");
- b[i] = in.nextDouble();
- System.out.println();
- }
- /* -------------------------------
- The Gaussian elimination method
- ------------------------------- */
- for (i = 0; i < n; i++) {
- // Process row i
- /* ----------------
- (A) Select pivot
- ---------------- */
- pivot = A[i][i];
- /* -------------------
- (B) Normalize row i
- ------------------- */
- for (j = 0; j < n; j++)
- A[i][j] = A[i][j] / pivot;
- b[i] = b[i] / pivot;
- /* ---------------------
- (C) Sweep using row i
- --------------------- */
- for (k = 0; k < n; k++) {
- if ( k != i ) {
- factor = A[k][i];
- for (j = 0; j < n; j++)
- A[k][j] = A[k][j] - factor*A[i][j];
- b[k] = b[k] - factor*b[i];
- }
- }
- }
- System.out.println("Solution : ");
- for (i = 0; i < n; i++)
- System.out.println("x" + (i+1) + " = " + b[i]);
- /* -------------------------------------
- Soal dari ppt :
- 27 x1 + 6 x2 - x3 = 85 .....(1a)
- 6 x1 + 15 x2 + 2 x3 = 72 .....(1b)
- x1 + x2 + 54 x3 = 110 .....(1c)
- ------------------------------------- */
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement