Find the inverse matrix in MATLAB, is inv (A) or A \ eye (size (A)) more accurate?

The title explains it already. If I need to find the inverse matrix, is there some reason why I should use A\eye(size(A))instead inv(A)? And before asking: yes, I really need the opposite, and not just for calculations.

PS:

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

So which one is more accurate?

The UPDATE . This question was closed because it appeared as a duplicate of the question " why is inv in MATLAB so slow and inaccurate ." This question here is significantly different from the fact that it does not address the speed or accuracy of the function inv, but differs from invand .\eyeto calculate the true inverse to the matrix.

+4
source share
3 answers

If you need the opposite, you should use inv.

The inverse is calculated by the LU decomposition, while the backslash operator mldividecalculates 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 may give less exact results for the converse.

, , , , , mldivide (\). MATLAB inv - inv .

+4

() .


eps(n) - , n MATLAB. , eps(1) = 2.2204e-16 , 1 1 + 2.2204e-16. , eps(3000) = 4.5475e-13. :

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

, , , , 127.

, .

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

, , 255.

, inv(A) , , .


:

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

, , . , inv n = 255, n = 256.


, , . .

, svd pinv , . , Symbolic toolbox.


, , " ", , : inv(A)*b ! , , , .

+7

, , : :

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 , . , - .;)

+2
source

Source: https://habr.com/ru/post/1626145/


All Articles