0

I have the following code. I am trying to show a property with matrix inverses. Specifically:

inv(A*B) = inv(B)*inv(A)

I have written the following code:

disp("Verifying if inv(A*B) = inv(B)*inv(A)");
A = [1 2 ; 3 4]
B = [1 6 ; 5 7]
format short
E = A*B;
C = inv(E);
disp("inv(A*B) is :"), disp(rats(C));
D = inv(B)*inv(A);
disp("inv(B)*inv(A) is :"), disp(rats(D));
if (isequal(C,D))
    disp("Yes they are equal");
else 
    disp("No they are not equal");
endif

This is the output I am receiving:

Verifying if inv(A*B) = inv(B)*inv(A)
A =

   1   2
   3   4

B =

   1   6
   5   7

inv(A*B) is 
          1     -10/23
       -1/2      11/46
inv(B)*inv(A) is :
          1     -10/23
       -1/2      11/46
No they are not equal

How come the statement is not equal?

rayryeng
  • 102,964
  • 22
  • 184
  • 193
  • You are experiencing the perils of floating point, a plague that haunts all programmers. See the post I marked as duplicate for more details. Though you may have relationships where their calculations yield that they should theoretically be equal, floating point precision towards the tail end of each result may not be equal, which is why you get the relationship to be false. As a testament to this theory, try doing `0.11 + 0.11 + 0.12 == 0.34` in MATLAB, and see if it's true. You'll probably be surprised! To circumvent this, compare with a **threshold**. Again, see the post for more details. – rayryeng Dec 07 '14 at 06:43
  • 1
    And just to emphasize what @rayryeng said: this haunts _all programmers_. You will have exactly the same problem in every other programming language. – Stewie Griffin Dec 07 '14 at 07:53

0 Answers0