MathGroup Archive 2007

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

Search the Archive

Re: Using Locators in Mathematica v6

  • To: mathgroup at smc.vnet.net
  • Subject: [mg76546] Re: [mg76443] Using Locators in Mathematica v6
  • From: DrMajorBob <drmajorbob at bigfoot.com>
  • Date: Wed, 23 May 2007 05:45:27 -0400 (EDT)
  • References: <8414877.1179846748964.JavaMail.root@m35>
  • Reply-to: drmajorbob at bigfoot.com

This is my first try with Dynamic and all that, so it's far more clumsy =
 =

than necessary, I'm sure.

But here goes:

(* a Plot routine *)
Clear[fit]
fit[drop_Integer?NonNegative, data_List] :=
  Block[{x, err, f, g, pruned, min = Min@data, max = Max@data},
   err@{a_, b_} := Abs[f[a] - b];
   f[x_] = Fit[data, {1, x}, x];
   pruned = Drop[SortBy[data, err], -drop];
   g[x_] = Fit[pruned, {1, x}, x];
   Show[Graphics@Text["dropped outliers: " <> ToString[drop], {4, 4}],
    Graphics@AbsolutePointSize[3], ListPlot@pruned,
    Plot[f[x], {x, min, max}],
    Plot[g[x], {x, min, max}, PlotStyle -> {Red, Dashed}],
    ImageSize -> 300, AspectRatio -> 1]
   ]

(* test data *)
data = Array[{0, 1} RandomReal[NormalDistribution[0, .5]] + {1, -1}
       RandomReal[NormalDistribution[0, 1]] &, {30}];
outliers =
   Array[{0, 1} RandomReal[NormalDistribution[0, 2.5]] + {1, -1}
       RandomReal[NormalDistribution[0, 1]] &, {7}];
all = Join[data, outliers];

(*
DynamicModule[{d = 0},
  Column[{Slider[Dynamic[d], {0, Length[all] - 3, 1}],
    Dynamic[Graphics[ControlActive[fit[d, all], fit[d, all]]],
     SynchronousUpdating -> Automatic]}]]

Bobby

On Tue, 22 May 2007 01:53:54 -0500, Coleman, Mark  =

<Mark.Coleman at LibertyMutual.com> wrote:

> Greetings,
>
> I've been exploring some of the new dynamic interface elements in  =

> Mathematica v6
> and I must say they are very impressive indeed. I've managed to set-up=

> some useful examples using Manipulate. Unfortunately the documentation=

> of
>
> One example I am working on involves the use of Locators. The example
> itself seem straightforward but I cannot quite get the effect I am
> looking for.  I am hoping someone on MathGroup can point me in the rig=
ht
> direction.
>
> Briefly, I want to illustrate the effects that outlier points an have =
on
> a line of best fit. For my example, I generate a small random set of
> points (x(i), y(i)), where y(i) = a + b x(i) + randomerror(i), for
> values a and b. I then calculate the line of best fit using Fit[ ]. I
> next ListPlot[ ] the underlying set of points and overlay the resultin=
g
> line from Fit[ ]. So far very simple.
>
> In my dynamic example, I'd like to have a locator button appear on the=

> graph such that the (x,y) location of the locator becomes an additiona=
l
> data point in the overall data set, and thereafter a new line of best
> fit is calcuated and displayed. Thus as the user moves the locator, a
> new best fit line is displayed.
>
> I'd appreciate any assistance other readers might offer.
>
> Thanks,
>
> -Mark
>



-- =

DrMajorBob at bigfoot.com


  • Prev by Date: Re: [Mathematica 6]3 lines of code which crashes Mathematica every time
  • Next by Date: Re: Re: Re: change $UserDocumentsDirectory
  • Previous by thread: Re: Using Locators in Mathematica v6
  • Next by thread: Re: Re: Using Locators in Mathematica v6