# Does DynamicModule cure Problem with Manipulate?

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:= a

Out= 2

In:= b

Out= 10

In:= dx

Out= 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:= a

Out= 2

In:= b

Out= 10

In:= dx

Out= 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?

Here's how I would do it:

Manipulate[
Plot[f[x], {x, a, b}, PlotStyle -> Thick, AxesLabel -> {"x", "y"},
Epilog ->
Dynamic@{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"},
{{a, 0}, None}, {{b, 1}, None}, {f, None}, {dx, None}, {rightSum, None},
Initialization :> (
Clear[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}];)]


Points of comparison:

• It creates a single DynamicModule via Manipulate, which is being used anyway. It's a simpler structure than the nested ones in the OP's code. Nesting DynamicModule is not bad per se, but it's complicated. (The outside DM contains the code to create an instance of the inside DM, when the outside DM is instantiated by the Front End. You can figure the rest out on your own, or not, as you wish. The subtleties rarely matter, but I prefer keeping things simple.)

• The plotted function never changes, but the rectangle graphics do. I isolated them with Dynamic so that they may be updated independently. This means that moving the Manipulator updates only the rectangles and is more responsive.

• Both the OP's method and this one localize all the variables (x being localized by Plot) so that one instance will be completely independent of another. (The use of Clear is necessary because {f, None} causes f first to be initialized to 0, which disrupts the definition of f; an alternative is to use the declaration form {{f, f}, None} instead of {f, None}, which initializes f to itself, i.e., to its Symbol.)

Further discussion of localization and Manipulate may be found here:

• This is going to seriously wonderful lesson! Thanks for the great effort. – David Aug 7 '15 at 4:50
• What code would I enter to time your new version vs. my older version? And I would be probably timing the difference after moving the n slide? – David Aug 7 '15 at 15:31
• In the body of the Manipulate, I would use SessionTime[] and calculate the difference each update; you'd have have to save the time in a variable, use TrackedSymbols to manage the updating, and display the difference somewhere. But since Manipulate` is mainly about human perception of responsiveness, just try it both ways. It will be noticeable if the plot takes a tenth of a second or more to compute; if it takes more than a few tenths, then it will seem a definite improvement. Graphics generally involves the kernel, the front end, and the GPU -- rather complicated to analyze precisely. – Michael E2 Aug 7 '15 at 15:51