Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
difference in linspace versus colon construction

Subject: difference in linspace versus colon construction

From: iamisha1

Date: 13 Feb, 2009 04:25:28

Message: 1 of 4

If I do the following:
x=0:0.1:1;
y=linspace(0,1,11);
x-y
then the result is
ans =

  1.0e-016 *

  Columns 1 through 5

         0 0 0 -0.5551 0

  Columns 6 through 10

         0 0 0 0 0

  Column 11

         0

Why is the fourth entry (where both x and y should be equal to 0.4) not exactly zero?

Subject: difference in linspace versus colon construction

From: Matt Fig

Date: 13 Feb, 2009 05:12:02

Message: 2 of 4



sprintf('%19.19f',y(4))
sprintf('%19.19f',x(4))

My guess is that these two functions use different methods to do the calculation. When two different methods are used on imperfectly represented floating point numbers, you cannot guarantee the same outcome numerically, no matter what the mathematics of the algorithms are.




_TCAAIA_GB_OfPUHBTAfMAHOY_EECA_NPT-V%_OOE_LK HmA9yI_ULEOMNO

Subject: difference in linspace versus colon construction

From: JackOLantern

Date: 19 Apr, 2013 09:01:08

Message: 3 of 4

iamisha1 <iamisha@hotmail.com> wrote in message <17000103.1234499159353.JavaMail.jakarta@nitrogen.mathforum.org>...
> If I do the following:
> x=0:0.1:1;
> y=linspace(0,1,11);
> x-y
> then the result is
> ans =
>
> 1.0e-016 *
>
> Columns 1 through 5
>
> 0 0 0 -0.5551 0
>
> Columns 6 through 10
>
> 0 0 0 0 0
>
> Column 11
>
> 0
>
> Why is the fourth entry (where both x and y should be equal to 0.4) not exactly zero?

I think this is related to floating-point accuracy. I have posted a question at

http://stackoverflow.com/questions/16100850/accuracy-of-matlab-linspace-and-range-versus-kahan-summation-algorithm

which may be of interest for the Matlab community.

Subject: difference in linspace versus colon construction

From: Steven_Lord

Date: 19 Apr, 2013 15:00:34

Message: 4 of 4



"JackOLantern " <angelo.liseno@gmail.com> wrote in message
news:kkr14k$9lu$1@newscl01ah.mathworks.com...
> iamisha1 <iamisha@hotmail.com> wrote in message
> <17000103.1234499159353.JavaMail.jakarta@nitrogen.mathforum.org>...
>> If I do the following:
>> x=0:0.1:1;
>> y=linspace(0,1,11);
>> x-y
>> then the result is ans =
>>
>> 1.0e-016 *
>>
>> Columns 1 through 5
>>
>> 0 0 0 -0.5551 0
>>
>> Columns 6 through 10
>>
>> 0 0 0 0 0
>>
>> Column 11
>>
>> 0
>>
>> Why is the fourth entry (where both x and y should be equal to 0.4) not
>> exactly zero?
>
> I think this is related to floating-point accuracy. I have posted a
> question at
>
> http://stackoverflow.com/questions/16100850/accuracy-of-matlab-linspace-and-range-versus-kahan-summation-algorithm
>
> which may be of interest for the Matlab community.

Take a look at question 1 in the Math and Algorithms section in the
newsgroup FAQ for more information. The Cleve's Corner article linked in
that answer is short but contains some good information; Goldberg's article
is longer and more technical but is also very good.

http://matlab.wikia.com/wiki/FAQ

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us