MathGroup Archive 2011

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

Search the Archive

Re: What is the point of having Initializations in DynamicModule and Manipulate?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg122995] Re: What is the point of having Initializations in DynamicModule and Manipulate?
  • From: "David Park" <djmpark at comcast.net>
  • Date: Sun, 20 Nov 2011 05:34:41 -0500 (EST)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • References: <398831.35647.1321702630679.JavaMail.root@m06>

Mike (or is it Armand? Both nice.),

I don't understand the purpose of the indicated display. Is the plot to be
completely static and just bundled with the dynamic part? In any case, for a
static plot:

Module[{g, plot},
 g[x_] := Cos[x];
 plot :=
  Plot[g[x], {x, 1, 10},
   PlotRange -> {-1.1, 1.1},
   Frame -> True,
   ImageSize -> 200];
 DynamicModule[{c = 0.5},
  {plot, Slider[Dynamic[c]], g[Dynamic@c]}]
 ] 

For a case where the plot depends on c as a parameter we could use:

DynamicModule[
 {c = 0.5, g, plot},
 g[x_] := Cos[x];
 plot[c_] := 
  Plot[g[c x], {x, 1, 10}, PlotRange -> {-1.1, 1.1}, Frame -> True,
   ImageSize -> 300];
 Column[{
   Dynamic@plot[c],
   Row[{"c: ", Slider[Dynamic[c], {0, 1}, Appearance -> "Labeled"]}]
   }]
 ]

Here is a somewhat more interesting case where we calculate a dependent
dynamic variable, d, and then use that in making the plot. Here calcAll is
the routine that calculates the dependent quantities and it is called from
the second argument of Dynamic in the Slider.

DynamicModule[
 {c = 0.5, d, g, plot, calcAll},
 g[x_] := Cos[x];
 
 (* Calculate dependent dynamic variables *)
 calcAll[cc_] := (d = cc^2; 
   plot = Plot[g[d x], {x, 1, 10}, PlotRange -> {-1.1, 1.1}, 
     Frame -> True,
     ImageSize -> 300]);
 
 (* Initialize *)
 calcAll[c];
 
 (* Display *)
 Column[{
   Dynamic@plot,
   Row[{"c: ", 
     Slider[Dynamic[c, (c = #; calcAll[c]) &], {0, 1}, 
      Appearance -> "Labeled"]}],
   Dynamic@Row[{"d: ", d}]
   }]
 ]

To me, this just seems to be more intuitive, although perhaps a bit more
wordy. And it works.


David Park
djmpark at comcast.net
http://home.comcast.net/~djmpark/  



From: Armand Tamzarian [mailto:mike.honeychurch at gmail.com] 

I'd be interested in feedback from other users about the way in which
Initializations works within DynamicModule (and Manipulate?). It is
fair to say that I expected an initialization to occur "initially."
That is to say I expected Initialization content to be evaluated prior
to the body of the DynamicModule. I have exchanged emails with tech
support about this. Attached is an example sent to me by tech support.

ClearAll[plot, g];

(* and in a separate cell *)
DynamicModule[{c}, {plot, Slider[Dynamic[c]], g[Dynamic@c]},
 Initialization :> (g[x_] := Cos[x]; plot = Plot[g[x], {x, 1, 10}]),
 UntrackedVariables -> {g}]

It is important for this example to keep the ClearAll line in a
separate cell so that the global variables are only cleared once. On
my system (OS X 10.6.8, Mathematica 8.0.4) the output is a list

 {plot, "slider", g[0.]}

where "slider" is the actual rendered slider but plot is the word
(unset variable) plot. In other words Global`plot has no value at the
time the body of the DynamicModule is evaluated. Ditto Global`g.

If you evaluated the cell (the second cell with the DynamicModule) a
second time a plot graphic appears in the list and g[0.] becomes
Cos[0.]. This is not surprising because Global`plot and Global`g now
have a value.

Wolfram have indicated that this is working as designed -- which is
presumably why this example was sent to me of how this should work. If
that is the case it seems poorly designed because (and it is here I
would like feedback) users would expect initializations to be
evaluated prior to the body of the DynamicModule -- otherwise what is
the point?

I'd appreciate the thoughts of other users about this.

Additionally I have been told by Wolfram that wrapping Dynamic around
the variable(s) that are not being evaluated will trigger the
evaluation. But why would a user want to make plot or g Dynamic? It
seems to me that the entire purpose of having an Initialization is to
evaluate this "one off" variable/functions.

thanks

Mike




  • Prev by Date: Re: Timing graphics in the real world
  • Next by Date: Re: What is the point of having Initializations in DynamicModule and Manipulate?
  • Previous by thread: Re: What is the point of having Initializations in
  • Next by thread: Re: What is the point of having Initializations in DynamicModule and Manipulate?