5
$\begingroup$

This works:

ClearAll[f1];
f1[x_] /; NumericQ[x] := x;
Plot[f1[x]^2, {x, 0, 5}]

enter image description here

This does not:

ClearAll[f2];
SetAttributes[f2, HoldAll];
f2[x_] /; NumericQ[x] := x;
Plot[f2[x]^2, {x, 0, 5}]

enter image description here

However, Table works:

Table[f2[x]^2, {x, 0, 5}]
(*  {0, 1, 4, 9, 16, 25}  *)

Q: Why?

P.S. These also fail (blank plots):

Plot3D[y^2 + f2[x]^2, {x, 0, 5}, {y, 0, 5}]
ContourPlot[y^2 + f2[x]^2, {x, 0, 5}, {y, 0, 5}]
DensityPlot[y^2 + f2[x]^2, {x, 0, 5}, {y, 0, 5}]

But these succeed:

ParametricPlot[{x, f2[x]^2}, {x, 0, 1}]
ParametricPlot3D[{x, f2[x]^2, x^3}, {x, 0, 1}]

One might test other plotters, but let's focus on Plot.

I get the same results for f2[x_?NumericQ] := x.

I'm using V12. The behavior is probably version-dependent.

$\endgroup$
  • 1
    $\begingroup$ This is just a bug that will probably be fixed in the next release. Diving into the "why" and finding workarounds might be academically interesting, but it's not behavior that needs to be explained in terms of the intended design of the functionality. $\endgroup$ – user6014 Jan 23 at 15:49
  • $\begingroup$ @user6014 Thanks. For some reason, I didn't even consider it might be a bug. $\endgroup$ – Michael E2 Jan 23 at 16:14
  • $\begingroup$ @user6014 Please go ahead and add the bugs tag and header in these cases. $\endgroup$ – Szabolcs Jan 24 at 9:08

Your Answer

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

Browse other questions tagged or ask your own question.