4
$\begingroup$

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?

$\endgroup$
2
  • $\begingroup$ closely related: 1929 $\endgroup$
    – Kuba
    Commented Jun 17, 2015 at 9:26
  • 1
    $\begingroup$ Cleaner: Plot[Sin[x], {x, ##}] & @@ {0, 2 π}. $\endgroup$ Commented Jun 17, 2015 at 9:51

2 Answers 2

7
$\begingroup$

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}]
$\endgroup$
5
  • $\begingroup$ Ah, now I understand. If x is define somewhere then @m0nhawk's solution fails? $\endgroup$
    – JeffDror
    Commented Jun 17, 2015 at 9:28
  • $\begingroup$ @JeffDror Yes, unfortnately. Evaluate prevents x from being properly scoped. $\endgroup$
    – Kuba
    Commented Jun 17, 2015 at 9:29
  • $\begingroup$ What if you use With[] for the insertion? $\endgroup$ Commented Jun 17, 2015 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
    Commented Jun 17, 2015 at 9:55
  • $\begingroup$ Ooh, forgot that thread. Thanks for the reminder! $\endgroup$ Commented Jun 17, 2015 at 9:57
4
$\begingroup$

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}]
$\endgroup$
2
  • $\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
    Commented Jun 17, 2015 at 9:24
  • 4
    $\begingroup$ @JeffDror xrange = Sequence[0, 2 \[Pi]]; x = 1; Plot[Sin[x], Evaluate@{x, xrange}] $\endgroup$
    – Kuba
    Commented Jun 17, 2015 at 9:27

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.