For the beginning of this discussion, see: A problem with Manipulate.
Now, the final use of the Initialization command doesn't protect the Global workspace. What I am discussing is not a single Manipulate object. Rather, imagine a notebook filled with static code and many Manipulate objects.
Now, here is an example. Suppose that the values of a and b before performing the Manipulate are assigned as follows:
a = 1; b = 5;
Then you run the Manipulate object.
Manipulate[
Show[Plot[f[x], {x, a, b}, PlotStyle -> Thick,
AxesLabel -> {"x", "y"}],
Graphics[{Table[{Opacity[0.05], EdgeForm[Gray],
Rectangle[{a + i dx[n], 0}, {a + (i + 1) dx[n],
f[a + (i + 1) dx[n]]}]}, {i, 0, n - 1, 1}],
Text["N = " <> ToString[n] <> ", R = " <>
ToString[rightSum[n]], {(a + b)/2, f[b]}]}]], {{n, 10}, 10, 50,
10, Appearance -> "Labeled"},
Initialization :> (a = 0; b = 1; dx[n_] := (b - a)/n; f[x_] := x^2;
rightSum[n_] := N@Sum[f[a + i dx[n]] dx[n], {i, 1, n}])]
Now, you are back in your workspace and you enter:
In[37]:= a
Out[37]= 0
In[38]:= b
Out[38]= 1
So, you can see that the Manipulate code affected objects in the workspace. So, question 1 is, what do you add to this code to prevent this from happening.
Here is a second example. Supposed that in the workspace you have defined:
dx = (b - a)/n
Then you try to run the Manipulate object.
Manipulate[
Show[Plot[f[x], {x, a, b}, PlotStyle -> Thick,
AxesLabel -> {"x", "y"}],
Graphics[{Table[{Opacity[0.05], EdgeForm[Gray],
Rectangle[{a + i dx[n], 0}, {a + (i + 1) dx[n],
f[a + (i + 1) dx[n]]}]}, {i, 0, n - 1, 1}],
Text["N = " <> ToString[n] <> ", R = " <>
ToString[rightSum[n]], {(a + b)/2, f[b]}]}]], {{n, 10}, 10, 50,
10, Appearance -> "Labeled"},
Initialization :> (a = 0; b = 1; dx[n_] := (b - a)/n; f[x_] := x^2;
rightSum[n_] := N@Sum[f[a + i dx[n]] dx[n], {i, 1, n}])]
Now it doesn't work, sending error messages such as:
Coordinate {(4/$CellContext`n)[10], (4/$CellContext`n)[10]^2} should be a pair of numbers, or a Scaled or Offset form. Not only that, but my Documentation notebook is also messed up.
Here is a third problem. Suppose I change the function and a and b in the initialization section to create a new Manipulate. That is, I first do this: set a=0, b=1, and f[x_]:=x^2.
Manipulate[
Show[Plot[f[x], {x, a, b}, PlotStyle -> Thick,
AxesLabel -> {"x", "y"}],
Graphics[{Table[{Opacity[0.05], EdgeForm[Gray],
Rectangle[{a + i dx[n], 0}, {a + (i + 1) dx[n],
f[a + (i + 1) dx[n]]}]}, {i, 0, n - 1, 1}],
Text["N = " <> ToString[n] <> ", R = " <>
ToString[rightSum[n]], {(a + b)/2, f[b]}]}]], {{n, 10}, 10, 50,
10, Appearance -> "Labeled"},
Initialization :> (a = 0; b = 1; dx[n_] := (b - a)/n; f[x_] := x^2;
rightSum[n_] := N@Sum[f[a + i dx[n]] dx[n], {i, 1, n}])]
Then set a=0, b=2, and f[x_]=x:
Manipulate[
Show[Plot[f[x], {x, a, b}, PlotStyle -> Thick,
AxesLabel -> {"x", "y"}],
Graphics[{Table[{Opacity[0.05], EdgeForm[Gray],
Rectangle[{a + i dx[n], 0}, {a + (i + 1) dx[n],
f[a + (i + 1) dx[n]]}]}, {i, 0, n - 1, 1}],
Text["N = " <> ToString[n] <> ", R = " <>
ToString[rightSum[n]], {(a + b)/2, f[b]}]}]], {{n, 10}, 10, 50,
10, Appearance -> "Labeled"},
Initialization :> (a = 0; b = 2; dx[n_] := (b - a)/n; f[x_] := x;
rightSum[n_] := N@Sum[f[a + i dx[n]] dx[n], {i, 1, n}])]
If you enter both in a notebook, then evaluate the notebook, you will see that both graphs and values of a and b are the ones defined in the second Manipulate chunk.
So I need to learn more. This is similar to what I posted at: Difficulty with Manipulate that has same variables as other code lines in a notebook, but I obviously need to learn more.
What should be added to the Manipulate code to prevent these things from happening?