Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the inverse of a Matrix in MATLAB, is inv(A) or A\eye(size(A)) more precise? [duplicate]

The title explains it already. If I need to find an inverse of a matrix, is there any reason I should use A\eye(size(A)) instead of inv(A)? And before you ask: Yes, I really need the inverse, not only for calculations.

PS:

isequal(inv(A), A\eye(size(A)))
ans =
 0

So which one is more precise?

UPDATE: This question was closed as it appeard to be a duplicate of the question "why is inv in MATLAB so slow and inaccurate". This question here differs significantly by not addressing the speed, nor the accuarcy of the function inv but the difference of inv and .\eye to calculate the true inverse of a matrix.

like image 448
rst Avatar asked Jan 27 '16 10:01

rst


People also ask

How do you find the inverse of a matrix in Matlab?

Y = inv( X ) computes the inverse of square matrix X . X^(-1) is equivalent to inv(X) .

How do I find the inverse of a matrix matrix?

To find the inverse of a 2x2 matrix: swap the positions of a and d, put negatives in front of b and c, and divide everything by the determinant (ad-bc).

How do you find the inverse of a function in Matlab?

g = finverse( f ) returns the inverse of function f , such that f(g(x)) = x . If f contains more than one variable, use the next syntax to specify the independent variable. g = finverse( f , var ) uses the symbolic variable var as the independent variable, such that f(g(var)) = var .

How do I find the inverse of a 3x3 matrix?

To find the inverse of a 3x3 matrix, first calculate the determinant of the matrix. If the determinant is 0, the matrix has no inverse. Next, transpose the matrix by rewriting the first row as the first column, the middle row as the middle column, and the third row as the third column.


3 Answers

Let's disregard performance (speed) and best practice for a bit.


eps(n) is a command that returns the distance to the next larger double precision number from n in MATLAB. So, eps(1) = 2.2204e-16 means that the first number after 1 is 1 + 2.2204e-16. Similarly, eps(3000) = 4.5475e-13. Now, let's look at the precision of you calculations:

n = 100;
A = rand(n);
inv_A_1 = inv(A);
inv_A_2 = A \ eye(n);

max(max(abs(inv_A_1-inv_A_2)))
ans =
   1.6431e-14

eps(127) = 1.4211e-14
eps(128) = 2.8422e-14

For integers, the largest number you can use that has an accuracy higher than the max difference between your two matrices is 127.

Now, let's check how the accuracy when we try to recreate the identity matrix from the two inverse matrices.

error_1 = max(max(abs((A\eye(size(A))*A) - eye(size(A)))))
error_1 =
   3.1114e-14
error_2 = max(max(abs((inv(A)*A) - eye(size(A)))))
error_2 =
   2.3176e-14

The highest integer with a higher accuracy than the maximum difference between the two approaches is 255.

In summary, inv(A) is more accurate, but once you start using the inverse matrices, they are for all intended purposes identical.


Now, let's have a look at the performance of the two approaches:

n = fix(logspace(1,3,40));
for i = 1:numel(n)
A = rand(round(n(i)));
t1(i) = timeit(@()inv(A));
t2(i) = timeit(@()A\eye(n(i)));
end
loglog(n,[t1;t2])

enter image description here

It appears that which of the two approaches is fastest is dependent on the matrix size. For instance, using inv is slower for n = 255, but faster for n = 256.


In summary, choose approach based on what's important to you. For most intended purposes, the two approaches are identical.

Note that svd and pinv may be of interest if you're working with badly scaled matrices. If it's really really important you should consider the Symbolic toolbox.


I know you said that you "actually need the inverse", but I can't let this go unsaid: Using inv(A)*b is never the best approach for solving a linear equation! I won't explain further as I think you know this already.

like image 107
Stewie Griffin Avatar answered Oct 21 '22 23:10

Stewie Griffin


If you need the inverse, you should use inv.

The inverse is calculated via LU decomposition, whereas the backslash operator mldivide calculates the solution to your linear system using different methods depending on the properties of your matrix A (see https://scicomp.stackexchange.com/a/1004), which can yield less accurate results for the inverse.

It should be noted that if you want to solve a linear system, the calculation is likely going to be much faster and more accurate using mldivide(\). The MATLAB documentation of inv is basically one big warning not to use inv to solve linear systems.

like image 23
dasdingonesin Avatar answered Oct 22 '22 00:10

dasdingonesin


Just a way trying to check this, not sure if it's completely helpful though: multiply your inverse matrix result back with it's original version and check the deviation from the identity matrix:

A = rand( 111 );
E1 = abs( (A\eye(size(A) ) * A ) - eye( size(A) ) );
E2 = abs( ( inv(A) * A )         - eye( size(A) ) );
mean(E1(:))
mean(E2(:))

inv seems to be more accurate as I would have expected. Maybe somebody can re-evaluate this. ;)

like image 2
Matthias W. Avatar answered Oct 21 '22 22:10

Matthias W.