Dear all, I have the following code and the corresponding results:
+++++++++++++++++++++++++++++++
>> aa = 0.005:0.01:0.025
aa =
0.0050 0.0150 0.0250
>> daa = diff(aa)
daa =
0.0100 0.0100
>> daa(1)-daa(2)
ans =
1.7347e-018
++++++++++++++++++++++++++++++++++
Obviously, we should have daa(1)-daa(2) = 0. Who can tell me why Matlab can not give the correct result? Thanks!
crammer008 <luochao2000@hotmail.com> wrote in message
<8220611.1195636303283.JavaMail.jakarta@nitrogen.mathforum.o
rg>...
> Dear all, I have the following code and the corresponding
results:
> +++++++++++++++++++++++++++++++
> >> aa = 0.005:0.01:0.025
> aa =
> 0.0050 0.0150 0.0250
> >> daa = diff(aa)
> daa =
> 0.0100 0.0100
> >> daa(1)-daa(2)
> ans =
> 1.7347e-018
> ++++++++++++++++++++++++++++++++++
> Obviously, we should have daa(1)-daa(2) = 0. Who can tell
me why Matlab can not give the correct result? Thanks!
"Steve Amphlett" <Firstname.Lastname@Where-I-Work.com>
wrote in message <fi0thf$9p1$1@fred.mathworks.com>...
> crammer008 <luochao2000@hotmail.com> wrote in message
>
<8220611.1195636303283.JavaMail.jakarta@nitrogen.mathforum.o
> rg>...
> > Dear all, I have the following code and the
corresponding
> results:
> > +++++++++++++++++++++++++++++++
> > >> aa = 0.005:0.01:0.025
> > aa =
> > 0.0050 0.0150 0.0250
> > >> daa = diff(aa)
> > daa =
> > 0.0100 0.0100
> > >> daa(1)-daa(2)
> > ans =
> > 1.7347e-018
> > ++++++++++++++++++++++++++++++++++
> > Obviously, we should have daa(1)-daa(2) = 0. Who can
tell
> me why Matlab can not give the correct result? Thanks!
>
>
> Check out the FAQ.
>
> http://matlabwiki.mathworks.com/MATLAB_FAQ#Why_is_0.3-0.2-
> 0.1_not_equal_to_zero_.28or_similar.29.3F
crammer008 <luochao2000@hotmail.com> wrote in message
<12361651.1195657935111.JavaMail.jakarta@nitrogen.mathforum.
org>...
> Dear Steve Amphlett,
>
> As the code listed above, if I want to let daa(1)-daa(2)
= 0, what function should be used in Matlab?
>
> Thanks a lot!
> LUO Chao
You can't. 0.01 is one of the floating point "bogey"
numbers. It has no exact representation. You really have
to decide how close the values have to be to be considered
the same. Not a Matlab issue, just floating point. Now if
there was a BCD mode...
Thanks a lot for your great help! Now I think I have to set a threshold and use a "if" language to get rid of the difference below my threshold so that the accumulation of errors won't affect my result. Do you think it is a good way?
"crammer008" <luochao2000@hotmail.com> wrote in message
news:8220611.1195636303283.JavaMail.jakarta@nitrogen.mathforum.org...
> Dear all, I have the following code and the corresponding results:
> +++++++++++++++++++++++++++++++
>>> aa = 0.005:0.01:0.025
> aa =
> 0.0050 0.0150 0.0250
>>> daa = diff(aa)
> daa =
> 0.0100 0.0100
>>> daa(1)-daa(2)
> ans =
> 1.7347e-018
> ++++++++++++++++++++++++++++++++++
> Obviously, we should have daa(1)-daa(2) = 0. Who can tell me why Matlab
> can not give the correct result? Thanks!
"Nasser Abbasi" <nma@12000.org> wrote in message
<RAF1j.26885$aN3.11505@newsfe12.phx>...
>
> "crammer008" <luochao2000@hotmail.com> wrote in message
>
news:8220611.1195636303283.JavaMail.jakarta@nitrogen.mathforum.org...
> > Dear all, I have the following code and the
corresponding results:
> > +++++++++++++++++++++++++++++++
> >>> aa = 0.005:0.01:0.025
> > aa =
> > 0.0050 0.0150 0.0250
> >>> daa = diff(aa)
> > daa =
> > 0.0100 0.0100
> >>> daa(1)-daa(2)
> > ans =
> > 1.7347e-018
> > ++++++++++++++++++++++++++++++++++
> > Obviously, we should have daa(1)-daa(2) = 0. Who can
tell me why Matlab
> > can not give the correct result? Thanks!
>
>
> use VPA on the diff, then you will get your zero:
>
> EDU>> aa = 0.005:0.01:0.025
>
> aa =
>
> 0.0050 0.0150 0.0250
>
> EDU>> daa = vpa(diff(aa));
> EDU>> double(daa(1)-daa(2))
>
> ans =
>
> 0
>
> Nasser
>
>
>
The above code works using the variable precision arithmetic
function vpa that is part of the symbolic toolbox. The
student version comes with this built in. The professional
version does not, so not all people will be able to use it.
crammer008 <luochao2000@hotmail.com> wrote in message
<8220611.1195636303283.JavaMail.jakarta@nitrogen.mathforum.org>...
> Dear all, I have the following code and the corresponding
results:
> +++++++++++++++++++++++++++++++
> >> aa = 0.005:0.01:0.025
> aa =
> 0.0050 0.0150 0.0250
> >> daa = diff(aa)
> daa =
> 0.0100 0.0100
> >> daa(1)-daa(2)
> ans =
> 1.7347e-018
> ++++++++++++++++++++++++++++++++++
> Obviously, we should have daa(1)-daa(2) = 0. Who can tell
me why Matlab can not give the correct result? Thanks!
Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for
all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content.
Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available
via MATLAB Central. Read the complete Disclaimer prior to use.