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
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
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
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by