4

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?

m0nhawk
  • 3,867
  • 1
  • 20
  • 35
JeffDror
  • 1,880
  • 1
  • 13
  • 29

2 Answers2

7

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}]
Kuba
  • 136,707
  • 13
  • 279
  • 740
4

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}]
m0nhawk
  • 3,867
  • 1
  • 20
  • 35
  • Hmm, this actually does seem to work now (Mathematica 10.0.0 Linux) . I was sure that I tried this... Thanks for your response. – JeffDror Jun 17 '15 at 09:24
  • 4
    @JeffDror xrange = Sequence[0, 2 \[Pi]]; x = 1; Plot[Sin[x], Evaluate@{x, xrange}] – Kuba Jun 17 '15 at 09:27