Difference between inv() and pinv() functions in MATLAB


In this tutorial, we will discuss two important functions namely, "inv()" and "pinv()", used for matrix inversion in MATLAB. Since, the fundamental purpose of both the functions is the same, i.e. matrix inversion, but they are different from each other in many aspects. Hence, we will learn the important differences between 'inv()' and 'pinv()' functions.

What is 'inv()' Function?

In MATLAB, the 'inv()' function is a built-in function used to calculate the inverse of a non-singular square matrix.

Therefore, if 'M' is a non-singular square matrix, then the function 'inv(M)' will return a new matrix 'B' for which 'I = M * B'. Where, 'I' is called the identity matrix.

It is important to note that the 'inv()' function cannot be used to find the inverse of a singular or non-invertible matrix. If we apply the 'inv()' function to a singular matrix, then it will produce an inaccurate result. In short, the 'inv()' function is developed to handle non-singular matrices only.

Example

The following example demonstrates the use of the 'inv()' function to calculate the inverse of a square matrix −

% MATLAB code to find inverse of a non-singular square matrix
% Create a non-singular square matrix
M = [1, 2; 3, 4];

% Find the inverse using inv() function
B = inv(M);

% Display the original matrix and inverted matrix
disp('Orignal Matrix is: ');
disp(M);
disp('Inverted Matrix is: ');
disp(B);

Output

Orignal Matrix is: 
     1     2
     3     4

Inverted Matrix is: 
   -2.0000    1.0000
    1.5000   -0.5000

Example 2

Now, let us get another example to understand the operation of the 'inv' function for a singular matrix.

% MATLAB code to find inverse of a singular square matrix
% Create a singular square matrix
M = [1, 2; 3, 6];

% Find the inverse using inv() function
B = inv(M);

% Display the original matrix and inverted matrix
disp('Orignal Matrix is: ');
disp(M);
disp('Inverted Matrix is: ');
disp(B);

Output

Orignal Matrix is: 
     1     2
     3     6

Inverted Matrix is: 
   Inf   Inf
   Inf   Inf

Hence, it is clear that the 'inv()' function cannot be used to find the inverse of a singular matrix.

What is 'pinv()' Function?

In MATLAB, the 'pinv()' function is used to find the pseudo-inverse of a given matrix. The upside of this function is that it can be applied to both square and non-square matrices. The 'pinv()' function uses the floating-point arithmetic to compute the inverse of a given matrix.

The major advantage of the 'pinv()' function is that it can be used to calculate the inverse of both singular and non-singular matrices.

Example 3

The following MATLAB program demonstrates the use of 'pinv()' function to calculate the inverse of a singular matrix and a non-singular matrix.

% MATLAB code to find inverse of a non-singular square matrix
% Create a non-singular matrix
M1 = [1, 2, 3; 3, 4, 6];

% Create a singular matrix
M2 = [1, 2; 3, 6];

% Find the inverse of M1 and M2 using pinv() function
B1 = pinv(M1);
B2 = pinv(M2);

% Display the original matrices and inverted matrices
disp('Orignal Matrix M1 is: ');
disp(M1);
disp('Inverted Matrix B1 is: ');
disp(B1);
disp('Orignal Matrix M2 is: ');
disp(M2);
disp('Inverted Matrix B2 is: ');
disp(B2);

Output

Orignal Matrix M1 is: 
     1     2     3
     3     4     6

Inverted Matrix B1 is: 
   -2.0000    1.0000
    0.4615   -0.1538
    0.6923   -0.2308

Orignal Matrix M2 is: 
     1     2
     3     6

Inverted Matrix B2 is: 
    0.0200    0.0600
    0.0400    0.1200

Hence, it is clear that we can use the 'pinv' function to compute the inverse of a singular as well as non-singular matrix.

Now, after getting an overview of the 'inv()' function and the 'pinv()' function. Let us now discuss the important differences between them.

Differences Between inv() and Pinv() Functions

The following table highlights all the major differences between 'inv()' and 'pinv()' functions in MATLAB −

Parameter

inv() Function

pinv() Function

Purpose

The 'inv()' function is used to find the inverse of a non-singular square matrix.

The 'pinv()' function is used to find the inverse of a singular as well as non-singular matrix.

Type of matrix

The 'inv()' function can be used to calculate the inverse of a square matrix only.

The 'pinv()' function can be used to calculate the inverse of any type of matrix.

Result

The 'inv()' function returns the exact inverse of a matrix.

The 'pinv()' function returns the Moore-Penrose Pseudo Inverse of a matrix.

Execution speed

The 'inv()' function executes at faster speed.

The 'pinv()' function starts at slower speed.

Floating point arithmetic

The 'inv()' function does not use the floating point arithmetic.

The 'pinv()' function uses the floating point arithmetic.

Return value for singular matrices

When the given matrix is a singular matrix, then the 'inv' function returns errors.

When the given matrix is a singular matrix, the 'pinv' function returns a pseudo inverse of the matrix.

Efficiency

The 'inv()' function can be less efficient for large matrices.

The 'pinv' function is more efficient than the 'inv()' function.

Numerical stability

The 'inv' function can produce numerical instability for non-invertible matrices.

The 'pinv' function is more stable for non-invertible matrices.

Suitability

The 'inv' function is suitable to solve systems of linear equations with square matrices.

The 'pinv' function is suitable for solving underdetermined and overdetermined systems.

Conclusion

Both these MATLAB functions are used to calculate the inverse of a given matrix. The important difference between 'inv' and 'pinv' functions is that the 'inv' function can be used to calculate the inverse of a non-singular square matrix, while the 'pinv' function can be used to calculate the inverse of any type of matrix, including the singular matrices. Another important point to note about the 'inv' and 'pinv' functions is that the 'inv' function returns the true inverse of the given matrix, whereas the 'pinv' function returns the pseudo inverse of the given matrix.

In the above sections of this tutorial, we have explained the 'inv' and 'pinv' functions with the help of example MATLAB programs for both singular and non-singular matrices.

Updated on: 06-Sep-2023

157 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements