info@meritcampus.com    +91-85006-22255
...
`

Write a program to get the inverse of 2 x 2 matrix.

Input (Two Dimensional matrix) Output (Two Dimensional matrix)
`5  82   4` `1.0  -2.0-0.5  1.25`
`20  452  5` `0.5  -4.5-0.2  2.0`
`-1  23  -7` `-7.0  -2.0-3.0  -1.0`
`1  22  3` `-3.0  2.02.0  -1.0`

``` class FindTheInverseOf2X2Matrix {     public static void main(String s[])    {        int input[][] = {{20, 45}, {2, 5}};        double output[][] = getInverse(input);        for (double[] array1d : output) {        for (double element : array1d) {        System.out.print(element + "   ");        }        System.out.println();        }    }     public static double[][] getInverse(int matrix[][]) { ```
```     }          public static int getDeterminant(int matrix[][]) {     int result = 0; ```
```     return result;     } } ```
Topic:

### User comments below.All of them might not be correct.

Here in this program we need to find the inverse of matrix.. To do it we must follow some steps
1) First we have to find the determinant of matrix.. As it is a 2x2 matrix we have the formula det = ad - bc.. In coding (matrix[0][0] * matrix[1][1]) - (matrix[0][[1] * matrix[1][0]).. Get this from the getDeterminant() method calling it from getInverse() method.

2) Then in getInverse() method calculate the inverse of matrix.. Take 2D double array to store the inverse of matrix(double[][] res = new double[2][2];) and store the values as follows..
res[0][0] = (double)matrix[1][1] / det;
res[0][1] = -(double)matrix[0][1] / det;
res[1][0] = -(double)matrix[1][0] / det;
res[1][1] = (double)matrix[0][0] / det;
and return the resultant matrix

Posted by Gopinath Manchikanti    2015-03-14 05:58:21

To find Inverse of a given matrix formula is as below:
If A is given matrix of order 2*2 as follows:
A B
C D
Then inverse of matrix A will be:
1/|A| [ D -B ]
[  -C A]
--so,first we find determinant of matrix A as follows:
Int d=matrix[0][0]*matrix[1][1]-matrix[0][1]* matrix[1][0].

--then we have to divide each element of matrix in formula by determinant of matrix stored in d.

-- matrix[0][0]=(double) matrix[1][1]
matrix[0][1]=-(double) matrix[0][1]
matrix[1][0]=-(double) matrix[1][0]
matrix[1][1]=(double) matrix[0][0]

Then print the each element of matrix by using for loop in proper order.

Posted by Mânïshå Mùlchåndânï    2015-03-15 10:32:32

This dose is now closed and the winners are Gopinath Manchikanti,  for 'First Correct Comment', Mânïshå Mùlchåndânï,  for 'Second Correct Comment'. The 'lucky liker' is Cool Pawan. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-03-16 01:48:16