nuve
nuve

Reputation: 21

Plotting graph in maple

I've been trying to plot a graph of function f(x) on interval 0,6. For some reason maple doesn't plot the graph when I use 'f(x)' as an argument. It works fine when I substitute assigned function as an argument. What can be the reason? How can I plot it using 'f(x)' as an argument?

My code is as follows and the error is on the pictures:

mystep:=proc(x,a,b,c) 
if x<a 
then 
 b;
else 
 c; 
end if:
end proc:

f(x):=mystep(x,3,-2,7);

plot('f(x)', x=0..6);

enter image description here

Upvotes: 1

Views: 445

Answers (2)

The type of function you have is piecewise-defined function (see this wikipedia page https://en.wikipedia.org/wiki/Piecewise). And in Maple there is already a command for defining this type of a function, piecewise, see its help page for a complete explanation of how to use it. Now for your mysetup, you have a condition x < a, and a value for when this happens, b, and a value for otherwise, c. So you want piecewise( x < a, b, c ). I think it is better to just use this command, but if it is really necessary to define a new procedure, then your mysetup becomes like the following.

mystep := proc(x, a, b, c)
    return( piecewise( x < a, b, c ) ):
end proc:

Now for your plot you can use either

plot( piecewise( x < 3, -2, 7 ), x = 0..6 );

or

f(x) := mystep(x, 3, -2, 7);
plot( f(x), x = 0..6);

Upvotes: 0

acer
acer

Reputation: 7246

Your syntax for creation of the operator (that you hope to assign to f) is incorrect for 1D plaintext input.

Here is the usual syntax for creation of such an operator, and assignment to name f.

restart;
mystep:=proc(x,a,b,c)
   if x<a then b; else c; end if:
end proc:

f:=x->mystep(x,3,-2,7);

These should now all work as you expect.

plot('f(x)', x=0..6);
plot(f, 0..6);
plot(x->mystep(x,3,-2,7), 0..6);

In recent versions of Maple the syntax that you used can work in (only) 2D Input mode. But it is a poor syntax to use, since is easily confused with the 1D syntax for assigning into the remember table of an operator (and with even more confusion ensuing). You should avoid ambiguous syntax.

Upvotes: 2

Related Questions