Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: What happened to so simple Matlab code?
Date: Fri, 23 Nov 2007 23:37:59 +0000 (UTC)
Organization: Siemens Medical
Lines: 50
Message-ID: <fi7o8n$sti$1@fred.mathworks.com>
References: <8220611.1195636303283.JavaMail.jakarta@nitrogen.mathforum.org> <RAF1j.26885$aN3.11505@newsfe12.phx>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1195861079 29618 172.30.248.37 (23 Nov 2007 23:37:59 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 23 Nov 2007 23:37:59 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1150820
Xref: news.mathworks.com comp.soft-sys.matlab:439108



"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.  

Regards,
Georgios