xoux
xoux

Reputation: 3494

Is it possible to plot from inside a procedure in Maple?

I was hoping the procedure below would create a plot. It does not

xsquared := proc() plot(x^2); return ; end proc
xsquared()

Is there a reason, and is there a workaround for automating plot creation?

Upvotes: 2

Views: 363

Answers (1)

acer
acer

Reputation: 7246

Plots get rendered (ie. displayed) by virtue of being printed.

Your procdure does indeed create a plot, but your procedure also finishes by returning NULL. That is, you procedure is written to throw away the created plot, rather than return it.

Let's alter your procedure so that it returns the plot instead of throwing it away. (There are shorter ways to do this, but I'll make it assign the plot to a local variable, as you might want this more general usage later on.)

xsquared := proc()
  local P;
  P := plot(x^2);
  return P;
end proc;

Now lets call xsquared but suppress the output by terminating with a full colon. We'll assign the result to some name, to keep the example more general.

Notice that no plot is rendered. That's because we suppressed the result from being printed, by terminating with a full colon.

This := xsquared():

If we print the value assigned to This then we'll see a plot rendered.

This;

That was done at the "top level", outside any procedure. So we were able to print the value assigned to This merely by evaluating it.

Another way would be to simply avoid suppressing the output from the call to xsquared, by terminating with a semicolon. Recall that merely printing a plot will cause it to be rendered. So allowing the output to print will cause it to be rendered.

xsquared();

That leaves the behaviour of having a procedure cause the plot to be rendered even if the plot (plot structure) is not actually returned by any call to that procedure. Once again, recall that a plot gets rendered when it is printed. We can force the printing of the plot from within the procedure.

xsquared := proc()
  local P;
  P := plot(x^2);
  print(P);
  return NULL;
end proc;

xsquared():

The above call to the new version of xsquared causes the plot to be printed and rendered even though it is not returned by the call.

Upvotes: 4

Related Questions