10
$\begingroup$

I'm trying to run this:

domain := {n, 0, 10};
Plot[n, domain]

but it doesn't work. Instead, it generates the message

Plot::pllim: Range specification domain is not of the form {x, xmin, xmax}.

and returns

Plot[n, domain].

Why?

I've also tried alternatives such as using =, or trying to define Domain[n_] = {n, 0, 10} but it all seemed to be of no avail.

$\endgroup$
1
  • $\begingroup$ Works as-is in version 12. $\endgroup$
    – Rabbit
    Commented Feb 21, 2020 at 22:09

3 Answers 3

13
$\begingroup$

Use:

domain := {n, 0, 10};
Plot[n, Evaluate[domain]]

Plot has the HoldAll attribute which prevents domain from evaluating:

Attributes[Plot]
{HoldAll, Protected}
$\endgroup$
1
  • $\begingroup$ @Szabolcs, I think I should transfer half my reputation, since you make my terse posts actually readable. Thanks once more. $\endgroup$
    – user21
    Commented Feb 28, 2012 at 13:53
5
$\begingroup$

Indeed the cause is the evaluation order resulting from the HoldAll attribute of Plot. Here are several ways to get around this:

domain := {n, 0, 10};

Plot[n, Evaluate[domain]]

Plot[n, #] & @ domain

With[{d = domain}, Plot[n, d]]

{domain} /. {d__} :> Plot[n, d]
  • I normally favor the method using Function (&) for its brevity.
  • I think With can be the most easy to read in longer expressions.
  • The last method is specialized and is helpful in difficult operations.
$\endgroup$
2
$\begingroup$

Alternatively, I only define the range but not the iterator variable, as it is then outside of its scope (n in Plot[f, {n, 0, 1}] is local to Plot):

domain = {0, 10};
Plot[n, {n, First[domain], Last[domain]}]

or

domain = {0, 10};
Plot[n, Evaluate@{n, Sequence @@ domain}]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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