Path: news.mathworks.com!not-for-mail
From: "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid>
Newsgroups: comp.soft-sys.matlab
Subject: Re: how come matlab drew this?
Date: Sun, 24 Feb 2008 20:22:04 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 52
Message-ID: <fpsjlc$q6q$1@fred.mathworks.com>
References: <fps18k$dh3$1@fred.mathworks.com>
Reply-To: "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1203884524 26842 172.30.248.35 (24 Feb 2008 20:22:04 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sun, 24 Feb 2008 20:22:04 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:453422


"Marcelo Tames" <jmarcelo.tb@mathworks.com> wrote in message <fps18k
$dh3$1@fred.mathworks.com>...
> Hi I saw a exercise in a book and asked me to plot these 
> graphs togheter as abs(y1-y2):
> 
> y1(x) =sqrt(x^2 + 1)- 1 
> 
> and 
> 
> y2(x) =x^2/sqrt(x^2 + 1) + 1
> 
> and I did the following:
> 
> delta=10^(-3);
> x=linspace(10^(-4),10^(-3),100);
> y1 = sqrt(x.^2+1)-1;
> y2 = x.^2./(sqrt(x.^2+1)+1);
> z = abs(y1-y2)
> plot(x,z)
> ylabel('value');
> xlabel('x');
> title('compare');
> 
> I get a graph but I don?t really understand why is that, I 
> mean if one solves abs(y1-y2) by hand everything becomes 
> zero, can anyone explain to me why matlab still graphs 
> something and is what I did wrong? I?m a beginner so please 
> understand that my reasoning is not that advanced
> 
> thanks 
--------
  I assume from your remarks that you are aware that the two expressions in 
question are mathematically identically equal to one another, so you are 
apparently asking why you didn't get a graph showing a horizontal line of 
height zero.  If that is your question, the answer is round off error in 
computing the two quantities.  These involve differing mathematical 
operations and will produce slightly different results because of this round off 
error.  Note that the values you are getting in the graph are of size 
somewhere in the neighborhood of 10^(-16) which is a very small value 
indeed.  If you were to accompany this graph with another one that assumes 
values in the neighborhood of 1, the first graph would be squeezed down to 
the horizontal line you are expecting.

  If you are asking why the two expressions are identical, try multiplying the 
numerator and denominator of  y2 by (sqrt(x^2+1)-1) and simplify the result.  
What you arrive at is precisely y1.

  By the way, in the first expression for y2, you appear to have left out a pair 
of parentheses, though that is corrected in the code.

Roger Stafford