ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 14 Nov 2019 22:47:07 +0100incorrect plot of simple defined function, but works with lambdahttps://ask.sagemath.org/question/48736/incorrect-plot-of-simple-defined-function-but-works-with-lambda/ I am trying to produce a plot of a simple defined function with a single if statement. The function works correctly as demonstrated by numerical values, but the corresponding plot is incorrect.
If I create the plot using a lambda definition of the function, the plot is correct. The sample code follows:
def h(x):
if x<0: return x
return x^2
for k in (-2..2):
print "(%d,%d)"% (k,h(k)),
print 'sample h(x) vs. x pairs'
H = plot(h(x),(-1.5,1.5),linestyle='dotted',color='red',thickness=3)
L = plot(lambda x: x if x<0 else x^2,(-1.5,1.5),figsize=3)
H+L
The following image shows the code and output in a Sagemath notebook.
As you can see the plot of h(x) does not properly evaluate the function if statement, but the lambda plot does.
![image description](https://snipboard.io/5C1Tey.jpg)holistoneThu, 14 Nov 2019 22:47:07 +0100https://ask.sagemath.org/question/48736/