1 | initial version |
The problem in your first example is that when you do f(x)
, it is actually calling f and returning a value. However, you want to plot the function, not the return value from one call. So you should do this:
def f(x):
if x>3:
return(x^2)
if x<=3:
return(3*x)
plot(f,(x,0,5))
Notice that now, I haven't called the function. Instead, I'm just passing the function into plot, and plot will call the function with different values.
Your lambda function trick works because it is passing a function into plot. plot then calls the lambda function, which in turn calls the f function. But I think it's easier to what I did above.