2
$\begingroup$

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:

sin(x)+5

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.
$\endgroup$

2 Answers 2

8
$\begingroup$

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

Module[{
  lst = {}},
 Do[
  AppendTo[lst, f[i]], {i, 1, 10}
  ];
 lst
 ]
$\endgroup$
5
$\begingroup$

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?

Clear[x,i]
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}.

$\endgroup$
3
  • $\begingroup$ 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. $\endgroup$
    – trayres
    Commented Feb 10, 2012 at 17:57
  • $\begingroup$ @trayres: see my edit. $\endgroup$
    – Cassini
    Commented Feb 10, 2012 at 19:02
  • 2
    $\begingroup$ If you'd like to know which curve is which, replace the Plot expression with: Plot[Tooltip[solns], {x, -5, 5}, PlotStyle -> Blue $\endgroup$
    – murray
    Commented Feb 11, 2012 at 19:44

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.