Why does linspace deliver unequal spacing?
18 views (last 30 days)
I thought linspace was supposed to deliver a vector with perfectly even spacing, but there seems to be a slight difference in spacing, for example:
XGlobal = sort(rand(1,100))*100 - 50;
You can see the last few digits differ. That means that, for example, interp2 with cubic interpolation won't work because it requires evenly spaced vectors. What is the workaround?
the cyclist on 13 Aug 2015
Edited: the cyclist on 13 Aug 2015
This is a result of computing in floating point arithmetic. Here is one of many possible starting points to read about this.
Did you actually use interp2, and it failed because of this, or was that more of a theoretical statement you made?
More Answers (1)
Walter Roberson on 13 Aug 2015
Consider for example, diff((1:3)/3,2) = 5.55111512312578e-17 indicating that (2/3 - 1/3) is not exactly equal to (3/3 - 2/3). And
cumsum(1/3*ones(1,10)) - (1:10)/3
is non-zero in positions 5, 6, and 7, indicating that adding 1/3 five times does not give the same result as if you had calculated 5/3 directly.
linspace has to choose one way of calculating the values. Whichever way it chooses is going to be "wrong" by some measure. If it makes the intervals exactly equal then there must be cumulative error. Do you want the "add 1/3 five times" value where the intervals are equal, or do you want the "5/3" value where the values are more precise but the intervals might not be exactly the same?