Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am trying to generalize a Manipulate construct into a DynamicModule one to try and include smoother controls, but I seem to be running afoul of Dynamic's compartmentalization of my intermediate variables inside what used to be a Module.

More specifically, I used to have a construct of the form

Manipulate[
 Module[{y},
  y = f[x];
  {x, g[y]}
  ]
 , {x, 0, 1}]

including (a large number of) intermediate expressions like y=f[x], and which produced output like

enter image description here

I now want to move control of the variable x into the Module - essentially, into a LocatorPane command - but I don't know what the correct translation of the code above should be. My naive attempt is to so something like

DynamicModule[{x, y},
 y = f[x];
 {Slider[Dynamic[x]], Dynamic[x], Dynamic[g[y]]}
 ]

and this produces no errors but the output inside g fails to update:

enter image description here

What is the correct generalization of the Manipulate[Module[ ... ] ] combination? I have tried inserting Dynamic commands in all possible combinations but I cannot seem to get them right. I know the answer is probably fairly basic, but I'm also a bit lost in the maze of tutorials for these commands.

Obviously, simply doing {Slider[Dynamic[x]], Dynamic[x], Dynamic[f[g[y]]]} is not an option in my case. The intermediate calculations f[x] are quite a few and very verbose, and the 'displayable' function g[y] is actually several rather long and slow plotting commands. I need to do the central bit of the computation only once, and I do not want to repeat myself in giving it to the plotting commands.

What am I missing here? It feels very basic but I can't seem to get it right.

share|improve this question
add comment

2 Answers 2

up vote 3 down vote accepted

To mimic Manipulate, wrap the body in Dynamic:

DynamicModule[{x, y},
 Dynamic[
  y = f[x];
  {Slider[Dynamic[x]], Dynamic[x], Dynamic[g[y]]}]
 ]

(It's what Manipulate does.)

But putting the y code inside its own Dynamic is a little cleaner:

DynamicModule[{x, y},
 {Slider[Dynamic[x]],
  Dynamic[x],
  Dynamic[y = f[x]; Dynamic[g[y]]]}
 ]
share|improve this answer
add comment

You should do something like:

DynamicModule[{x, y},
  {Slider[Dynamic[x]], Dynamic[x], Dynamic[g[y]]}
  , 
 Initialization :> (y = f[x];)]

for full control I would avoid using Manipulate. When you don't know what's going on, use Print for example. Let's focus on your Manipulate code:

Manipulate[Module[{y}, Print[1]; y = f[x];
  {x, g[y]}], {x, 0, 1}]

If you move the slider you are flooded by print. Now you know that your example works because definition of y is reevaluated at each update.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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