1
$\begingroup$

The question is related to a number of questions that have been asked, but no answer is quite what I want, so I will try again. Suppose that I have a model with a lot of state - to give a stupid example, something like:

a = 1;
b = 2;
...
z= 26;

foo[bar_]:= bar^(a+b+...+z)

Now I want a Manipulate expression where I can modify all of my state (and the value of foo). To make things more interesting, suppose I have several functions (foo, 'baz', frobboz) which all need to do this, and I do not want to replicate the code. Any suggestions are welcome.

$\endgroup$
  • $\begingroup$ Please always try to bring a minimal example, here a Manipulate is missing. It may be obvious but it is not uncommon that simple questions are misinterpreted. $\endgroup$ – Kuba Nov 6 '19 at 6:30
  • $\begingroup$ Is this topic the answer: 10604, or do you just want to use LocalizeVariables -> False option? $\endgroup$ – Kuba Nov 6 '19 at 6:31
  • $\begingroup$ I had tried the localize variables thing before asking, it does not seem to work. that is, setting bar = 1.0, foo[x_]:= Sin[bar x], Manipulate[Plot[foo[x], {x, 0, Pi}], {{bar, 2.0, "bar"}}, LocalizeVariables-> False] does not work, but maybe I am missing something. $\endgroup$ – Igor Rivin Nov 6 '19 at 6:34
  • $\begingroup$ That is why mentioned example was needed from the start. Without a reference to bar in the body of Manipulate, it won't update so you need: Manipulate[bar; Plot[foo[x], {x, 0, Pi}], .... $\endgroup$ – Kuba Nov 6 '19 at 6:40
  • $\begingroup$ @Kuba Thank you, that does work! But it does not address the second part of the question: Should every Manipulate have the same fifty variables mentioned by name? Or is there some more elegant way to do encapsulate? $\endgroup$ – Igor Rivin Nov 6 '19 at 6:44
4
$\begingroup$

If you do want to keep variables localized then How are parameters evaluated for a Plot in Manipulate is the answer.

If you don't then you can use LocalizeVariables -> False.

One needs to remember that in case of:

bar = 1.0; 
foo[x_]:= Sin[bar x];
Manipulate[Plot[foo[x], {x, 0, Pi}], {{bar, 2.0, "bar"}}, LocalizeVariables-> False]

there is no sign of bar within Manipulate body so it won't know it needs to update. A common trick is to put it there e.g.:

Manipulate[bar; Plot[...

But in case of fity variables and many manipulates it is not neat so you can use TrackedSymbols -> All option:

Manipulate[
 Plot[foo[x], {x, 0, Pi}], {{bar, 2.0, "bar"}, 1, 10}, 
 LocalizeVariables -> False, TrackedSymbols :> All]

Manipulate[
 Plot[-foo[x], {x, 0, Pi}], LocalizeVariables -> False, 
 TrackedSymbols :> All]
$\endgroup$
  • $\begingroup$ Very cool, thanks! $\endgroup$ – Igor Rivin Nov 6 '19 at 15:53
4
$\begingroup$

To explore complex expressions interactively in a notebook, Dynamic[] is often more nice than Manipulate.

Here is a example where :

  • I begin to plot a expression without knowing a priori what parameter I will change
  • Then I play with the parameter bar first by assigning the value 2, then I create a slider to play dynamically with bar
  • then I find a nice value for bar, so I type bar = bar and evaluate the second barin place. That's simply a method to memorize the value somewhere in the notebook, eventually with comments in (* *)
  • then I can play between the different memorized value of bar simply by evaluating the corresponding cell
  • One can redefine foo too and see immediatly the effect.

enter image description here

$\endgroup$
  • $\begingroup$ That's very cool! $\endgroup$ – Igor Rivin Nov 10 '19 at 15:58

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.