MathGroup Archive 2007

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

Search the Archive

Re: Locator question

  • To: mathgroup at smc.vnet.net
  • Subject: [mg79434] Re: Locator question
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
  • Date: Thu, 26 Jul 2007 06:30:49 -0400 (EDT)
  • Organization: The Open University, Milton Keynes, UK
  • References: <f86r55$pc7$1@smc.vnet.net> <200707250930.FAA26363@smc.vnet.net> <f89pve$5jn$1@smc.vnet.net>

Murray Eisenberg wrote:
> I'd like to define a function that would do that, taking as argument the 
> function name, the interval on which to plot, and an optional point at 
> which to place the locator.
> 
> Here are two attempts, each with unpleasant consequences...
> 
> Attempt 1:
> 
>    trackPointOnPlot[f_, {a_, b_}, start_: {a, f[a]}] :=
>     DynamicModule[{p = start},
>      Column[{LocatorPane[Dynamic[p, (p = {First@#, f[First@#]}) &],
>         Plot[Sin[x], {x, a, b}]], Dynamic[p]}]]
> 
> With a third argument supplied, this works OK, e.g.:
> 
>    trackPointOnPlot[Sin, {0, 10}, {Pi, Sin[Pi]}]
> 
> However, if I evaluate, say,
> 
>    trackPointOnPlot[Sin,{0,10}]
> 
> then the locator appears initially to the left of the x-axis. So I ask 
> whether that syntax on the left for trackPointOnPlot is legitimate? That 
> is, can the default value for variable 'start' refer legitimately to the 
> value of a preceding argument?
[snip]

Hi Murray,

The short answer to your question is 'No'. You cannot refer to 
'previous' patterns in your function declaration. The same restriction 
applies to initializations within *Module*, *With*, or *Block* 
constructs. For instance, one cannot expect correct result from, say, 
Module[{a = 1, b = 2, c = a + b}, somecode]. (Note that if a and/or b 
have also some global values, this is these values that are going to be 
taken at this stage for computing c.) A valid expression would be
Module{{a = 1, b = 2, c}, c = a + b; somecode], expression that 
guarantee that c is equal to 3. (There is a discussion about that in the 
Mathematica Book, Section 2.7 "Modularity and the Naming of Things".)

Back to your function, the following modified version will work as you 
wanted (I hope!).

trackPointOnPlot[f_, {a_, b_}, start_: {0, 0}] :=
  DynamicModule[{p = If[start == {0, 0}, {a, f[a]}, start]},
   Column[{LocatorPane[Dynamic[p, (p = {First@#, f[First@#]}) &],
      Plot[f[x], {x, a, b}]], Dynamic[p]}]]

trackPointOnPlot[Cos, {0, 10}, {Pi, Cos[Pi]}]
trackPointOnPlot[Sin, {0, 10}, {Pi, Sin[Pi]}]
trackPointOnPlot[Cos, {0, 10}]
trackPointOnPlot[Sin, {0, 10}]

Regards,
Jean-Marc


  • Prev by Date: approximation
  • Next by Date: Re: Mathematica to .NET compiler
  • Previous by thread: Re: Locator question
  • Next by thread: Re: Locator question