1

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.

Igor Rivin
  • 5,094
  • 20
  • 19
  • 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. – Kuba Nov 06 '19 at 06:30
  • Is this topic the answer: 10604, or do you just want to use LocalizeVariables -> False option? – Kuba Nov 06 '19 at 06:31
  • 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. – Igor Rivin Nov 06 '19 at 06:34
  • 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}], .... – Kuba Nov 06 '19 at 06:40
  • @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? – Igor Rivin Nov 06 '19 at 06:44

2 Answers2

4

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]
Kuba
  • 136,707
  • 13
  • 279
  • 740
4

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

andre314
  • 18,474
  • 1
  • 36
  • 69