MATLAB: Difference between lines using linespace

linespace

Hi there, I need to know what is the difference between these: f=linspace(-fs/2,fs/2,samp_Tr); f = -fs/2 + (0:samp_Tr-1)*fs/samp_Tr; actually it gives me in my system different solution
thanks in advance

Best Answer

The two are very similar. The difference is that linspace taked some extra care to reduce the effect of accumulated round-off error. For example, because 1/3 cannot be exactly represented in binary, if you add the closest binary value to 1/3 together three times, you will get a value slightly less than 1. Imagine that you did that 300 times in a row; the result would have to end up 100 times that small difference less than 100. That would happen with the : operator. linspace works with that by dividing the range into two pieces and allowing accumulated error from the beginning to the middle and accumulated error from the end towards the middle, so right in the middle the gap might be a slightly different size.