1
$\begingroup$

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?

$\endgroup$
1
  • $\begingroup$ You might repeat your experiment with a Manipulate hidden inside a Module. Perhaps that will give you the name isolation that you are looking for. $\endgroup$
    – Bill
    May 25, 2015 at 21:50

1 Answer 1

3
$\begingroup$

I would localize the variables inside the DynamicModule created by the Manipulate by using the ControlType -> None (or simply None for short) specification. This use is discussed here:

Code:

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"},
 {{a, 0}, None}, {{b, 1}, None},
 {{dx, dx}, None}, {{f, f}, None},
 {{rightSum, rightSum}, None},
 Initialization :> (
   a = 0; b = 1;
   dx[n_] := (b - a)/n;
   f[x_] := x^2;
   rightSum[n_] := Total@Table[f[a + i dx[n]] dx[n], {i, 1., n}])]

Note that a declaration of the form {var, None} is equivalent to {{var, 0}, None}, so that the variable var would be initialized to 0. To prevent this, I initialized dx, f, and rightSum to their own symbols. If they were initialized to 0, then the definitions, dx[n_] := ... etc., would generate errors. (These initializations happen before the Initialization option is evaluated.)

$\endgroup$
6
  • $\begingroup$ Oh my goodness! http://mathematica.stackexchange.com/questions/72422/controltype-none-vs-module-inside-manipulate-i-e-making-everything-local/72477#72477 is a blast from the past. I don't remember reading this before. Might have had something to do with Christmas vacation. But it is awesome and I checked it for credit and I'm going to study it in detail tonight. Awesome help Michael. You are really helping me. $\endgroup$
    – David
    May 26, 2015 at 2:29
  • $\begingroup$ @David Very glad to help. Thanks. $\endgroup$
    – Michael E2
    May 26, 2015 at 2:35
  • $\begingroup$ my #1 concern is the writing of notebooks to help students and instructors at our school be introduced to mathematica at a level that is easiest for beginners to understand. One of my favorite books is the Students's Introduction to Mathematica. It has been extremely helpful, but the authors wrap manipulate inside a module and they place modules inside manipulate. Do you have a text recommendation that trains its readers to use the manipulate command at a proper level? $\endgroup$
    – David
    May 26, 2015 at 5:54
  • $\begingroup$ @David I don't have a recommendation, and I would expect a book by Bruce Torrence, whom I've met, to be pretty good. Dynamic stuff is complicated and constructs that work in a narrow context don't work in a broader one. The notion of "scope" (of a symbol) becomes many times more complicated by Manipulate (or DynamicModule), and people new at programming find scope a difficult concept even at its simplest. In this context, what you show you're doing in your questions is somewhat sophisticated, probably an appropriate stretch for instructors but perhaps a little much for some students. $\endgroup$
    – Michael E2
    May 26, 2015 at 12:23
  • $\begingroup$ @David You could tell your students & colleagues that any the Module outside a Manipulate should be a DynamicModule. That would probably work in most/all use-cases and is a fairly straightforward instruction. (Other than initialization, which needs some special care.) $\endgroup$
    – Michael E2
    May 26, 2015 at 13:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.