Operations by matrix disease: 1.000.. - 1 is not equal to zero

2 次查看(过去 30 天)
Goodmorning everyone, I'm having some trouble with my CLA code. The product delta = P*beta - m_origin must be equal to [0; 0; 0] because P*beta is equal to [1; 2; 3] and m_orign is [1; 2; 3] but the difference between them, in my code, is'nt equal to [0; 0; 0] but to [2.2204e-16; 0; 0]. I've just checked if the first element of the arrays is approximated but this is'nt the case. Thansk to everyone.
n_asset = 3;
C = [1, 0.5, 0; 0.5, 1, 0; 0, 0, 1];
A = ones(n_asset,1)';
P = [C, A'];
m_orig = [1; 2; 3];
beta = [-1.428571428571429; 0.571428571428571; 0.857142857142857; 2.142857142857143];
delta = P*beta-m_orig
  1 个评论
Scott MacKenzie
Scott MacKenzie 2021-6-14
编辑:Scott MacKenzie 2021-6-14
It is not true that
P*beta is equal to [1; 2; 3]
Here's a little modification I made to your code to illustrate what is going on:
format longg
x = P*beta
y = m_orig
delta = x-y
Output:
x =
0.999999999999999
2
3
y =
1
2
3
delta =
-8.88178419700125e-16
-8.88178419700125e-16
0

请先登录,再进行评论。

回答(2 个)

Steven Lord
Steven Lord 2021-6-14
Welcome to the world of floating-point arithmetic.
Please try this little experiment. Find something to write with and something to write on (ideally compatible things; pencil and paper not pencil and whiteboard.)
Step 1: Using long division (like you learned in school) divide 1 by 7. Call the result x. You are allowed to write as many decimal places of the result as you want, but only those you explicitly write can be used in step 2. No indicating a set of repeated digits to get "an infinite" number of places.
Step 2: Multiply x by 7. Call the result y.
In exact arithmetic we know (1/7)*7 is exactly 1. But x is not one seventh. It is slightly smaller than one seventh because you rounded off one seventh to fit it into x. Therefore y will not be 1, it will be slightly smaller than 1.
The numbers in your beta variable are not exactly multiples of one seventh.
beta = [-1.428571428571429; 0.571428571428571; 0.857142857142857; 2.142857142857143];
q = 7*beta
q = 4×1
-10.0000 4.0000 6.0000 15.0000
format longg
beta - [-10; 4; 6; 15]/7 % not all 0's
ans = 4×1
-4.44089209850063e-16 -4.44089209850063e-16 -1.11022302462516e-16 0
Those differences between the elements of beta and multiples of one seventh are small, but they have an impact on the calculations performed using beta.

Leonardo Coccia
Leonardo Coccia 2021-6-15
Thanks everyone. I solved:
delta = round(P*beta, 12)-m_origin;

类别

Help CenterFile Exchange 中查找有关 Pole and Zero Locations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by