Piewise functions



On 11/17/06, Bob Jerrard <rjerrard at math.concordia.ab.ca> wrote:
> I am using Maxima 5.9.3 on a Debian system. The following odd situation
> arose in trying to get a plot of a piecewise function.
>
> (%i1) f(x):='(if x<=2 then x-3 else x^2);
>                                                          2
> (%o1)               f(x) := '(if x <= 2 then x - 3 else x )
> (%i2) plot2d([[parametric,t,f(t),[t,-1,2]],[parametric,t,f(t),[t,2
> +0.000001,5]]],[x,-1,5]);
>
> Maxima was unable to evaluate the predicate:
> x <= 2
>  -- an error.  Quitting.  To debug this try debugmode(true);
> (%i3)plot2d([[parametric,x,f(x),[x,-1,2]],[parametric,x,f(x),[x,2
> +0.000001,5]]],[x,-1,5]);
> (%o3)
> (%i4) plot2d([[parametric,t,f(t),[t,-1,2]],[parametric,t,f(t),[t,2
> +0.000001,5]]],[t,-1,5]);
> Maxima was unable to evaluate the predicate:
> x <= 2
>  -- an error.  Quitting.  To debug this try debugmode(true);
> (%o4)

You should not quote function definition because:

(%i4) f(x) := '(if x<=2 then x-3 else x^2);
(%o4) f(x):='(if x<=2 then x-3 else x^2)
(%i5) f(4);
(%o5) if x<=2 then x-3 else x^2

which explains the behaviour.

Instead you should do something like

(%i1) f(x) := if x<=2 then x-3 else x^2;
(%o1) f(x):=if x<=2 then x-3 else x^2
(%i2) plot2d([[parametric,t,'(f(t)),[t,-1,2]],[parametric,t,'(f(t)),[t,2
+0.000001,5]]],[x,-1,5]);

The difference is the you quote f(t) in plot2d.

HTH,
Andrej