# How to scope variables in Manipulate (for Autorun)

This question is connected with that one (and all other connected). The purpose is to make a demonstration that has a Dynamic part among controls (I'm aware that it is not recommended, but nonetheless...). An artificial example.

Manipulate[
f[x_] = 5 - a x;
sol = First@Solve[f[x] == 0, x];

Plot[f[x], {x, 0, 5},
PlotRange -> {0, 5},
Epilog -> Text[x /. sol, {5., 5.}, {1., 1.}], (* !!! *)
ImageSize -> Small]
,
Dynamic[x /. sol],
{sol, None},
{a, 1, 2},
TrackedSymbols -> {a},
AutorunSequencing -> {2}
]


Assume that we need sol variable to use it in code repeatedly (say, Solve is huge and is used in many places). The code works fine unless one launches Autorun mode. Then periodically one may see the following.

The question: 1) how to avoid this problem given that we need to localize sol for Manipulate (I'm using None construct as being recommended). 2) explain a bit why does it happen (how does Autorun mode work in this case).

• Try setting the initial value of sol to be a valid rule, i.e. use something like {{sol, x -> 5}, None} Nov 24, 2015 at 19:36
• @SimonWoods, thank you for comment, it solve the problem to hide x\. and applies the rule. In more involved cases (when sol is a result of many calculations with many controls) it might not help. Nov 24, 2015 at 20:03

When Autorun goes to repeat, there is a pause where the Manipulate appears in its initialized state. When you specify a local variable thus

{sol, None}


the variables is automatically initialize to 0. To get something other than x /. 0 to appear, you should explicitly initialize sol. One of the following may be suitable (the output above the slider is noted in the commented code):

{{sol, {}}, None}          (* output: x *)
{{sol, {x -> 0}}, None}    (* output: 0 *)
{{sol, sol}, None}         (* output: 5 -- uses current value of sol *)


Whether one of these or another initialization seems most appropriate is a matter of style left to the user.

• {{sol, sol}, None} this works nice, thank you. Nov 25, 2015 at 6:40
• @garej You're welcome and thanks for the accept. {{sol, sol}, None} is the form I most often use. Nov 25, 2015 at 12:18

In your artificial example (at least), the problem can be avoided by eliminating sol altogether.

Manipulate[
Dynamic @ Column[{
Solve[f[x] == 0, x][[1, 1, 2]],
Plot[f[x], {x, 0, 5}, PlotRange -> {0, 5}, ImageSize -> Small]
}],
{{a, 1}, 1, 2, Appearance -> "Labeled"},
Initialization :> (f[x_] := 5 - a x)]


The moral of this story may be that your simplified example is too simple to represent your actual problem accurately.

"Everything should be made as simple as possible, but not simpler." - Albert Einstein

### Update

This is my attempt to come up with an example where it is plausible for the root of f[x] == 0 be captured in a localized dynamic variable.

Manipulate[
Dynamic[
root = Solve[f[x] == 0, x][[1, 1, 2]];
Plot[f[x], {x, 0, 5},
PlotRange -> {0, 5},
Epilog -> Text[root, {5., 5.}, {1., 1.}],
ImageSize -> Small]],
{{a, 1}, 1, 2, Appearance -> "Labeled"},
{root, None},
TrackedSymbols :> {a},
AutorunSequencing -> {1},
Initialization :> (f[x_] := 5 - a x)]


This example also avoids the problem you allude to and may be more applicable to your actual coding problem.

### Update 2

The case you bring up in your comment, having the root appear in the control area rather than in the content pane, is another situation where a localized variable is unnecessary.

Manipulate[
Dynamic @ Plot[f[x], {x, 0, 5}, PlotRange -> {0, 5}, ImageSize -> Small],
{{a, 1}, 1, 2, Appearance -> "Labeled"},
Dynamic @ Row[{"root = ", Solve[f[x] == 0, x][[1, 1, 2]]}],
Initialization :> (f[x_] := 5 - a x)]


• May I ask you to add a string to your code to make root changing on the control panel? Nov 24, 2015 at 21:48