5
$\begingroup$

To insure a collection of plots has identical appearance, the plot command is encapsulated in a function. The results are undesirable. The fiducial plot is

 ggood = ParametricPlot[{r, ZernikeR[100, 0, r]}, {r, -1, 1}, 
  PlotRange -> {Automatic, 1.05 {-1, 1}}]

which creates plot of Zernike polynomial

The functional version is

Clear[pp];
pp[ψ_, options___] := 
 ParametricPlot[{r, ψ}, {r, -1, 1}, options, 
  PlotRange -> {Automatic, 1.05 {-1, 1}}]

 gbad = pp[ZernikeR[100, 0, r]]

which produces

bad plot

How should the functional form be posed?

$\endgroup$
  • $\begingroup$ BTW: did you know that ParametricPlot[{r, f[r]}, {r, a, b}] is the same as Plot[f[r], {r, a, b}]? With the HoldFirst attribute you learned from the Wizard, you can now use this much simpler construction. $\endgroup$ – J. M. will be back soon May 23 '15 at 2:42
  • $\begingroup$ Michael E2 demonstrates the difference between these macros here: mathematica.stackexchange.com/questions/83375/…. What are your thoughts? $\endgroup$ – dantopa May 25 '15 at 20:54
5
$\begingroup$

As MarcoB posted before me this has to do with evaluation differences. Simply setting HoldFirst on pp produces the original plot:

SetAttributes[pp, HoldFirst]

pp[ψ_, options___] := 
 ParametricPlot[{r, ψ}, {r, -1, 1}, options, PlotRange -> {Automatic, 1.05 {-1, 1}}]

pp[ZernikeR[100, 0, r]]

enter image description here

You will want this attribute anyway as without it the global value of r will be used, if it exists, which would result in even worse behavior.

As to why the symbolic expansion of ZernikeR[100, 0, r] needs extra WorkingPrecision to get the desired output please see:

$\endgroup$
  • 1
    $\begingroup$ HoldFirst! That's what I was looking for!! Man, I have spent the last ten minutes thinking "If only there was a way to tell a function not to evaluate its arguments..." Thank you for putting me out of my misery. I'm also out of votes, I've just realized, so I guess we owe each other one :-) $\endgroup$ – MarcoB May 22 '15 at 20:37
  • $\begingroup$ @MarcoB There is a whole family of Hold functions, e.g. HoldRest, SequenceHold, etc. Some of the lesser known (I think) are NHoldAll (and rest, first) but these can be very important at times. $\endgroup$ – Mr.Wizard May 22 '15 at 20:44
  • $\begingroup$ I definitely need to get some practice with those. I came across HoldAll and its effects early in my tinkering days, through the classical beginner's problem of coloring when plotting things that evaluate to lists, but are not lists yet. I should have followed through then! Anyway, I saw your post today re: Robby Villegas tutorial on Unevaluated, so I have it on my list of things to read. I've scanned it briefly, and I think it mentions Hold* attributes too. $\endgroup$ – MarcoB May 22 '15 at 21:01
  • $\begingroup$ @MrW: Great explanation and extremely helpful link! This solves many problems dealing with the Zernike polynomial set. $\endgroup$ – dantopa May 22 '15 at 21:02
6
$\begingroup$

This is a comment rather than an answer, but it grew too long for the comment box, and I wanted to show graphics, so I'll leave it here in hopes that someone better versed in the inner workings of MMA might explain this behavior. I am using your definition of the wrapper function pp.

First of all, I get a slightly different version of the "wrong" function:

wrapped parametric plot

Having said that, it seems that this behavior has something to do with the order of evaluation. In fact, if I pass the function to plot to the pp wrapper unevaluated, then I obtain the same well-behaved output as your "free standing" ParametricPlot:

pp[Unevaluated@ZernikeR[100, 0, r]]

passing unevaluated

The opposite is also true: if I pre-evaluate the ZernikeR function, then try to plot the result, I obtain the same result I had from feeding this function straight to your wrapper:

ZernikeR[100, 0, r];
ParametricPlot[{r, %}, {r, -1, 1}, PlotRange -> {Automatic, 1.05 {-1, 1}}]

Mathematica graphics

I am afraid that this is as far as I was able to go to understand the problem. Depending on how you are constructing the lists of functions to plot, you may be able to pass the functions unevaluated programmatically, and get around the road block for now. For instance

pp /@ 
  Unevaluated/@ 
    Unevaluated@
      {ZernikeR[20, 0, r], ZernikeR[50, 0, r], ZernikeR[100, 0, r]}

Mathematica graphics

I'd still like to understand why this is the case, however.

$\endgroup$
  • 3
    $\begingroup$ +1 good observation. If you see the polynomial that results from ZernikeR[100, 0, r] you can see why extended precision is needed. Actually you can see the trouble if you just do (z = ZernikeR[100, 0, r]; z /. r -> .9) vs ZernikeR[100, 0, .9] $\endgroup$ – george2079 May 22 '15 at 20:36
  • $\begingroup$ @george2079 That's a very nice example, thank you. In fact, playing around with the precision of that $0.9$ shows that one needs at least 40 digits of precision to obtain a reasonable result... Ouch! $\endgroup$ – MarcoB May 22 '15 at 20:48
  • $\begingroup$ @MarcoB: Very nice approach; wonderful example. $\endgroup$ – dantopa May 22 '15 at 21:06
1
$\begingroup$

Eliminate the options and impose a reasonable WorkingPrecision:

Clear[pp];
pp[\[Psi]_] := 
 ParametricPlot[{r, \[Psi]}, {r, -1, 1}, 
  PlotRange -> {Automatic, 1.05 {-1, 1}},
  WorkingPrecision -> 200]

enter image description here

$\endgroup$
  • $\begingroup$ Why is WorkingPrecision only needed inside of the function wrapper? (Also, WorkingPrecision->100 can be placed in the options slot.) One reason to use ParametricPlot in lieu of Plot is to obviate the need to adjust WorkingPrecision. Thanks for your comment. $\endgroup$ – dantopa May 22 '15 at 19:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.