MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Dynamic changing of variables

  • To: mathgroup at smc.vnet.net
  • Subject: [mg96344] Re: Dynamic changing of variables
  • From: Albert Retey <awnl at gmx-topmail.de>
  • Date: Thu, 12 Feb 2009 06:34:07 -0500 (EST)
  • References: <gmu8rl$gn3$1@smc.vnet.net>

Hi,

> DynamicModule[{a, b = 0}, 
>   a = Dynamic[Sin[b]]; 
>   Column[{
>     Dynamic[a], 
>     Slider[Dynamic[b], {0, 2*Pi}]
>    }]
> ]

> In order to update "a" with the actual value of Sin[b] I need 
> Dynamic around it. Unfortunately, now the variable "a" is invisibly
> wrapped and completely useless for further calculations. I'm not able to
> calculate even a+1 inside the DynamicModule.
> 
> DynamicModule[{a, b = 0}, 
>   a = Dynamic[Sin[b]]; 
>   Column[{
>     Dynamic[a+1], 
>     Slider[Dynamic[b], {0, 2*Pi}]
>    }]
> ]
> 
> If I'm not just too stupid and this behaviour is intended, then I'm
> wondering whether this doesn't lead to problems when you have more
> complex constructs with several dynamic variables.

You need to understand that Dynamic is something that only starts its
magic when it is shown within the FrontEnd. So this:

a = Dynamic[Sin[b]];

is assigning the Dynamic (actually a DynamicBox) to a, something that
only makes sense for the FrontEnd, not the Kernel. What you really want
is not to assign a to Dynamic[Sin[b]] but you want to assign Sin[b] to a
dynamically, which could be done with:

Dynamic[a=Sin[b]]

but again, this would only work if it appears visible within the
FronEnd, if you put a ; after it, it will not appear in the output, so
it will not do anything. I think this is more close to what you want:

DynamicModule[{a,b = 0},
 Column[{
  Dynamic[a = Sin[b]],
  Dynamic[a + 1],
  Slider[Dynamic[b], {0, 2*Pi}]
  }]
 ]

If you don't want the 'intermediate' result to be shown in the FrontEnd,
you can wrap everything and suppress output:

DynamicModule[{a,b = 0},
 Column[{
  Dynamic[
	a = Sin[b];
	a + 1
	],
  Slider[Dynamic[b], {0, 2*Pi}]
  }]
 ]

Another very useful mechanism to assign a value to a whenever b changes
is to use the second argument in Dynamic, like this:

DynamicModule[{a,b = 0},
 Column[{
   Dynamic[a + 1],
   Slider[Dynamic[b, (b = #; a = Sin[b]; #) &], {0, 2*Pi}]
   }]
 ]

hth,

albert


  • Prev by Date: Re: Custom label for a slider in Manipulate
  • Next by Date: Re: Help! About drawing a high-precision 3D graph
  • Previous by thread: Re: Dynamic changing of variables
  • Next by thread: Re: Dynamic changing of variables