How to Find Inverse of Matrix in MATLAB: Syntax and Examples
In MATLAB, you find the inverse of a matrix using the
inv() function by passing the matrix as an argument, like inv(A). This returns the inverse matrix if it exists, which means the matrix must be square and non-singular.Syntax
The basic syntax to find the inverse of a matrix A in MATLAB is:
B = inv(A): Returns the inverse of matrixAand stores it inB.
The matrix A must be square (same number of rows and columns) and invertible (non-singular).
matlab
B = inv(A);
Example
This example shows how to create a 2x2 matrix and find its inverse using inv(). It also verifies the result by multiplying the matrix by its inverse, which should give the identity matrix.
matlab
A = [4 7; 2 6]; B = inv(A); I = A * B; disp('Inverse matrix B:'); disp(B); disp('Product of A and B (should be identity):'); disp(I);
Output
Inverse matrix B:
0.6 -0.7
-0.2 0.4
Product of A and B (should be identity):
1.0000 0
0 1.0000
Common Pitfalls
Common mistakes when finding a matrix inverse in MATLAB include:
- Trying to invert a non-square matrix (e.g., 2x3), which is not possible.
- Inverting a singular matrix (determinant zero), which has no inverse.
- Using
inv()for solving linear systems instead of the recommended backslash operator\for better accuracy and performance.
Example of a wrong and right approach:
matlab
% Wrong: Inverting a singular matrix A = [1 2; 2 4]; B = inv(A); % This will give a warning or error % Right: Check determinant before inversion if det(A) ~= 0 B = inv(A); else disp('Matrix is singular and cannot be inverted.'); end
Output
Matrix is singular and cannot be inverted.
Quick Reference
| Operation | Description |
|---|---|
| inv(A) | Returns inverse of square matrix A |
| det(A) | Computes determinant of A to check invertibility |
| A \ b | Solves linear system Ax = b without explicit inverse |
| eye(n) | Creates n x n identity matrix |
Key Takeaways
Use inv(A) to find the inverse of a square, non-singular matrix A in MATLAB.
Always check if the matrix is invertible by ensuring it is square and has a non-zero determinant.
Avoid using inv() to solve linear equations; prefer the backslash operator \ for better accuracy.
Multiplying a matrix by its inverse returns the identity matrix.
Singular matrices cannot be inverted and will cause errors with inv().