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

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

0       0.25     -0.25

-0.5   0.4375   -0.1875

Run Again (Y/N): N

Expert Answer

 

Explanation:
This is related to matrix inverse and corresponding matrix values in the double format.
import java.util.Scanner;
public class TestInverseMatrix {
public static void main(String argvSt[]) {
String objChoice = “Y”;
while (objChoice.equalsIgnoreCase(objChoice)) {
Scanner inputObjScanner = new Scanner(System.in);
System.out.println(“Please provide the dimension of matrix: “);
int nLength = inputObjScanner.nextInt();
if (nLength > 21) {
System.exit(0);
}
double aObj[][] = new double[nLength][nLength];
System.out.println(“Please give the elements of matrix: “);
for (int iObj = 0; iObj < nLength; iObj++)
for (int jObj = 0; jObj < nLength; jObj++)
aObj[iObj][jObj] = inputObjScanner.nextDouble();
double dObj[][] = invert(aObj);
System.out.println(“Inverse matrix: “);
for (int iObj = 0; iObj < nLength; ++iObj) {
for (int jObj = 0; jObj < nLength; ++jObj) {
System.out.print(dObj[iObj][jObj] + ” “);
}
System.out.println();
}
Scanner chScanner = new Scanner(System.in);
System.out.println(“Run Again (Y/N): “);
objChoice = chScanner.next();
if(objChoice.equalsIgnoreCase(“N”)){
System.exit(0);
}
}
}
public static double[][] invert(double aObj[][]) {
int nObjLen = aObj.length;
double xObj[][] = new double[nObjLen][nObjLen];
double bObj[][] = new double[nObjLen][nObjLen];
int indexObj[] = new int[nObjLen];
for (int iObj = 0; iObj < nObjLen; ++iObj)
bObj[iObj][iObj] = 1;
gaussian(aObj, indexObj);
for (int iObj = 0; iObj < nObjLen – 1; ++iObj)
for (int jObj = iObj + 1; jObj < nObjLen; ++jObj)
for (int kObj = 0; kObj < nObjLen; ++kObj)
bObj[indexObj[jObj]][kObj] -= aObj[indexObj[jObj]][iObj] * bObj[indexObj[iObj]][kObj];
for (int iObj = 0; iObj < nObjLen; ++iObj) {
xObj[nObjLen – 1][iObj] = bObj[indexObj[nObjLen – 1]][iObj] / aObj[indexObj[nObjLen – 1]][nObjLen – 1];
for (int jObj = nObjLen – 2; jObj >= 0; –jObj) {
xObj[jObj][iObj] = bObj[indexObj[jObj]][iObj];
for (int kObj = jObj + 1; kObj < nObjLen; ++kObj) {
xObj[jObj][iObj] -= aObj[indexObj[jObj]][kObj] * xObj[kObj][iObj];
}
xObj[jObj][iObj] /= aObj[indexObj[jObj]][jObj];
}
}
return xObj;
}
public static void gaussian(double aObj[][], int indexObj[]) {
int nObjLen = indexObj.length;
double cObj[] = new double[nObjLen];
for (int iObj = 0; iObj < nObjLen; ++iObj)
indexObj[iObj] = iObj;
for (int iObj = 0; iObj < nObjLen; ++iObj) {
double c1Obj = 0;
for (int jObj = 0; jObj < nObjLen; ++jObj) {
double objC0 = Math.abs(aObj[iObj][jObj]);
if (objC0 > c1Obj)
c1Obj = objC0;
}
cObj[iObj] = c1Obj;
}
int kObj = 0;
for (int jObj = 0; jObj < nObjLen – 1; ++jObj) {
double pi1Obj = 0;
for (int iObj = jObj; iObj < nObjLen; ++iObj) {
double objPi0 = Math.abs(aObj[indexObj[iObj]][jObj]);
objPi0 /= cObj[indexObj[iObj]];
if (objPi0 > pi1Obj) {
pi1Obj = objPi0;
kObj = iObj;
}
}
int itmpObj = indexObj[jObj];
indexObj[jObj] = indexObj[kObj];
indexObj[kObj] = itmpObj;
for (int iObj = jObj + 1; iObj < nObjLen; ++iObj) {
double pjObj = aObj[indexObj[iObj]][jObj] / aObj[indexObj[jObj]][jObj];
aObj[indexObj[iObj]][jObj] = pjObj;
for (int lObj = jObj + 1; lObj < nObjLen; ++lObj)
aObj[indexObj[iObj]][lObj] -= pjObj * aObj[indexObj[jObj]][lObj];
}
}
}
}
Output Screenshot:

Buy Essay
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.

24/7 Customer Support

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

Place your order

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.

STEP 1

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.

STEP 2

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.

STEP 3

Editing (OUR PART)

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

STEP 4

Receive your Paper

After Editing, your paper will be sent to you via email.