4

I would like to plot a function which involves multiple symbols created using Unique. Consider the following simple example.

X = {Unique["x"]}
Plot[Evaluate[2 X[[1]]], {Evaluate[X[[1]]], 0, 1}]

Unfortunately, this results in an error Plot::write: "Tag Evaluate in Evaluate[X[[1]]] is Protected.".

Is there a way to plot using "dynamic" variables?

  • 1
    Related, possible duplicate: (7756), (20718), (46751) – Mr.Wizard Feb 07 '16 at 14:49
  • @Mr.Wizard It seems to me the underlying problem is that Evaluate should be placed outside the list, Evaluate@{X[[1]], 0, 1}. This is the issue in many posts, such as (5235) and (47907), and is also explained in this answer. I couldn't find one in which the issue was specifically using Evaluate in the second argument to Plot. It's usually the first argument. – Michael E2 Feb 07 '16 at 17:09
  • @MichaelE2 Yes, those are good, perhaps better, originals. And I found another related question and added it to my comment above. This question seems headed toward closure as "in the documentation" at this point; hopefully the links are useful to Markus and others. – Mr.Wizard Feb 07 '16 at 17:14
  • Thank you very much for the links. – Markus Müller Feb 07 '16 at 17:56

1 Answers1

8

If you have a relatively recent version of Mathematica, you don't need to use Evaluate.

X = {Unique["x"]};
With[{x = X[[1]]}, Plot[Legended[2 x, x], {x, 0, 1}]]

plot

m_goldberg
  • 107,779
  • 16
  • 103
  • 257