Matlab

Find Inverse of a Matrix in MATLAB

A matrix inverse can be defined as the matrix which when multiplied with the default original matrix results in an identity matrix. The output identity matrix contains ones at its diagonal and all remaining entities are zeros.

Finding the inverse of a matrix can be useful for different tasks, such as solving systems of linear equations, inverting transformations, and calculating determinants.

Finding the Inverse of a Matrix in MATLAB

MATLAB has two built-in functions for finding the inverse of a matrix: inv() and backslash.

MATLAB inv() Function

In MATLAB, to find matrix inverse generally inv(A) function is used. Now we will cover the details of this function and how we can use it in MATLAB code.

Syntax

The syntax for using the inv() function is:

B = inv(A);

where A is the input square matrix and B is the output matrix, which is the inverse of A.

Parameters

The inv() function takes a single parameter:

A: This is the input square matrix for which you want to calculate the inverse.

Return

The inv() function returns the inverse matrix B. If the input matrix A is invertible (non-singular), the function will calculate and return the inverse matrix. However, if the input matrix is singular or nearly singular, the function may not be able to compute the inverse accurately, and an error may be thrown.

Note that the inv() function should be used with caution because calculating the inverse of a matrix can be computationally difficult, especially for large matrices. In many cases, it is more efficient and numerically stable to solve linear systems of equations using the backslash operator (\) or other matrix factorization methods.

Example Code

For example, to find the inverse of the matrix A, you would use the following code:

A = [1 2; 3 4];

B = inv(A)

A screenshot of a computer Description automatically generated with medium confidence

Finding Inverse Using Backslash Operator

The backslash operator in MATLAB can also be used for matrix inverse calculations. However, the backslash operator is generally faster than the inv() function.

Example Code

Below MATLAB code uses the backslash operator for finding the inverse of the 2×2 square matrix:

A = [1 2; 3 4];

B = A\eye(2)

A picture containing text, screenshot, software, display Description automatically generated

Finding the Inverse of a 3×3 Matrix

Now we will find the inverse of the 3×3 matrix using the MATLAB inv() function:

A = [1 2 3; 4 5 6; 7 8 9];

B = inv(A)

A screenshot of a computer Description automatically generated with medium confidence

Conclusion

To find the inverse of a matrix in MATLAB we can use the inv() function or use the backslash. Both these can easily find the inverse of a 2×2 or 3×3 matrix. For more complex matrices it’s recommended to use the backslash. Because it is more efficient and numerically stable to solve linear systems of equations using the backslash operator.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.