# Answered Essay: 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

import java.util.Scanner;

public class Inverse

{

public static void main(String argv[])

{

Scanner input = new Scanner(System.in);

System.out.println(“Enter the size of the Matrix (<=21) : “);

int n = input.nextInt();

double a[][]= new double[n][n];

System.out.println(“Enter 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”);

}

double 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 double[][] inverse(double a[][])

{

int n = a.length;

double x[][] = new double[n][n];

double b[][] = new double[n][n];

int index[] = new int[n];

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

b[i][i] = 1;

// Transform the matrix into an upper triangle

gaussian(a, index);

// 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 backward 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;

}

// Method to carry out the partial-pivoting Gaussian

// elimination. Here index[] stores pivoting order.

public static void gaussian(double a[][], int index[])

{

int n = index.length;

double c[] = new double[n];

// Initialize the index

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

index[i] = i;

// Find the rescaling factors, one from each row

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

{

double c1 = 0;

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

{

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

if (c0 > c1) c1 = c0;

}

c[i] = c1;

}

// Search the pivoting element from each column

int k = 0;

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

{

double pi1 = 0;

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

{

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

pi0 /= c[index[i]];

if (pi0 > pi1)

{

pi1 = pi0;

k = i;

}

}

// Interchange rows according to the pivoting order

int itmp = index[j];

index[j] = index[k];

index[k] = itmp;

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

{

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

// Record pivoting ratios below the diagonal

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

// Modify other elements accordingly

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

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

}

}

}

}

Output:

Calculate your paper price
Pages (550 words)
Approximate price: -

Help Me Write My Essay - Reasons:

Best Online Essay Writing Service

We strive to give our customers the best online essay writing experience. We Make sure essays are submitted on time and all the instructions are followed.

Our Writers are Experienced and Professional

Our essay writing service is founded on professional writers who are on stand by to help you any time.

Free Revision Fo all Essays

Sometimes you may require our writers to add on a point to make your essay as customised as possible, we will give you unlimited times to do this. And we will do it for free.

Timely Essay(s)

We understand the frustrations that comes with late essays and our writers are extra careful to not violate this term. Our support team is always engauging our writers to help you have your essay ahead of time.

Customised Essays &100% Confidential

Our Online writing Service has zero torelance for plagiarised papers. We have plagiarism checking tool that generate plagiarism reports just to make sure you are satisfied.

Our agents are ready to help you around the clock. Please feel free to reach out and enquire about anything.

Try it now!

## Calculate the price of your order

Total price:
\$0.00

How it works?

Follow these simple steps to get your paper done

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

HOW OUR ONLINE ESSAY WRITING SERVICE WORKS

Let us write that nagging essay.

## Submit Your Essay/Homework Instructions

By clicking on the "PLACE ORDER" button, tell us your requires. Be precise for an accurate customised essay. You may also upload any reading materials where applicable.

Pick A & Writer

Our ordering form will provide you with a list of writers and their feedbacks. At step 2, its time select a writer. Our online agents are on stand by to help you just in case.

Editing (OUR PART)

At this stage, our editor will go through your essay and make sure your writer did meet all the instructions.