I wrote these for an answer here http://community.wolfram.com/groups/-/m/t/153862?p_p_auth=GLyok3xN
The manipulate expression is anything between the start of Manipulate and the first ","
Manipulate[Plot[Sin[x (1 + a x)], {x, 0, 6}], {a, 0, 2}]
It has the form
Manipulate[expression, controlVariables, Initialization :> ()]
foo[] := Plot[Sin[x (1 + a x)], {x, 0, 6}] Manipulate[Evaluate@foo[], {a, 0, 2}]
move foo[] in the above example to inside Manipulate, in the initialization section, add literal symbol a so Manipulate will track it
Manipulate[a; foo[], {a, 0, 2}, Initialization :> (foo[] := Plot[Sin[x (1 + a x)], {x, 0, 6}]) ]
Notice the trick above. a was added so that it appears in the Manipulate expression. Otherwise, it will not be tracked. You’ll get an initial plot, but nothing will happen when moving the slider
move foo[] to global context, but have to tell Manipulate that LocalizeVariable is false
foo[] := Plot[Sin[x (1 + a x)], {x, 0, 6}]; Manipulate[a; foo[], {a, 0, 2}, LocalizeVariables -> False]
But notice, we still need to put a somewhere in the expression for it to tracked. Not enough just to say LocalizeVariables -> False
It is not enough to use TrackedSymbols :>a, if the symbol itself do not show up in the expression. Hence this does not work
foo[] := Plot[Sin[x (1 + a x)], {x, 0, 6}]; Manipulate[foo[], {a, 0, 2}, LocalizeVariables -> False, TrackedSymbols :> a]
Notice there is no a in the expression. Manipulate will not track a even though we told it to !
Same as case 4, even if we put the function inside the Initialization section, it will still not track a . One will get an initial plot, but that is all.
Manipulate[foo[], {a, 0, 2}, TrackedSymbols :> a, Initialization :> (foo[] := Plot[Sin[x (1 + a x)], {x, 0, 6}];)]
Putting the function definition of foo[] itself inside Manipulate expression, now Manipulate sees a there and will automatically track it. No need to do anything more:
Manipulate[Module[{}, foo[] := Plot[Sin[x (1 + a x)], {x, 0, 6}]; foo[]], {a, 0, 2}]
Or simply
Manipulate[Plot[Sin[x (1 + a x)], {x, 0, 6}], {a, 0, 2}]
This is the method I use myself. Put all the functions inside the initialization section, but pass the dependent variables by argument call.
Manipulate[foo[a], {a, 0, 2}, Initialization :> ( foo[a_] := Module[{x}, Plot[Sin[x (1 + a x)], {x, 0, 6}]] )]
I like this, becuase it achieves both the goal of having all the slider symbols inside the Manipulate expression, hence Manipulate will track them, and at the same time, it avoid the function be in global context, and it is the modular way, since one can see which parameter each function depends on by looking at the signature of the function.
Similar to case 7, but the function itself is now in the global context. This is a fine solution as well, if this function needs to be called from somewhere else as well other than from the Manipulate. Otherwise, it is best not to have in the global context and use case 7.
foo[a_] := Module[{x}, Plot[Sin[x (1 + a x)], {x, 0, 6}]]; Manipulate[foo[a], {a, 0, 2}]