Strange results for evaluating quadratic function on a linspace
Show older comments
Could it be that linspace is not working quite exactly?
Consider the following code:
X = linspace(-1.2,1.2,25);
F =@(x,y) x.^2+y.^2-1;
F(X(23),X(13))
It should be F(X(23),X(13)) = F(1,0) = 0, but somehow the result is
-4.440892098500626e-16
I came across this problem while trying to find intersections of an implicitly given curve and lattice knots (more precisely elements of ZxZ). A simple check such as
if F(X(i),X(j)) == 0
A = [A;X(i),X(j)];
end
should have sufficed, but apparently this is not working because of the above issue. I appreciate your help!
Accepted Answer
More Answers (1)
Welcome to the world of numerics with limited precision. Remember that floating point numbers are displayed in decimal format, but store in binary format internally. This must lead to rounding effects and there is no way to avoid this. See also: FAQ: Why is 0.3-0.2~=0.1 .
4.44e-16 is 2*|eps|, which is the expected inaccuracy for the input data. linspace is working correctly.
Another standard example, where you see a 0 although a non-zero is expected mathematically:
1e17 + 1 - 1e17
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!