From: Leo Alekseyev on
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(a)Locator[Dynamic[p], Appearance -> {Large}],
Graphics(a)Text[Grid[{{"a:", a, "; b:", b}}], {-1, 2}]}]]

From: John Fultz on
On Sun, 4 Jul 2010 03:10:13 -0400 (EDT), Leo Alekseyev wrote:
> 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(a)Locator[Dynamic[p], Appearance -> {Large}],
> Graphics(a)Text[Grid[{{"a:", a, "; b:", b}}], {-1, 2}]}]]

(a) Use Row[] instead of Grid[]. If the spacing properties of Grid are what
you're having a problem with, then you'll be much more satisfied with Row.

(b) Don't do this. Dynamic is not a way of delaying evaluation or tying
variable evaluations together. That's what SetDelayed is for. Dynamic is for
displaying results. I've commented on this ad nauseum here...

http://forums.wolfram.com/mathgroup/archive/2009/Feb/msg00424.html

(c) Here's a simple example which demonstrates some basic techniques.

SetAttributes[TracePlot, HoldFirst];
TracePlot[f_, {x_, xfirst_, xrest_}, opts___] :=
DynamicModule[{loc = {Mean[{xrest, xfirst}], 0}},
Column[{
LocatorPane[
Dynamic[loc, (loc = {#[[1]], f /. x -> #[[1]]}) &],
Plot[f, {x, xfirst, xrest}, opts]],
Dynamic[loc]
}]]

TracePlot[Sin[x], {x, 0, 2Pi}]

If you haven't gone all the way through the various examples in the Locator and
LocatorPane documentation, you should. There are other techniques there you may
find interesting.

Sincerely,

John Fultz
jfultz(a)wolfram.com
User Interface Group
Wolfram Research, Inc.

From: David Park on
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(a)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(a)Row[{"a = ", NumberForm[a, {4, 2}]}],
Dynamic(a)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(a)comcast.net
http://home.comcast.net/~djmpark/


From: Leo Alekseyev [mailto:dnquark(a)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(a)Locator[Dynamic[p], Appearance -> {Large}],
Graphics(a)Text[Grid[{{"a:", a, "; b:", b}}], {-1, 2}]}]]



From: Helen Read on
On 7/4/2010 3:10 AM, Leo Alekseyev wrote:
> 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

I use Row for that sort of thing, and use Style to format it however you
like. You can style individual elements or the entire row.

Row[{Style["a: ", Blue, Bold], Style[a, Bold], Style["\nb: ", Red],
Style[b, 14, Italic]}]


Style[Row[{"a: ", a, "\nb: ", b}], 16, Red, Bold]


You can stick the Row right in a PlotLabel if you like.

PlotLabel -> Style[Row[{"a: ", a, "\nb: ", b}], 16, Red, Bold]



I don't have good answers for your questions (b) and (c) and will defer
to others.


--
Helen Read
University of Vermont