Hopefully, this is a final cure for the Problem with Manipulate introduced on A problem with Manipulate and then continued on Continuation of a Problem with Manipulate. Michael E2 suggested on the last page on surrounding the Manipulate command with a DynamicModule. Here is my work.
Before I show all of my work, it is important to understand that this is all in a a single notebook. The desire is to not have the manipulate activities affect one another and the second desire is to not have the static stuff in the notebook affect the manipulates and vice-versa.
First, clearing the Global workspace and adding three variables that have caused problems in the links above.
Clear["Global*`"];
a = 2;
b = 10;
dx = (b - a)/n;
Now, here is my first use of Manipulate.
DynamicModule[{a = 0, b = 1, n, f, dx, rightSum},
f[x_] := x^2;
dx[n_] := (b - a)/n;
rightSum[n_] := Total@Table[f[a + i dx[n]] dx[n], {i, 1., n}];
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"}]]
Which produces this image.
Now the first test:
In[30]:= a
Out[30]= 2
In[31]:= b
Out[31]= 10
In[32]:= dx
Out[32]= 8/n
Note that because of the dynamic module, the variables in the workspace were not changed, nor did they have an effect on the variables and definitions in the manipulate activity. Now, the second manipulate activity.
DynamicModule[{a = 0, b = 1, n, f, dx, rightSum},
f[x_] := x;
dx[n_] := (b - a)/n;
rightSum[n_] := Total@Table[f[a + i dx[n]] dx[n], {i, 1., n}];
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"}]]
Which produces this image.
I cannot demonstrate this here, but I can let everyone know that the function f defined in the second manipulate (a straight line) did not affect the first manipulate. They remained the same. Now for the variables test.
In[27]:= a
Out[27]= 2
In[28]:= b
Out[28]= 10
In[29]:= dx
Out[29]= 8/n
Again, the manipulate did not affect the variables in the workspace, nor did they affect the variables in the manipulates.
Now, I am trying this based on one of MichaelE2's comments, which occurs at the bottom of Continuation of a Problem with Manipulate.
What do folks think? Is this the best, easiest, and safest approach for students and teachers who are just beginning to learn Mathematica?