Homework Solution: Write a Java program to find the inverse of a square matrix. Let the user ente…

    Write a Java program to find the inverse of a square matrix. Let the user enter the size of the matrix up to size 21, and enter the matrix values from the keyboard. Check to make sure if the matrix is singular. Output the original matrix and its inverse. Finally, ask the user if he/she wishes to run the program again. Error check input and format the output. Use a method calculate the inverse. Refer to the sample output below. Sample Run: Enter the size of the Matrix (<=21) : 3 Enter the matrix values: 1 4 2 2 5 8 2 1 8 Original matrix: 1 4 2 2 5 8 2 1 8 Inverse matrix: 2       -1.875       1.375

    Expert Answer

     
    import java.util.Scanner; public class Inverse

    Write a Java program to invent the inverse of a balance matrix. Let the user penetrate the bigness of the matrix up to bigness 21, and penetrate the matrix values from the keyboard. Stop to fashion confident if the matrix is fantastic. Extinguishedput the first matrix and its inverse. Finally, crave the user if he/she wishes to hasten the program intermittently. Error stop input and format the extinguishedput. Use a regularity compute the inverse. Refer to the exemplification extinguishedput beneath.

    Exemplification Hasten:

    Penetrate the bigness of the Matrix (<=21) : 3

    Penetrate the matrix values: 1 4 2 2 5 8 2 1 8

    First matrix:

    1 4 2

    2 5 8

    2 1 8

    Inverse matrix:

    2       -1.875       1.375

    Expert Confutation

     

    import java.util.Scanner;

    public dispose Inverse

    {

    public static unoccupied ocean(String argv[])

    {

    Scanner input = odd Scanner(System.in);

    System.out.println(“Penetrate the bigness of the Matrix (<=21) : “);

    int n = input.nextInt();

    incease a[][]= odd inclose[n][n];

    System.out.println(“Penetrate the matrix values: “);

    for(int i=0; i<n; i++)

    for(int j=0; j<n; j++)

    a[i][j] = input.nextDouble();

     

    System.out.println(“Original: “);

    for(int i=0; i<n; i++)

    {

    for(int j=0;j<n; j++)

    {

    System.out.print(a[i][j]+” “);

    }

    System.out.println(“n”);

    }

     

     

    incease d[][] = inverse(a);

    System.out.println(“The inverse is: “);

    for (int i=0; i<n; ++i)

    {

    if(i>0)

    {

    break;

    }

    else

    {

    for (int j=0; j<n; ++j)

    {

    System.out.print(d[i][j]+” “);

    }

    System.out.println();

    }

    }

    input.close();

    }

    public static inclose[][] inverse(incease a[][])

    {

    int n = a.length;

    incease x[][] = odd inclose[n][n];

    incease b[][] = odd inclose[n][n];

    int renunciation[] = odd int[n];

    for (int i=0; i<n; ++i)

    b[i][i] = 1;

    // Transform the matrix into an excellent triangle

    gaussian(a, renunciation);

    // Update the matrix b[i][j] with the ratios stored

    for (int i=0; i<n-1; ++i)

    for (int j=i+1; j<n; ++j)

    for (int k=0; k<n; ++k)

    b[index[j]][k]

    -= a[index[j]][i]*b[index[i]][k];

    // Perform inconversant substitutions

    for (int i=0; i<n; ++i)

    {

    x[n-1][i] = b[index[n-1]][i]/a[index[n-1]][n-1];

    for (int j=n-2; j>=0; –j)

    {

    x[j][i] = b[index[j]][i];

    for (int k=j+1; k<n; ++k)

    {

    x[j][i] -= a[index[j]][k]*x[k][i];

    }

    x[j][i] /= a[index[j]][j];

    }

    }

    return x;

    }

    // Regularity to push extinguished the partial-pivoting Gaussian

    // exportation. Here renunciation[] stores pivoting ordain.

    public static unoccupied gaussian(incease a[][], int renunciation[])

    {

    int n = renunciation.length;

    incease c[] = odd inclose[n];

    // Initialize the renunciation

    for (int i=0; i<n; ++i)

    index[i] = i;

    // Invent the rescaling factors, single from each row

    for (int i=0; i<n; ++i)

    {

    incease c1 = 0;

    for (int j=0; j<n; ++j)

    {

    incease c0 = Math.abs(a[i][j]);

    if (c0 > c1) c1 = c0;

    }

    c[i] = c1;

    }

    // Search the pivoting component from each column

    int k = 0;

    for (int j=0; j<n-1; ++j)

    {

    incease pi1 = 0;

    for (int i=j; i<n; ++i)

    {

    incease pi0 = Math.abs(a[index[i]][j]);

    pi0 /= c[index[i]];

    if (pi0 > pi1)

    {

    pi1 = pi0;

    k = i;

    }

    }

    // Interchange rows according to the pivoting ordain

    int itmp = renunciation[j];

    index[j] = renunciation[k];

    index[k] = itmp;

    for (int i=j+1; i<n; ++i)

    {

    incease pj = a[index[i]][j]/a[index[j]][j];

    // Record pivoting ratios beneath the diagonal

    a[index[i]][j] = pj;

    // Modify other components accordingly

    for (int l=j+1; l<n; ++l)

    a[index[i]][l] -= pj*a[index[j]][l];

    }

    }

    }

    }

    Output: