Different method of calculation the same thing give different values?
1 次查看(过去 30 天)
显示 更早的评论
Dear reader,
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
or manually:
C = 1/(3*4-2*1) * [4 -2 ; -1 3]
Now these answers should be the same, but if one would calculate the difference, this gives:
B-C
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
0 个评论
采纳的回答
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!