Re: Display/manipulating numeric values inside a DynamicModule[]

*To*: mathgroup at smc.vnet.net*Subject*: [mg110765] Re: Display/manipulating numeric values inside a DynamicModule[]*From*: "David Park" <djmpark at comcast.net>*Date*: Mon, 5 Jul 2010 06:02:59 -0400 (EDT)

I know that Manipulate is versatile and popular but I usually find it easier to design custom dynamics without it and use Presentations to control the drawing of various objects. So here is how I would do your example: Needs["Presentations`Master`"] DynamicModule[ {p = {1, 1}, (* Dependent dynamic variables *) a, b, x, f, calcAll}, f[x_] := x^2 + I; calcAll[loc_] := (x = loc[[1]]; a = f[x]; b = Re[a]); (* Initialize *) calcAll[p]; Column[ {Draw2D[ {(* Static curve *) Draw[Re@f[t], {t, -2, 2}], (* Dynamic circle that moves with the locator *) Dynamic@Circle[p, .1 + Sqrt[b/8]], (* Locator *) Locator[Dynamic[p, (p = #; calcAll[p]) &], CirclePointLocator[3, Blue]]}, Frame -> True, PlotRange -> {{-2, 2}, {0, 4}}, PlotRangePadding -> .1, ImageSize -> 300], (* Dynamic data presented outside the plot *) Dynamic@Row[{"a = ", NumberForm[a, {4, 2}]}], Dynamic@Row[{"b = ", NumberForm[b, {4, 2}]}] }] ] The trick is to use the second argument of Dynamic that calls a calcAll routine that in turn calculates all the dependent dynamic variables in your diagram. Then you can draw other items that depend on the primary and dependent variables, and dynamically display calculated values that again depend on both the primary and dependent variables. In the construction above CirclePointLocator provides a small outlined colored disk as a locator, which I think looks a lot better that the militaristic gun sight. I displayed the calculated values outside of the graphic, but they could have been displayed with a Text statement within the graphic. The Circle is a construction that depends on both the primary dynamic variable, p, and a dependent dynamic variable b. Since we are working with graphics primitives, it is easy to just draw one item after another and control which are static and which are dynamic. David Park djmpark at comcast.net http://home.comcast.net/~djmpark/ From: Leo Alekseyev [mailto:dnquark at gmail.com] Recently, I've been using DynamicModules[] to interactively explore some plots that I make -- by introducing a locator, whose coordinates allow me to trace the plotted data in some fashion (e.g. by displaying the function value for the x coordinate of the locator, or finding the closest plotted point to a locator in a ListPlot, etc.) My problem is that I haven't figured out a good way to display dynamically updated values as part of the plot or, for that matter, perform manipulations with the dynamic values. The reason seems to be that once an expression has head Dynamic, the behavior of many familiar functions changes (e.g. NumericQ on a dynamic value returns False, which makes it impossible to numerically evaluate Re or Im, etc.) Below is a simple example of what I'm doing, and workaround that I came up with. Here are some concrete questions: (a) is there a better way to display the dynamic values "a" and "b" inside a formatted string?.. My workaround is to use something like Grid[{{"a:", a, "; b:", b}}], which is not entirely satisfactory (b) is there a better way of doing arithmetic with the dynamic value "a"? I would like to be able to say something like b = Re[a], as opposed to b = Dynamic[Re[whatever long expression I might have used to compute a]] (c) are there any good examples of using Dynamic to trace plots, as described above?.. I'm sure I'm not the only one who is doing this :) Clear[f]; f[x_] := x^2 + I; DynamicModule[{p = {1, 1}, a, b}, a := Dynamic[f[p[[1]]]]; b := Dynamic[Re@f[p[[1]]]]; Show[{Plot[{Re[f[x]]}, {x, -2, 2}, Frame -> True], Graphics@Locator[Dynamic[p], Appearance -> {Large}], Graphics@Text[Grid[{{"a:", a, "; b:", b}}], {-1, 2}]}]]