# Different method of calculation the same thing give different values?

조회 수: 3(최근 30일)
Ariwan Abdollah 2021년 12월 10일
편집: David Goodmanson 2021년 12월 11일
I have a question regarding the 'exact' value:
Say we have
A = [3 2 ; 1 4];
And for this we'd want to calculate the inverse. This could be done through:
B = A^-1
B = 2×2
0.4000 -0.2000 -0.1000 0.3000
or manually:
C = 1/(3*4-2*1) * [4 -2 ; -1 3]
C = 2×2
0.4000 -0.2000 -0.1000 0.3000
Now these answers should be the same, but if one would calculate the difference, this gives:
B-C
ans = 2×2
1.0e+-16 * -0.5551 0.2776 0.1388 -0.5551
Which is not equal to zero. Yes it is almost zero, but because it is not exactly zero, this causes problems down the line.
So which one is (more) correct ? And what causes this issue?
Kind regards,
Ariwan

댓글을 달려면 로그인하십시오.

### 채택된 답변

David Goodmanson 2021년 12월 10일
편집: David Goodmanson 2021년 12월 11일
Hi Ariwan,
The differences here are all down at the level of machine precision. Differences on the order of 1e-16 happen all the time and are not considered bad behavior, and sometimes it's difficult to say which result might be considered 'more correct'. Going to format hex shows how the values are stored:
format hex
A = [3 2; 1 4];
B = A^-1
C = inv(A)
D = A\eye(2,2)
E = 1/(3*4-2*1)*[4 -2; -1 3]
F = [4 -2; -1 3]/10
format short
B =
3fd9999999999999 bfc9999999999999
bfb9999999999999 3fd3333333333333
C =
3fd9999999999999 bfc9999999999999
bfb9999999999999 3fd3333333333333
D =
3fd9999999999999 bfc9999999999999
bfb9999999999999 3fd3333333333333
E =
3fd999999999999a bfc999999999999a
bfb999999999999a 3fd3333333333334
F =
3fd999999999999a bfc999999999999a
bfb999999999999a 3fd3333333333333
As you can see, none of the values in the inverse are stored exacty, as opposed to, say
10
ans =
4024000000000000
All the results for the inverse disagree just in the last decimal place. Note that E and F, which differ only in the order of multiplication, are different.
Since the values of A^-1 are repeating decimals, you could say that B,C,D do better than E and F. But this is a case where the determinant = 10, so it's pretty easy to see what's going on. Now consider a more common situation:
format hex
A = [3.1 2.2; 1.1 4.1];
B = A^-1
C = inv(A)
D = A\eye(2,2)
E = 1/(3.1*4.1 -2.2*1.1) * [4.1 -2.2; -1.1 3.1]
F = [4.1 -2.2 ; -1.1 3.1]/10.29
format short
B =
3fd9801fd831c1ce bfcb5dcac28ccffe
bfbb5dcac28ccffe 3fd347e62057928a
C =
3fd9801fd831c1ce bfcb5dcac28ccffe
bfbb5dcac28ccffe 3fd347e62057928a
D =
3fd9801fd831c1cd bfcb5dcac28ccffe
bfbb5dcac28ccffd 3fd347e62057928a
E =
3fd9801fd831c1cd bfcb5dcac28ccffd
bfbb5dcac28ccffd 3fd347e620579289
F =
3fd9801fd831c1ce bfcb5dcac28ccffd
bfbb5dcac28ccffd 3fd347e620579289
Here the determinant is 10.29. Would you be prepared to say which of these results is better, based on the value of the last digit? I don't think I would.
##### 댓글 수: 1표시숨기기 없음
Ariwan Abdollah 2021년 12월 10일
Hi David,
Thank you for your response, this makes things very clear.
I'm going to find out then what a better way to deal with these issues is.
Cheers, Ariwan

댓글을 달려면 로그인하십시오.

R2020a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!