Skip to content

MATLAB - Inverse of Matrix

MATLAB - Matrix Inverse and Linear Systems

Section titled “MATLAB - Matrix Inverse and Linear Systems”

In linear algebra, the inverse of a square matrix A, denoted by A⁻¹, is a matrix such that the following relationship holds:

A * A⁻¹ = A⁻¹ * A = I
(where I is the identity matrix)

A matrix must be square (have the same number of rows and columns) and non-singular to have an inverse. A matrix is singular if its determinant is zero. A non-singular matrix is also called an invertible matrix.

Calculating the Inverse: The inv() Function

Section titled “Calculating the Inverse: The inv() Function”

In MATLAB, you can calculate the inverse of a matrix using the inv() function. The inverse of a matrix A is simply inv(A).

Create a script file and type the following code to define a matrix and compute its inverse.

% Define a 3x3 square matrix
A = [1, 4, 2;
2, 5, 1;
3, 6, 0];
% Calculate the inverse of A
A_inv = inv(A);
% Display the original and inverted matrices
disp('Original Matrix A:');
disp(A);
disp('Inverse Matrix A_inv:');
disp(A_inv);
% Verification: A * A_inv should be the identity matrix
identity_check = A * A_inv;
disp('Verification (A * A_inv):');
disp(identity_check);

When you run the script, it will display the following result. Note that due to floating-point arithmetic, the verification matrix might have very small non-zero values instead of perfect zeros.

Original Matrix A:
1 4 2
2 5 1
3 6 0
Inverse Matrix A_inv:
-2.0000 4.0000 -2.0000
1.0000 -2.0000 1.0000
-1.0000 2.0000 -1.0000
Verification (A * A_inv):
1.0000 0 0
0 1.0000 0
-0.0000 -0.0000 1.0000

Best Practice: Solving Linear Systems without inv()

Section titled “Best Practice: Solving Linear Systems without inv()”

A common use for a matrix inverse is to solve a system of linear equations of the form Ax = b. While it’s mathematically correct to say that x = A⁻¹b, this is not the recommended way to solve it in MATLAB or any numerical computing environment.

Calculating the explicit inverse with inv(A) is computationally more expensive and often less accurate than using matrix division with the backslash operator (\).

Let’s solve the system Ax = b where A is our previous matrix and b is a column vector.

% Define the same matrix A
A = [1, 4, 2; 2, 5, 1; 3, 6, 0];
% Define the vector b
b = [6; 7; 6];
% --- Inefficient Method ---
% Using the inverse function
x_inefficient = inv(A) * b;
% --- Recommended Method ---
% Using the backslash operator (matrix left division)
x_efficient = A \ b;
disp('Solution using inv(A)*b:');
disp(x_inefficient);
disp('Solution using A\b:');
disp(x_efficient);

Both methods yield the same result for this simple case, but the backslash operator is more robust and efficient, especially for large matrices. It uses different algorithms (like LU decomposition) optimized for solving the system directly.

Solution using inv(A)*b:
2.0000
1.0000
-1.0000
Solution using A\b:
2.0000
1.0000
-1.0000

The inverse of a matrix does not exist if its determinant is zero. You can check the determinant using the det() function.

A_singular = [1, 2; 2, 4];
det_A_singular = det(A_singular);
disp(['Determinant of A_singular is: ', num2str(det_A_singular)]);
% Trying to invert a singular matrix will result in a warning.
% inv(A_singular)

In practice, numerical issues mean a determinant might be a very small number close to zero rather than exactly zero. A better way to check if a matrix is close to being singular (i.e., ill-conditioned) is to use the cond() function to calculate the condition number. A very large condition number indicates that the matrix is ill-conditioned, and the solution to Ax=b may be inaccurate.

cond_A = cond(A);
cond_A_singular = cond(A_singular);
disp(['Condition number of A: ', num2str(cond_A)]);
disp(['Condition number of A_singular: ', num2str(cond_A_singular)]);

The output will show a small condition number for our well-behaved matrix A and a very large (or Inf) condition number for the singular matrix A_singular.

  • Error: Matrix must be square. This occurs if you try to use inv() on a non-square matrix. The inverse is only defined for square matrices.
  • Warning: Matrix is close to singular or badly scaled. This warning appears when you use inv() or \ on a singular or ill-conditioned matrix. The results may be unreliable. Always check the condition number with cond() if you suspect this.
  • Incorrect Dimensions for Multiplication: When solving Ax=b, ensure that the number of rows in A matches the number of elements in b.