First time here? Check out the FAQ!

Ask Your Question
1

plot operation error

asked 7 years ago

matxzero gravatar image

updated 7 years ago

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?

Preview: (hide)

Comments

Welcome to Ask Sage! Thank you for your question.

slelievre gravatar imageslelievre ( 7 years ago )

2 Answers

Sort by » oldest newest most voted
2

answered 7 years ago

Emmanuel Charpentier gravatar image

updated 7 years ago

Or simply:

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

HTH,

Preview: (hide)
link
1

answered 7 years ago

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')
Preview: (hide)
link

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: 7 years ago

Seen: 830 times

Last updated: Oct 15 '17