binomial theorem and MATLAB
2 views (last 30 days)
Show older comments
Hello,
I habe 3 variables:
x= 0.988:0.0001:1.012;
y= (x-1).^7
y2=x.^7-7*x.^6+21*x.^5-35*x.^4+35*x.^3-21*x.^2+7*x-1;
y2 is the therm (x-1)^2 calculated with the binomial theorem.
So, to my understanding the solution for every x should be the same in y and y2:
But this is my Result:

Would be awesome if someone could explain this odd result to me :)
0 Comments
Accepted Answer
Steven Lord
on 4 May 2017
Cleve Moler, the original author of MATLAB, wrote am article about this (with this specific example) in 1996.
https://www.mathworks.com/company/newsletters/articles/floating-points-ieee-standard-unifies-arithmetic-model.html
More Answers (1)
John D'Errico
on 4 May 2017
Edited: John D'Errico
on 4 May 2017
Actually, what you wrote for y2 is NOT (x-1)^2. It is (x-1)^7. 2 and 7 are not the same thing. Well, they look alike in some fonts and the way some people write the numbers. But 2 and 7 are in fact different numbers.
syms x
expand((x-1)^7)
ans =
x^7 - 7*x^6 + 21*x^5 - 35*x^4 + 35*x^3 - 21*x^2 + 7*x - 1
The plot you got is entirely reasonable for what you did, computing the result (x-1)^7 where x is in the interval [1-0.012,1+0.012].
So then x-1 lies in the interval [-0.012,0.012]. And seince:
0.012^7
ans =
3.5832e-14
that plot should be scaled entirely in the range of +/- 5e-14. The fuzziness of the curve is due to floating point trash on the computation, sometimes called massive subtractive cancellation.
2 Comments
John D'Errico
on 4 May 2017
Edited: John D'Errico
on 4 May 2017
The plot you generated is quite correct. The difference between the two curves is simply due to massive subtractive cancellation. That does not happen for
(x-1)^7
But it does so when you try to compute things in the expanded form:
x^7 - 7*x^6 + 21*x^5 - 35*x^4 + 35*x^3 - 21*x^2 + 7*x - 1
It is MUCH more accurate to compute things using the form (x-1)^7, because there you subtract off 1 FIRST.
While the two forms are symbolically identical, in floating point arithmetic they are not. Look at the individual terms, for x=1.012.
format long g
x = 1.012;
(x-1)^7
ans =
3.58318080000002e-14
x^7
ans =
1.08708521100641
-7*x^6
ans =
-7.51936410775185
21*x^5
ans =
22.2906050625055
-35*x^4
ans =
-36.71048264576
35*x^3
ans =
36.27518048
-21*x^2
ans =
-21.507024
7*x
ans =
7.084
-1
ans =
-1
Remember that each of those terms has a tiny amount of noise in the least significant bits. But the least significant bits of each of those numbers vary by quite a bit.
eps(35*x^3)
ans =
7.105427357601e-15
eps(-1)
ans =
2.22044604925031e-16
So, when you add up all of those terms, you get noise that will be on the order of 1.e-14. Again, this is the concept of massive subtractive cancellation.
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!