Question

java inverse square matrix

The inverse of a square matrix A is denoted A-1 , such that A × A-1 = I, where I is the identity matrix with all 1s on the diagonal and 0 on all other cells. The inverse of a 2×2 matrix A can be obtained using the following formula:       = c d a b A      − − − = − c a d b ad bc A 1 1 Implement the following method to obtain an inverse of 2×2 matrix: public static double[][] inverse(double[][] A) The method returns null if ad – bc is 0. Write a test program that prompts the user to enter a, b, c, d for a matrix, and displays its inverse matrix. Here are three sample runs: Enter a, b, c, d: 1 2 3 4 -2.0 1.0 1.5 -0.5 Enter a, b, c, d: 0.5 2 1.5 4.5 -6.0 2.6666666666666665 2.0 -0.6666666666666666 2 Enter a, b, c, d: 1 2 3 6 No inverse matrix

0 0
Add a comment Improve this question Transcribed image text
Answer #1

package Lab_5;

 

import java.util.Scanner;

 

public class InverseMatrix {

 

     public static void main(String[] args) {

         

          //initializing Scanner and variables

          Scanner input = new Scanner(System.in);

          System.out.print("Enter a, b, c, d: ");

         

          double [][] squareMatrix = new double [2][2];

         

          //taking input from the user

          for (int i = 0; i < 2; i++) {

               for (int j = 0; j < 2; j++) {

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

               }

          }

         

          //calling the inverse method

          double [][] returnInverseMatrix = inverse(squareMatrix);

         

         

          //checking to see if the inverse matrix is null or not

          if (returnInverseMatrix == null) {

               System.out.print("No inverse matrix");

          } else {

              

               //printing the inverse matrix according to the format

               for (int i = 0; i < 2; i++) {

                     for (int j = 0; j < 2; j++) {

                          System.out.print(squareMatrix [i][j] + " ");

                     }

                     System.out.print("\n");

               }

          }

         

          input.close();

     }

    

     public static double[][] inverse(double[][] A) {

         

          //calculating the denominator

          double denominator = (A[0][0]*A[1][1]) - (A[0][1]*A[1][0]);

         

          //checking if the denominator is 0. if it is zero then returning null

          if (denominator == 0) {

               return null;

          }

         

          //interchanging the values of a and d

          double temp;

          temp = A[0][0];

          A[0][0] = A[1][1];

          A[1][1] = temp;

         

          //making the value of b and c negative

          A[0][1] = -A[0][1];

          A[1][0] = -A[1][0];

         

         

          // dividing all the values of matrix of A by the denominator to get the inverse matrix

          for (int i = 0; i < 2; i++) {

               for (int j = 0; j < 2; j++) {

                     A[i][j] = (A[i][j]/denominator);

               }

          }

         

          //returning the matrix

          return A;

         

     }

}


answered by: anonymous
Add a comment
Know the answer?
Add Answer to:
java inverse square matrix
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 2. Inverse of a square matrix: Determine the inverse matrix [A™'] of the given square matrix...

    2. Inverse of a square matrix: Determine the inverse matrix [A™'] of the given square matrix [A] using the Gauss-Jordan Elimination Method (GEM), and verify that [A-!] [A] = I where I is the identity matrix. A = [ 1 4 -27 0 -3 -2 | -3 4 1

  • Hello! A solution to a question in the textbook Introduction to Java is unavailable and I...

    Hello! A solution to a question in the textbook Introduction to Java is unavailable and I can't comprehend it. It is problem # 3.3 in Chapter 3. The question states: Write a program that prompts the user to enter a,b,c,d,e, and f and display the results. If ad-bc is 0, report that "The equation has no solution." Enter a, b, c, d, e, f: 9.0 4.0 3.0 -5.0 -6.0 -21.0. x is -2.0 and y is 3.0 Enter a, b,...

  • [12] QUESTION 4 (a) Let A be an m × m symmetric matrix and P be an orthogonal matrix such the PAP...

    [12] QUESTION 4 (a) Let A be an m × m symmetric matrix and P be an orthogonal matrix such the PAP-D,where D is a diagonal matrix with the characteristic roots of A on the diagonal. Show that PA P is also a diagonal matrix. (b) Let A be an m × n matrix of rank m such that A = BC where B and C each has rank m. Show that (BC) CB. 16 STA4801/101/0/2019 (c) For the matrix...

  • Find the power of A for the matrix A = -1 0 0 0 - 1...

    Find the power of A for the matrix A = -1 0 0 0 - 1 0 0 0 0 OOOO OOOO 0 0 0 0 0 0 0 0 1 If A is the 2 x 2 matrix given by [aь A = cd and if ad - bc + 0, the inverse is given by d-b ad - bc Use the formula above to find the inverse of the 2 x 2 matrix (if it exists). (If an...

  • Algebra of matrices. 3. (a) If A is a square matrix, what does it mean to say that B is an inverse of A (b) Define AT....

    Algebra of matrices. 3. (a) If A is a square matrix, what does it mean to say that B is an inverse of A (b) Define AT. Give a proof that if A has an inverse, then so does AT. (c) Let A be a 3 x 3 matrix that can be transformed into the identity matrix by perform ing the following three row operations in the given order: R2 x 3, Ri R3, R3+2R1 (i) Write down the elementary...

  • [12] QUESTION 4 (a) Let A be an m × m symmetric matrix and P be...

    [12] QUESTION 4 (a) Let A be an m × m symmetric matrix and P be an orthogonal matrix such the PAP-D,where D is a diagonal matrix with the characteristic roots of A on the diagonal. Show that PA P is also a diagonal matrix. (b) Let A be an m × n matrix of rank m such that A = BC where B and C each has rank m. Show that (BC) CB. 16 STA4801/101/0/2019 (c) For the matrix...

  • (a) Write down the definition of the inverse of an n × n matrix A. (b)...

    (a) Write down the definition of the inverse of an n × n matrix A. (b) Using elimination, find the inverse of the matrix I. where a, b, c, d are real numbers such that a 0 and ad -be 0.

  • Please answer the 25,26, and 27 25) A square matrix A = (a ) is called...

    Please answer the 25,26, and 27 25) A square matrix A = (a ) is called diagonal if all its elements off the main diagonal are zero. That is, aij = 0 if j. (The matrix of Problem 24 is diagonal.) Show that a diagonal matrix is invertible if and only if each of its diagonal components is nonzero. 26.) Let a1i 0 0 0 a22 0 00ann be a diagonal matrix such that each of its diagonal components is...

  • use java please Exercise 3 Sum the Major Diagonal of a Matrix Complete exercise 8.2 on...

    use java please Exercise 3 Sum the Major Diagonal of a Matrix Complete exercise 8.2 on page 308 of the textbook. Note there are some modifications to the exercise in the steps below. 1. Create a new NetBeans project called Ex3SumDiagonal. 2. Generate a 4x4 array and fill with random doubles between 1 and 20. Display your array. Hint: The printf() method can be used to limit the decimal places displayed. The code below can be used to generate a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT