Re: Tracking point on a plot

*To*: mathgroup at smc.vnet.net*Subject*: [mg76971] Re: [mg76929] Tracking point on a plot*From*: Chris Hill <chill at wolfram.com>*Date*: Thu, 31 May 2007 03:16:48 -0400 (EDT)*References*: <200705300920.FAA13167@smc.vnet.net>

At 04:20 AM 5/30/2007, Murray Eisenberg wrote: >I want a locator on the plot of, say, Sin[x], that will move only >along the graph of the function. How should this be done? > >The closest I've come so far is the following, where the x-coordinate >is being read from wherever the mouse is, but the corresponding >{x,Sin[x]} point on the curve is where the locator appears: > > DynamicModule[{p=0}, > LocatorPane[{Dynamic[p],Dynamic[Sin[p]]}, > Plot[Sin[x],{x,0,10}] > ] > ] > >(Also, onece the expression is evaluated, each time I move the mouse I >get a Set::write message about Tag Sin in Sin[...] being protected.) > >-- > Murray Eisenberg Internet: murray at math.umass.edu > Mathematics & Statistics Dept. Voice: 413-545-2859 (W) > University of Massachusetts 413-549-1020 (H) > Amherst, MA 01003 Fax: 413-545-1801 As is mentioned in the Introduction to Dynamic tutorial (tutorial/IntroductionToDynamic) in the section "The Second Argument of Dynamic", the second argument to Dynamic is useful for implementing constraints. The messages caused when moving the locator in your example happen when Mathematica is trying to make an assignment to Sin[p] which it doesn't know how to do. Using the second argument to Dynamic, you can tell it. In your particular case, the y coordinate is a function of p. p is being updated appropriately because it appears inside Dynamic as the x coordinate. This means you do not need to make any assignment to p for the y coordinate: DynamicModule[{p = 0}, LocatorPane[{Dynamic[p], Dynamic[Sin[p], Identity]}, Plot[Sin[x], {x, 0, 10}]]] Perhaps you'd also like to keep p within the plot interval: With[{xmin = 0, xmax = 10}, DynamicModule[{p = 0}, LocatorPane[{Dynamic[p, (p = Clip[#, {xmin, xmax}]) &], Dynamic[Sin[p], Identity]}, Plot[Sin[x], {x, xmin, xmax}]]]] Chris Hill Wolfram Research

**References**:**Tracking point on a plot***From:*Murray Eisenberg <murray@math.umass.edu>