http://www.mathworks.com/matlabcentral/newsreader/view_thread/284754
MATLAB Central Newsreader  building regular floating point vectors
Feed for thread: building regular floating point vectors
enus
©19942014 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Wed, 16 Jun 2010 16:29:04 +0000
building regular floating point vectors
http://www.mathworks.com/matlabcentral/newsreader/view_thread/284754#754955
Blue Wizard
I wish to build a vector of increasing floating point number, all<br>
separated by the same amount. I can do this either of the following<br>
two ways:<br>
<br>
delta is a floating point number, not necessarily whole<br>
vec is a vector of floating point numbers, not necessarily whole<br>
n is a floating point number but whole.<br>
<br>
for the nth value<br>
<br>
1) vec(n) = vec(n1) + delta<br>
2) vec(n) = vec(1) + (n  1) * delta<br>
<br>
Question: The numbers delta and vec(1) might not be representable<br>
exactly as binary numbers. Which method is more accurate in that<br>
vec(n) is closest to the true value when the computation is performed<br>
with floating point arithmetic. Thanks in advance.

Wed, 16 Jun 2010 16:46:50 +0000
Re: building regular floating point vectors
http://www.mathworks.com/matlabcentral/newsreader/view_thread/284754#754960
Steven Lord
<br>
"Blue Wizard" <hdh6@cornell.edu> wrote in message <br>
news:c4fc258ae911466fb736f431f4704112@d8g2000yqf.googlegroups.com...<br>
>I wish to build a vector of increasing floating point number, all<br>
> separated by the same amount. I can do this either of the following<br>
> two ways:<br>
><br>
> delta is a floating point number, not necessarily whole<br>
> vec is a vector of floating point numbers, not necessarily whole<br>
> n is a floating point number but whole.<br>
><br>
> for the nth value<br>
><br>
> 1) vec(n) = vec(n1) + delta<br>
> 2) vec(n) = vec(1) + (n  1) * delta<br>
<br>
Three other options:<br>
<br>
3) vec = lowerLimit:delta:upperLimit<br>
4) vec = linspace(lowerLimit, upperLimit, numberOfPoints)<br>
5) Scale vec(1) and delta so that they are exactly representable as binary <br>
numbers, construct your vector, then scale the vector back so the first <br>
element and spacing match your original values.<br>
<br>
for appropriate values of lowerLimit, upperLimit, and numberOfPoints.<br>
<br>
> Question: The numbers delta and vec(1) might not be representable<br>
> exactly as binary numbers. Which method is more accurate in that<br>
> vec(n) is closest to the true value when the computation is performed<br>
> with floating point arithmetic. Thanks in advance.<br>
<br>
What happens if for one of the methods, vec(k) is more accurate than <br>
vec(k+1) for some intermediate value k (by the metric you described) but the <br>
reverse is true for the other methods? Would the magnitude of the error in <br>
each of those elements lead you to choose one over the other?<br>
<br>
 <br>
Steve Lord<br>
slord@mathworks.com<br>
comp.softsys.matlab (CSSM) FAQ: <a href="http://matlabwiki.mathworks.com/MATLAB_FAQ">http://matlabwiki.mathworks.com/MATLAB_FAQ</a><br>
To contact Technical Support use the Contact Us link on <br>
<a href="http://www.mathworks.com">http://www.mathworks.com</a>

Wed, 16 Jun 2010 17:08:05 +0000
Re: building regular floating point vectors
http://www.mathworks.com/matlabcentral/newsreader/view_thread/284754#754967
Roger Stafford
Blue Wizard <hdh6@cornell.edu> wrote in message <c4fc258ae911466fb736f431f4704112@d8g2000yqf.googlegroups.com>...<br>
> I wish to build a vector of increasing floating point number, all<br>
> separated by the same amount. I can do this either of the following<br>
> two ways:<br>
> <br>
> delta is a floating point number, not necessarily whole<br>
> vec is a vector of floating point numbers, not necessarily whole<br>
> n is a floating point number but whole.<br>
> <br>
> for the nth value<br>
> <br>
> 1) vec(n) = vec(n1) + delta<br>
> 2) vec(n) = vec(1) + (n  1) * delta<br>
> <br>
> Question: The numbers delta and vec(1) might not be representable<br>
> exactly as binary numbers. Which method is more accurate in that<br>
> vec(n) is closest to the true value when the computation is performed<br>
> with floating point arithmetic. Thanks in advance.<br>
      <br>
The answer is that 2) is more accurate on average but not by much for large n. In a rough statistical sense, you can split up these errors into three kinds. First there is the error due to vec(1) being not exactly representable in binary. That tends to be a constant and is the same for 1) and 2). Next there is the error due to delta not being exactly representable in binary which tends to increase in direct proportion to n. That too is statistically the same for 1) and 2). Finally there is the error due to repeated addition round offs in 1) and the single multiplication round off in 2). That tends to increase as the square root of n for 1) but remains constant for 2). Consequently 2) has less error on average for that reason but only for that reason.<br>
<br>
For large n it is evident that the error due to the original error in delta will come to predominate over all other errors since it increases in proportion to n, while the repeated addition round offs increase only as the square root of n, statistically speaking, so the differences between 1) and 2) becomes proportionately much less than the errors in either one.<br>
<br>
To put all this in proper perspective, if you are using double precision binary floating point numbers, these have significands (mantissas) of 53 bits in length, so their errors in representing numbers are roughly one part in 2^53 (equals one part in 10^16.) These are very small errors.<br>
<br>
Be aware that matlab can build such vectors for you with its colon operator and indirectly with its linspace function.<br>
<br>
Roger Stafford