lettura simple

Inverse matrix in Matlab

In this lesson, I will explain how to calculate the inverse matrix of a square or rectangular matrix using Matlab.

What is the inverse matrix? A matrix M is said to be invertible if there exists another matrix called the inverse matrix M-1 such that the product of the two matrices is an identity matrix, which is a matrix with 1s on the main diagonal and 0s elsewhere. For example $$ M \cdot M^{-1} = I $$

Let me provide you with a practical example.

Define a square matrix with two rows and two columns.

>> M=[1 2;3 4]
M =
1 2
3 4

Calculate the inverse matrix M-1 of matrix M using the function inv()

>> inv(M)
ans =
-2.00000 1.00000
1.50000 -0.50000

Now multiply matrix M with its inverse inv(M).

The final result is an identity matrix ( unit matrix ).

>> M*inv(M)
ans =
1.00000 0.00000
0.00000 1.00000

You can use the inv() function to calculate the inverse matrix only if matrix M is a square matrix.

Note. Not all square matrices are invertible. For example, if a square matrix has a determinant equal to zero, then it is not invertible and does not have an inverse matrix. When a matrix is not invertible, the inv() function returns an error message "Warning: matrix is singular to working precision". After calculating the inverse matrix, verify that the product M*inv(M) is indeed an identity matrix.

How do you calculate the inverse matrix of rectangular matrices?

In Matlab, you can also calculate the inverse matrix of a rectangular matrix.

In this case, you need to use the pseudo inverse function pinv()

For example, define a rectangular matrix M2

>> M2=[1 2 3 ; 4 5 6]
M2 =
1 2 3
4 5 6

This is a 2x3 matrix with two rows and three columns.

Now calculate the inverse matrix of matrix M2 using the pinv() function

>> pinv(M2)
ans =
-0.94444 0.44444
-0.11111 0.11111
0.72222 -0.22222

Matlab returns the inverse matrix of M2.

To make sure it is the inverse matrix, multiply the rectangular matrix M2 by its inverse matrix pinv(M2).

>> M2*pinv(M2)
ans =
1.00000 -0.00000
0.00000 1.00000

The result of the product M*pinv(M2) is an identity matrix.

Therefore, the result is correct.

Note. In Matlab, you can use the pinv() function to calculate the inverse matrix of both square and rectangular matrices. So, you can use pinv() instead of inv() if the matrix is square. The result is always the same.
The difference between the functions inv() and pinv()
On the other hand, you can only use the inv() function on square matrices.




Report a mistake or post a question




FacebookTwitterLinkedinLinkedin