Re: Using Intermediate Variables in DynamicModules

*To*: mathgroup at smc.vnet.net*Subject*: [mg80359] Re: Using Intermediate Variables in DynamicModules*From*: Albert <awnl at arcor.net>*Date*: Sat, 18 Aug 2007 05:42:38 -0400 (EDT)*References*: <fa130g$mt7$1@smc.vnet.net>

Hi, > I am having a lot of trouble with a DynamicModule expression. I believe that > the following examples get to the heart of the matter. > > Here is a DynamicModule that uses an external function f. It has two > internal variables a and b, which are controlled by sliders. The third > output line gives a+b and f[a+b]. > > f[x_] := x^2 > DynamicModule[ > {a = 0.5, b = 0.5}, > Column[{Slider[Dynamic[a], {0, 2}], > Slider[Dynamic[b], {0, 2}], {Dynamic[a + b], Dynamic[f[a + b]]}}]] > > > Notice that f[a+b] is fully evaluated in the output. Now in my actual case I > have a long and complicated expression involving a and b that is used in a > number of different places. So to clarify the entire expression I would like > to define an intermediate expression that can then be used in the dynamic > output. Here is my attempt: > > f[x_] := x^2 > DynamicModule[ > {a = 0.5, b = 0.5, work}, > work = Dynamic[a + b]; > Column[{Slider[Dynamic[a], {0, 2}], > Slider[Dynamic[b], {0, 2}], {work, f[work]}}]] > > Now, although the output is formally correct, the output, and in particular > f[work], is not fully evaluated. I think that may be the cause of my > problem. Why, in the first case does the f expression become fully evaluated > but not in the second case? Is there a simple way to force kernel evaluation > when intermediate expressions are used? (I.ve tried many combinations of > using Dynamic in the output expression and none of them changed the > behavior.) Actually I am not sure whether the above and below techniques are meant to be used this way, but the following happens to work for me: f[x_] := x^2; DynamicModule[{a = 0.5, b = 0.5, work}, work = Dynamic[a + b]; Column[{ Slider[Dynamic[a], {0, 2}], Slider[Dynamic[b], {0, 2}], { Dynamic[work], Dynamic[f[Setting[work]]] } }] ] Maybe you want to have a look at the documentation of Setting to decide if this is a good solution for what you want to achieve... I have not investigated this in detail but with the "advanced" usage of dynamics I think it is a good idea to try to understand which parts are evaluated by the kernel and which settings are done by the FrontEnd and when each of these happen. You can get try to get an idea by looking at the cell expressions (Ctrl-Shift-E on Windows) of the output cells which contain dynamic stuff (or look at the InputForm of the above expression). You also might also want to look at the options UnsavedVariables and Initialization for DynamicModule... hth, albert

**Re: What determines what is assigned to Out[]?**

**Re: Mathematica 6**

**Re: Using Intermediate Variables in DynamicModules**

**What determines what is assigned to Out[]?**