A piecewise definition is fine, but the function must be well-defined
within the whole domain.
For example:
plot2d( '(if x>1 then 3 else 1) ,
[x,0,3] ); /* Expression form */
Note that you must quote the expression.
Logically, you should also be able to write:
plot2d( lambda([x], if x>1 then 3 else 1) ,
[x,0,3] ); /* Functional form */
but Maxima has a bug, and doesn't recognize the lambda expression as a
function.
You can also define a separate function:
f(x):= if x>1 then 3 else 1;
plot2d( f(x) , [x,0,3] ); /* Expression form */
plot2d( f , [x,0,3] ); /* Functional form */
Though the x is otiose, it is required.
For greater efficiency, you can do translate(f) or compile(f) before
plotting, though this sometimes introduces other problems (translation
bugs).
Unfortunately, there is currently no way to specify that a function is
undefined in some subdomain, or that it is infinite, so you must use
bogus values:
plot2d( '( if abs(x)<1.0e-1 then 0 else 1/x) , [x,-2,2] )
plot2d(
Note bogus assumption of continuity in the plot.
I would hope that in a future version of plot2d, it would understand
return values of UND, INF, etc.
You can get around this by setting the y-range explicitly, and using
points outside the plot area. This only really works correctly when the
function crosses the y-max/min:
plot2d( '(if abs(x)<1.0e-1 then signum(x)*1.0e3 else 1/x) ,
[x,-2,2] ,
[y,-10,10] )