3
$\begingroup$

Consider the following function:

f[n_?NumericQ] := n;
f[args___] := Throw[{args}];

Then try to plot it:

Plot[f[x], {x, 1, 10}]

Throw::nocatch: Uncaught Throw[{Charting`Private`pvar$7957}] returned to top level. Hold[Throw[{Charting`Private`pvar$7957}]]


Examination

If we now redefine f without the catch-all definition, and examine the behaviour of Plot, we find the following:

Clear[f];
f[n_?NumericQ] := n;
Reap@Plot[f[Sow[x]], {x, 1, 10}]

{-Graphics-, {{1, 1.00018, Charting`Private`pvar$8374, 1., 1.17666, 1.36818, ..., 9.80849, 9.90733, 9.95675, 9.98147, 9.99382, 1.00276, 9.8054, 9.90424, 9.95367, 9.97838, 9.99073, 9.99691}}}

So why does Plot evaluate function with argument Charting`Private`pvar$..., and moreover why after 2 numerical substitutions?

How can Plot work with a function that has a catch-all definition which throws error if its other patterns aren't matched?

I would like, if possible, to have Plot throw if the pattern checks of f don't match, and Catch it on a higher level.

$\endgroup$
  • 1
    $\begingroup$ Plot[Catch[f[x]], {x, 1, 10}] ? $\endgroup$ – Daniel Lichtblau Nov 12 '19 at 16:21
  • $\begingroup$ Thank you! You are correct, I forgot to mention I would like, if possible, to have Plot throw if the pattern checks of f don't match, and Catch it on a higher level. If I catch the error inside Plot everything will seem as if it worked right and a plot will be created, even when all of the plot's sampled [x]s evaluate to Throw. $\endgroup$ – Spyros Thanasoulas Nov 12 '19 at 17:07
  • 1
    $\begingroup$ Use f[n_?NumericQ] := n; f[_Symbol] = Null; f[args___] := Throw[{args}]? $\endgroup$ – Carl Woll Nov 12 '19 at 17:13
  • $\begingroup$ @Carl This seems to be what I'm looking for! Using head Symbol in patterns will do it for me I'm pretty certain, thanks! Any feedback on why that symbol is evaluated as x? Isn't that kind of an odd behaviour or am I missing something? $\endgroup$ – Spyros Thanasoulas Nov 12 '19 at 18:14

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.