2
$\begingroup$

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.

enter image description here

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).

$\endgroup$
2
  • 1
    $\begingroup$ Try setting the initial value of sol to be a valid rule, i.e. use something like {{sol, x -> 5}, None} $\endgroup$ Nov 24, 2015 at 19:36
  • $\begingroup$ @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. $\endgroup$
    – garej
    Nov 24, 2015 at 20:03

2 Answers 2

3
$\begingroup$

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.

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

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)]

demo

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)]

demo2

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)]

demo3

$\endgroup$
1
  • $\begingroup$ May I ask you to add a string to your code to make root changing on the control panel? $\endgroup$
    – garej
    Nov 24, 2015 at 21:48

Your Answer

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

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