I have a problem with a procedure which I want to plot.
Consider the following.
restart:with(plots): PrPDPD:=proc(x) local out, critical_value; critical_value:=evalf(Pi/4.0); if x > critical_value then out:=0.0; else out:=1.0; fi; out; end; PrPDPD(0.5);
gives 1.0 OK PrPDPD(0.8); gives 0.0 OK. but plot(PrPDPD(x),x=0..1.0); gives an error message Error, (in PrPDPD) cannot evaluate boolean.
Can anyone tell me what is going wrong and how I can use a procedure containing boolean statements inside a plot command?
This is Maple release 5.5
plot('PrPDPD(x)', x= 0..1.0);
Note that it is impossible to determine the truth of the expression x < Pi/4
if \(x\) does not
have a numerical value.
Thus, we need to delay the evaluation of the expression PrPDPD(x)
until the numerical
values are assigned. That’s what the quotes do.
Your expression PrPDPD(x)
already has a well-defined name in Maple, indeed in math in
general. It is Heaviside(Pi/4 - x)
.
| It is curious that if I put an equal sign instead of a less ...
Yes, that can be very annoying feature. I wish that Maple would fail with an error message for that one too. Perhaps there is a way to make it fail that I don’t know about. When it doesn’t fail, I think this behavior leads to erroneous computations.
Anyway, here’s why it happens: Consider the expression "x = 3"
, where \(x\) is an undefined
variable. Is an undefined variable the same thing as a number? Of course not. So are they
equal? No. Then they must be not equal.
Here’s another way of looking at it: I give you two sentences, A and B:
A: "The symbol x is the same thing as the number three." B: "The symbol x is less than the number three."
The first sentence makes sense, and is clearly false. The second sentence is nonsense.
If you call plot(something,options)
then the first thing that happens is that Maple
evaluates the arguments "something"
and "options"
.
Thus PrPDPD(x)
is evaluated before the variable \(x\) is assigned a (numerical) value, and Maple
cannot dicide if x > critical_value
.
Now you can do two things:
(1) prevent Maple to evaluate PrPDPD(x) before x has a numerical value. Possibilities:
plot('PrPDPD(x)',x=0..1.0); or plot(PrPDPD, 0..1.0);
(2) Alter the procedure so that it can handle the case that x doesn’t have a numerical value:
PrPDPD:=proc(x) local out, critical_value; critical_value:=evalf(Pi/4.0); if not type(x,numeric) then RETURN('procname(args)') elif x > critical_value then out:=0.0 else out:=1.0 fi; out; end;
Now plot(PrPDPD(x),x=0..1.0);
results in the disired plot.
There are several things you can do to get what you want.
You can use single quotes to delay the evaluation of the if in your procedure.
plot('PrPDPD(x)',x=0..1.0);
You can use the name of your function along with the domain without the variable.
plot(PrPDPD,0..1.0);
You can avoid having to use the single quotes in another way if you use the piecewise command in your proc.
PrPDPDnew:=proc(x) piecewise(x<Pi/4,1,x>=Pi/4,0) end: Then plot(PrPDPDnew(x),x=0..1.0);
will work.