Piewise functions

View: New views
4 Messages — Rating Filter:   Alert me  

Piewise functions

by Robert Jerrard :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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)

So, when f(x) is plotted with parameter t in a plot with domain variable
x the plot does not work. Plotting with parameter x in a plot with
domain variable x works just fine. Plotting with parameter t in a plot
with domain variable t does not work.

If the function f(x) had been defined as f(x):=x^2 all three of the
plots work just fine.

Why the different behaviours?

Bob
--
Dr. Robert J. Jerrard, Professor of Mathematics,
Concordia University College of Alberta,
7128 Ada Blvd., Edmonton, Alberta, T5B 4E4, Canada.
Phone: (780) 479-9291, Fax: (780) 474-1933.

_______________________________________________
Maxima mailing list
Maxima@...
http://www.math.utexas.edu/mailman/listinfo/maxima

Re: Piewise functions

by andrej.vodopivec :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 11/17/06, Bob Jerrard <rjerrard@...> 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
_______________________________________________
Maxima mailing list
Maxima@...
http://www.math.utexas.edu/mailman/listinfo/maxima

Re: Piewise functions

by Robert Dodier-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 11/17/06, Andrej Vodopivec <andrej.vodopivec@...> wrote:

> (%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.

Agreed with Andrej here -- a couple of other solutions.

(1) Just put the name of the function in the plot expression
(i.e. don't indicate the argument). E.g.

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

(2) Maxima 5.10.0 comes with an add-on package boolsimp which
implements unevaluated conditionals. This makes working with
functions defined piecewise more natural. E.g.

load (boolsimp);
f(x) := if x<=2 then x-3 else x^2;
plot2d ([[parametric, t, f(t), [t, -1, 2]], [parametric, t, f(t), [t,
2 + 0.000001, 5]]], [x, -1, 5]);

so your original plot2d expression works.

HTH
Robert Dodier
_______________________________________________
Maxima mailing list
Maxima@...
http://www.math.utexas.edu/mailman/listinfo/maxima

Re: Piewise functions

by S. Newhouse :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

My solution to this kind of problem has been to use Heaviside
functions such as the following example.

However, in this example, the plot produces a vertical line at the
discontinuity.  Is there a simple way to avoid this?


UnitStep(x) := (signum(x) + 1)/2;
f(t):= (1 - UnitStep(t-2))*(t-3) + UnitStep(t-2)*t^2;
plot(f(t),[t,-1,5]);

-sen




On Fri, 17 Nov 2006, Robert Dodier wrote:

> On 11/17/06, Andrej Vodopivec <andrej.vodopivec@...> wrote:
>
>> (%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.
>
> Agreed with Andrej here -- a couple of other solutions.
>
> (1) Just put the name of the function in the plot expression
> (i.e. don't indicate the argument). E.g.
>
> f(x) := if x<=2 then x-3 else x^2;
> plot2d ([[parametric, t, f, [t, -1, 2]], [parametric, t, f, [t, 2 +
> 0.000001, 5]]], [x, -1, 5]);
>
> (2) Maxima 5.10.0 comes with an add-on package boolsimp which
> implements unevaluated conditionals. This makes working with
> functions defined piecewise more natural. E.g.
>
> load (boolsimp);
> f(x) := if x<=2 then x-3 else x^2;
> plot2d ([[parametric, t, f(t), [t, -1, 2]], [parametric, t, f(t), [t,
> 2 + 0.000001, 5]]], [x, -1, 5]);
>
> so your original plot2d expression works.
>
> HTH
> Robert Dodier
> _______________________________________________
> Maxima mailing list
> Maxima@...
> http://www.math.utexas.edu/mailman/listinfo/maxima
>

--
  ---------------------------------------------------------------------------
  | Sheldon E. Newhouse            |    e-mail: sen1@...           |
  | Mathematics Department         |         |
  | Michigan State University      | telephone: 517-355-9684                |
  | E. Lansing, MI 48824-1027 USA  |       FAX: 517-432-1562                |
  ---------------------------------------------------------------------------
_______________________________________________
Maxima mailing list
Maxima@...
http://www.math.utexas.edu/mailman/listinfo/maxima