I would like to plot a function but have a domain defined elsewhere as follows:

xrange = Sequence[0, 2π];
Plot[Sin[x], {x, xrange}]

However, this doesn't work resulting in the error Range specification {x, xrange} is not of the form {x, xmin, xmax}. I've tried to put an Evaluate around {x, xrange} but Mathematica was not impressed.

I can think of two easy workarounds:

xrange = {0, 2π};
Plot[Sin[x], {x, xrange[[1]], xrange[[2]]}]
Plot[Sin[x], Evaluate[{x, ##} & @@ xrange]]

While these are fine solutions, they involve changing xrange to a List in a place where Sequence seems to fit perfectly. Is there a Sequence solution that I can use?


It would be also nice to preserve scoping of x by Plot so:

xrange = Sequence[0, 2 \[Pi]];
x = 1;
Plot[Sin[x], {x, ##}] &[xrange]

enter image description here

or, based on linked topic:

{xrange} /. {r__} :> Plot[Sin[x], {x, r}]
| improve this answer | |
  • $\begingroup$ Ah, now I understand. If x is define somewhere then @m0nhawk's solution fails? $\endgroup$ – JeffDror Jun 17 '15 at 9:28
  • $\begingroup$ @JeffDror Yes, unfortnately. Evaluate prevents x from being properly scoped. $\endgroup$ – Kuba Jun 17 '15 at 9:29
  • $\begingroup$ What if you use With[] for the insertion? $\endgroup$ – J. M.'s technical difficulties Jun 17 '15 at 9:53
  • $\begingroup$ @J. M. Sequence head will be there and Plot will still think it is only one argument. Something like in the linked topic. $\endgroup$ – Kuba Jun 17 '15 at 9:55
  • $\begingroup$ Ooh, forgot that thread. Thanks for the reminder! $\endgroup$ – J. M.'s technical difficulties Jun 17 '15 at 9:57

And what Mathematica version you have?

This works for me in Mathematica 10.0.2:

xrange = Sequence[0, 2 \[Pi]];
Plot[Sin[x], Evaluate@{x, xrange}]
| improve this answer | |
  • $\begingroup$ Hmm, this actually does seem to work now (Mathematica 10.0.0 Linux) . I was sure that I tried this... Thanks for your response. $\endgroup$ – JeffDror Jun 17 '15 at 9:24
  • 4
    $\begingroup$ @JeffDror xrange = Sequence[0, 2 \[Pi]]; x = 1; Plot[Sin[x], Evaluate@{x, xrange}] $\endgroup$ – Kuba Jun 17 '15 at 9:27

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.