3
$\begingroup$
Manipulate[
  f[n_] := 1/n^2; 
  pt = Accumulate[Table[Table[f[i], {i, max}][[n]], 
          {n, PermutationReplace[Table[i, {i, max}], RandomPermutation[max]]}]]; 

  DiscretePlot[pt[[n]], {n, 1, max}],

  {{max, 5}, 2, 15, 1, Appearance -> "Labeled"},

  Deployed -> True]

The problem is that Mathematica constantly reevaluates the randomization. Even if the mouse is completely outside the Manipulate, the plot constantly flashes and changes.

But now here goes the really weird part. If the whole Manipulate gets a bit less complex... this behaviour ceases.

Manipulate[
  pt = Accumulate[PermutationReplace[Table[i, {i, 1, max}], 
          RandomPermutation[max]]]; 

  DiscretePlot[pt[[n]], {n, 1, max}], 

  {{max, 5}, 2, 15, 1, Appearance -> "Labeled"},
  Deployed -> True]

Everything looks nicely here. Yes, it does seem Mathematica re-randomizes this two or three Times too often, so it takes a second for the plot to calm down and stop changing; but after that time, everything looks well.

How to fix this? And why does this happen?

PS. Sorry for spamming the site... I really need to have a better knowledge of Mathematica, I know. But, for my defence, this really seems weird and unandersandable.

$\endgroup$
  • 2
    $\begingroup$ Move the function definition outside the manipulate. $\endgroup$ – bill s Jan 14 '15 at 4:40
  • $\begingroup$ @bills Can't believe it, works! Finally stopped this bizzare blinking. Thanks! But, could you elaborate this a little bit more? I'd be very grateful if you made me understand was was actually going on :) $\endgroup$ – gaazkam Jan 14 '15 at 4:49
  • $\begingroup$ Best to use the Initialization option to initialize the function rather than have it in the body as others have pointed out. $\endgroup$ – Mike Honeychurch Jan 14 '15 at 5:24
2
$\begingroup$

Manipulate updates each time its expression changes. The expression is a function of the control variables. Or if the expression contains a symbol or expression that appears only inside the Manipulate expression (i.e. not global before), even though it is not control variable, and this symbol/expression gets updated during evaluation of the manipulate expression.

In your example below, we see these symbols f[],n,i,max,pt

Mathematica graphics

The control variable is max, so when max changes, pt changes. The symbols i,n are local to Table by definition, hence they do not affect the update.

So what is left is f[]. Notice that when pt changes, inside the Table you are calling f[i], then Table will make repeated evaluation for each Table index See

http://reference.wolfram.com/language/tutorial/EvaluationInIterationFunctions.html

To solve this, you make make a module inside Manipulate, and define any symbols used in the expression, which are not control variable, in the module, like this:

Manipulate[
 Module[{f, n, i},
  f[n_] := 1/n^2;
  pt = Accumulate[
    Table[
     Table[Evaluate@f[i], {i, max}][[n]], {n, PermutationReplace[Table[i, 
        {i, max}], RandomPermutation[max]]}]
    ];
  DiscretePlot[pt[[n]], {n, 1, max}]
  ],
 {{max, 5}, 2, 15, 1, Appearance -> "Labeled"},
 Deployed -> True]

Another way to solve this, which is what I prefer to do, is to use TrackedSymbols.

 Manipulate[
 f[n_] := 1/n^2;
 pt = Accumulate[
   Table[
    Table[f[i], {i, max}][[n]], {n, PermutationReplace[Table[i, {i, max}],
       RandomPermutation[max]]}]
   ];
 DiscretePlot[pt[[n]], {n, 1, max}],
 {{max, 5}, 2, 15, 1, Appearance -> "Labeled"},
 TrackedSymbols :> {max},
 Deployed -> True]

Here is a simple example that shows the problem you were having

Manipulate[
 g[n_] := n;
 Table[g[i], {i, 1, max}],
 {{max, 10, "max"}, 1, 10, 1}
 ]

This will now re-evaluate repeatedly.

$\endgroup$
  • $\begingroup$ Many thanks, it works! One last question. Why: TrackedSymbols:>{max}, and not: TrackedSymbols->{max}? Why do we have to have here a RuleDelayed rather then a normal Rule? $\endgroup$ – gaazkam Jan 14 '15 at 13:31
  • 1
    $\begingroup$ From reference.wolfram.com/language/ref/TrackedSymbols.html Using Rule for TrackedSymbols fails in some cases because the symbols are evaluated That is why delayed in this case is always used. Generally, RuleDelayed should always be used for TrackedSymbols: This is very important. Always use :> with trackedsymbols. I once spent whole day trying to figure what I was doing wrong, then at the end I noticed I used -> instead of :> $\endgroup$ – Nasser Jan 14 '15 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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