I have encountered unexpected behaviour when assigning variables in Manipulate. Consider an inverted parabola
y[x_, c_] := -x^2 + c
The following code finds the maximum of y and plots it
Manipulate[
Grid[{
{ ymax = FindMaximum[{y[x, c], -1 <= x <= 1}, x] },
{ Plot[y[x, c], {x, -1, 1}] }
}],
{c, -0.5, 0.5}]
which gives, for a the initial default value of c=-0.5,
In[3]:= ymax
Out[3]:= {-0.5, {x -> -1.70726*10^-14}}
Now I would expect that when I re-use ymax to calculate the maximum with a different value of c=2, it would permanently change the state of ymax:
In[4]:= ymax = FindMaximum[{y[x, 2], -1 <= x <= 1}, x]
Out[4]:= {2, {x -> -1.70726*10^-14}}
But it does not:
In[5]:= ymax
Out[5]:= {-0.5, {x -> -1.70726*10^-14}}
It seems that the cell with Manipulate in it re-evaluates every time I evaluate any cell; for example when I try to start with a clean slate and run
ClearAll["Global`*"]
in a different cell, the cell with Manipulate throws a bunch of error messages (because I have cleared y[x,c] and so Manipulate tries to evaluate without that function defined).
My questions are: is this behaviour, re-evaluation of Manipulate when you evaluate, known behaviour? Is it useful in a different context? Can I avoid it if I really want to re-use the variable again outside Manipulate, for whatever reason?
(General tips also welcomed as I am (clearly) a Mathematica novice!)