Re: Display/manipulating numeric values inside a DynamicModule[]
- To: mathgroup at smc.vnet.net
- Subject: [mg110762] Re: Display/manipulating numeric values inside a DynamicModule[]
- From: John Fultz <jfultz at wolfram.com>
- Date: Mon, 5 Jul 2010 06:02:25 -0400 (EDT)
- Reply-to: jfultz at wolfram.com
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@Locator[Dynamic[p], Appearance -> {Large}], > Graphics@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 at wolfram.com User Interface Group Wolfram Research, Inc.