Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: how come matlab drew this?

Subject: how come matlab drew this?

From: Marcelo Tames

Date: 24 Feb, 2008 15:08:04

Message: 1 of 3

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

Subject: Re: how come matlab drew this?

From: Roger Stafford

Date: 24 Feb, 2008 20:22:04

Message: 2 of 3

"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

Subject: Re: how come matlab drew this?

From: Marcelo Tames

Date: 24 Feb, 2008 21:03:06

Message: 3 of 3

"Roger Stafford"
<ellieandrogerxyzzy@mindspring.com.invalid> wrote in
message <fpsjlc$q6q$1@fred.mathworks.com>...
> "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
>

thanks Roger, I was suspecting that about the error but
wasn?t sure. I?ll discuss these with my classmates. thanks
again

best regards
Marcelo

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
graph Marcelo Tames 24 Feb, 2008 10:10:05
plot graph Marcelo Tames 24 Feb, 2008 10:10:05
rssFeed for this Thread

envelope graphic E-mail this page to a colleague

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.
Related Topics