Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I'm new to Mathematica, so I thought I'd try to exercise some solution curves. Consider a simple first order ODE: $y'(x)=\cos(x)$. We know the solution will be of the form $y = \sin(x)+c$

I tried to use a Table function to generate a list of solutions that I'd then use with the ContourPlot function as follows:

solns = Table[y = Sin[x] + i, {i, -5, 5}]

Then use ContourPlot:

ContourPlot[y == solns, {x, -5, 5}, {y, -5, 5}, FrameLabel -> Automatic]

But examine the label of the ordinate! It appears as:


The trouble lies in the definition of solns; when y=Sin[x]+i is used, for some reason the output is still the correct list that we expect:

{-5 + Sin[x], -4 + Sin[x], -3 + Sin[x], -2 + Sin[x], -1 + Sin[x], 
 Sin[x], 1 + Sin[x], 2 + Sin[x], 3 + Sin[x], 4 + Sin[x], 5 + Sin[x]}

However y is set equal to the last value in this list as an unintended consequence.

My question here is several fold:

  1. Is y being set to each element in the output list in turn, and only the last value is being used?
  2. Why didn't the output after I had evaluated this function show this sort of 'side effect'? I'd have expected it to be on the 'output' line associated with the Table function.
share|improve this question

2 Answers 2

up vote 8 down vote accepted

When you do solns = Table[y = Sin[x] + i, {i, -5, 5}], you are setting y=Sin[x]+i at each iteration; the last one is y=Sin[x]+5, so that is what y is.

If you instead do solns = Table[Sin[x] + i, {i, -5, 5}] and then ContourPlot[y == solns, {x, -5, 5}, {y, -5, 5}, FrameLabel -> Automatic] then you get y as a label, because now no value has been assigned to y.

Regarding it being an unintended consequence, y=expr explicitly sets y to expr; so if you do not intend this, then simply do not put y there. And as for this not being indicated, it is not indicated for the same reasons something like x=12 would not be; it just returns the value being assigned to x.

That is, what is effectively happening with something like Table[f[i],{i,1,10}] is that a loop is set up (with i localized, effectively with Block); at each iteration, f[i] is evaluated, and whatever it returns is appended to a list. In the end, the list is returned. Thus, if evaluating f[i] has side effects (such as assigning a value to y), you do not see that.

So in this case, you can think of Table[f[i], {i, 1, 10}] as equivalent to

  lst = {}},
  AppendTo[lst, f[i]], {i, 1, 10}
share|improve this answer

I think acl answered your questions, but I'm curious why you chose to use ContourPlot to display your solution? Perhaps I'm simply not understanding what you're trying to accomplish, but does the following work for you?

solns = Table[Sin[x] + i, {i, -5, 5}]
Plot[solns, {x, -5, 5},PlotStyle->Blue]

enter image description here

In case it's not clear why this worked, MMA cycles through options like PlotStyle for each curve in the figure. If there's only one style, it will necessarily apply that style to each curve (because the "cycle" is only one curve long). If you do PlotStyle->{Blue,Red} instead, you'll see alternating Blue and Red curves, and similarly if you do PlotStyle->{Blue,Red,Green}.

share|improve this answer
What if I want them all to be the same color? I tried: Table[{Blue, Sin[x] + i}, {i, -5, 5}] as an input to Plot[], but received no joy. –  trayres Feb 10 '12 at 17:57
@trayres: see my edit. –  David Skulsky Feb 10 '12 at 19:02
If you'd like to know which curve is which, replace the Plot expression with: Plot[Tooltip[solns], {x, -5, 5}, PlotStyle -> Blue –  murray Feb 11 '12 at 19:44

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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