plot operation error
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?
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?
Or simply:
plot(myfn,(x,-1,1))
HTH,
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')
Please start posting anonymously - your entry will be published after you log in or create a new account.
Asked: 2017-10-13 02:38:19 +0100
Seen: 733 times
Last updated: Oct 15 '17
Welcome to Ask Sage! Thank you for your question.