Wrong plotting.

3 views
Skip to first unread message

Eugene

unread,
Apr 6, 2008, 4:35:03 AM4/6/08
to sage-support
Hi all!

Help please, I can not understand what is wrong with this:

f(x)=x;
def g(x):
if (x>=0):
return f(x)
else:
return f(-x);

show(plot(g(x),(x,-1,1))) #form1
show(plot(g,-1,1)) #form2

same thing:
https://www.sagenb.org/home/pub/1770/

the two graphics must be same, because it is the same functions.
so, why form 2 gives wrong result?

P.S. I do not want to plot abs(x) function, that is only for example.

William Stein

unread,
Apr 6, 2008, 4:42:46 AM4/6/08
to sage-s...@googlegroups.com
On Sun, Apr 6, 2008 at 1:35 AM, Eugene <zhome...@gmail.com> wrote:
>
> Hi all!
>
> Help please, I can not understand what is wrong with this:
>
> f(x)=x;
> def g(x):
> if (x>=0):
> return f(x)
> else:
> return f(-x);
>
> show(plot(g(x),(x,-1,1))) #form1
> show(plot(g,-1,1)) #form2
>
> same thing:
> https://www.sagenb.org/home/pub/1770/
>
> the two graphics must be same, because it is the same functions.
> so, why form 2 gives wrong result?

In form 1 g(x) evaluates the function g *exactly* once. It
decides the x>=0 is false (since it isn't *always* true), so
it returns f(-x) since and for all. That is, g(x) evaluates
to something completely different than g.

Does that make sense?

>
> P.S. I do not want to plot abs(x) function, that is only for example.
>
> >
>

--
William Stein
Associate Professor of Mathematics
University of Washington
http://wstein.org

Reply all
Reply to author
Forward
0 new messages