Ask Your Question
1

plot operation error

asked 2017-10-13 02:38:19 +0100

matxzero gravatar image

updated 2017-10-15 20:24:31 +0100

calc314 gravatar image

I'm trying to plot the following.

def myfn2(x): if x<0: return 1 else: return -1 plot(myfn2(x),x,-3,3,figsize=3,color="red")

The graph is only displayed as -1. Why?

edit retag flag offensive close merge delete

Comments

Welcome to Ask Sage! Thank you for your question.

slelievre gravatar imageslelievre ( 2017-10-15 09:17:56 +0100 )edit

2 Answers

Sort by ยป oldest newest most voted
2

answered 2017-10-15 21:57:44 +0100

Emmanuel Charpentier gravatar image

updated 2017-10-15 21:57:59 +0100

Or simply:

plot(myfn,(x,-1,1))

HTH,

edit flag offensive delete link more
1

answered 2017-10-15 09:17:30 +0100

slelievre gravatar image

This is because it first evaluates myfn2(x) on the symbolic variable x, and the test x < 0 then returns False, so what gets plotted is only -1.

The workaround is this:

sage: def myfn2(x):
....:     if x < 0:
....:         return 1
....:     else:
....:         return -1
....:     
sage: plot(lambda x: myfn2(x), (-3, 3), figsize=3, color='red')
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2017-10-13 02:38:19 +0100

Seen: 733 times

Last updated: Oct 15 '17