How to resolve the algorithm Gaussian elimination step by step in the Java programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Gaussian elimination step by step in the Java programming language

Table of Contents

Problem Statement

Solve   Ax=b   using Gaussian elimination then backwards substitution. A   being an   n by n   matrix. Also,   x and b   are   n by 1   vectors. To improve accuracy, please use partial pivoting and scaling.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Gaussian elimination step by step in the Java programming language

This Java code demonstrates Gaussian elimination for solving a system of linear equations Ax = b. Here's a detailed explanation:

  1. Gaussian elimination is a method for solving systems of linear equations by converting the coefficient matrix A into an upper triangular matrix using elementary row operations.

  2. The solve method takes two parameters:

    • a: Coefficient matrix of the system of linear equations.
    • b: Column matrix representing the constants in the equations.
  3. The method first checks if the dimensions of a and b are valid. The number of rows in a and b should be equal, and the number of columns in a should also be equal to the number of rows in b.

  4. It initializes some variables:

    • n: Number of rows/columns in a.
    • p: Number of columns in b.
    • det: Variable to store the determinant of the coefficient matrix. Initially set to 1.0.
  5. The method enters a loop that iterates over the rows of the matrix a from the first row to the second-to-last row (n - 1).

  6. Inside the loop, it performs partial pivoting:

    • It finds the row with the largest absolute value in the current column.
    • If this row is not the same as the current row, it swaps the rows and flips the sign of the determinant.
  7. Then, it performs row transformations to transform the current column into a unit vector, while simultaneously applying the same transformations to the b matrix.

  8. After the loop, the matrix a is in an upper triangular form.

  9. The method then iterates over the rows in reverse order, from the last row to the first row.

  10. For each row, it applies row transformations to eliminate the non-zero values below the diagonal in that column, effectively transforming a into a diagonal matrix. Simultaneously, it updates the b matrix accordingly.

  11. Finally, it calculates and returns the determinant of the coefficient matrix a.

  12. In the main method, the code creates two example matrices, a and b, and uses the solve method to solve the system of equations. It prints the result and compares it with a known solution.

In summary, this code implements Gaussian elimination with partial pivoting to solve a system of linear equations, providing both the solution and the determinant of the coefficient matrix.

Source code in the java programming language

import java.util.Locale;

public class GaussianElimination {
    public static double solve(double[][] a, double[][] b) {
        if (a == null || b == null || a.length == 0 || b.length == 0) {
            throw new IllegalArgumentException("Invalid dimensions");
        }
        
        int n = b.length, p = b[0].length;
        if (a.length != n || a[0].length != n) {
            throw new IllegalArgumentException("Invalid dimensions");
        }

        double det = 1.0;
        
        for (int i = 0; i < n - 1; i++) {
            int k = i;
            for (int j = i + 1; j < n; j++) {
                if (Math.abs(a[j][i]) > Math.abs(a[k][i])) {
                    k = j;
                }
            }
            
            if (k != i) {
                det = -det;
                
                for (int j = i; j < n; j++) {
                    double s = a[i][j];
                    a[i][j] = a[k][j];
                    a[k][j] = s;
                }

                for (int j = 0; j < p; j++) {
                    double s = b[i][j];
                    b[i][j] = b[k][j];
                    b[k][j] = s;
                }
            }
            
            for (int j = i + 1; j < n; j++) {
                double s = a[j][i] / a[i][i];
                for (k = i + 1; k < n; k++) {
                    a[j][k] -= s * a[i][k];
                }
                
                for (k = 0; k < p; k++) {
                    b[j][k] -= s * b[i][k];
                }
            }
        }
        
        for (int i = n - 1; i >= 0; i--) {
            for (int j = i + 1; j < n; j++) {
                double s = a[i][j];
                for (int k = 0; k < p; k++) {
                    b[i][k] -= s * b[j][k];
                }
            }
            double s = a[i][i];
            det *= s;
            for (int k = 0; k < p; k++) {
                b[i][k] /= s;
            }
        }
        
        return det;
    }
    
    public static void main(String[] args) {
        double[][] a = new double[][] {{4.0, 1.0, 0.0, 0.0, 0.0},
                                       {1.0, 4.0, 1.0, 0.0, 0.0},
                                       {0.0, 1.0, 4.0, 1.0, 0.0},
                                       {0.0, 0.0, 1.0, 4.0, 1.0},
                                       {0.0, 0.0, 0.0, 1.0, 4.0}};

        double[][] b = new double[][] {{1.0 / 2.0},
                                       {2.0 / 3.0},
                                       {3.0 / 4.0},
                                       {4.0 / 5.0},
                                       {5.0 / 6.0}};
                                       
        double[] x = {39.0 / 400.0,
                      11.0 / 100.0,
                      31.0 / 240.0,
                      37.0 / 300.0,
                      71.0 / 400.0};
                                       
        System.out.println("det: " + solve(a, b));
        

        for (int i = 0; i < 5; i++) {
            System.out.printf(Locale.US, "%12.8f %12.4e\n", b[i][0], b[i][0] - x[i]);
        }
    }
}


  

You may also check:How to resolve the algorithm Real constants and functions step by step in the Ruby programming language
You may also check:How to resolve the algorithm Window management step by step in the Java programming language
You may also check:How to resolve the algorithm Program name step by step in the Io programming language
You may also check:How to resolve the algorithm Reverse a string step by step in the PowerBASIC programming language
You may also check:How to resolve the algorithm Pseudo-random numbers/PCG32 step by step in the Ada programming language