RE: RE: how to extract parameter values

• To: mathgroup at smc.vnet.net
• Subject: [mg37373] RE: [mg37316] RE: [mg37310] how to extract parameter values
• From: "DrBob" <drbob at bigfoot.com>
• Date: Fri, 25 Oct 2002 02:48:26 -0400 (EDT)
• Sender: owner-wri-mathgroup at wolfram.com

```I was wrong, perhaps, about whether the extra effort is worthwhile.
Both my earlier solutions return t values that aren't in order, because
of inaccuracy.  The first has consecutive values out of order about 14%
of the time, while the second has them out of order less than 1% of the
time (twice out of 271 values).  Increasing the accuracy goal just a
little eliminates that problem (if it IS a problem).  Dave noted that
putting t-values in a list as the Plot is built returns t-values that
aren't entirely in order because of "backtracking" (but I really don't
understand that).

I suppose we could simply Sort the t values, but that brings us back to
Dave's question of what they're needed for in the first place.  One
possibility is that we want to take advantage of ParametricPlot's
adaptive algorithm for choosing plot points, so that we can replace the
original f and g functions via Interpolation later.  In that case, the
values could be sorted first, or consecutive values that are out of
order could be replaced by their average, as points that close together
might not be needed.

a = {0.5, 0.25, 0.3}; b = {0.125, 0.35, 0.03};
{tmin, tmax} = {0, 2*Pi};
f[t_] := a . {Sin[t], Sin[2*t], Sin[3*t]};
g[t_] := b . {Cos[t], Cos[2*t], Cos[3*t]}
h[t_, {x_, y_}] = (f[t] - x)^2 + (g[t] - y)^2;
j[t_, {x_, y_}] = Simplify[D[h[t, {x, y}], t]];
nextGuess := guess.{1, -3, 3}
i[t_, {x_, y_}] := (guess = Join[Rest@guess, {t}] /.
FindRoot[h[t, {x, y}], {t, nextGuess}, Jacobian -> {{j[t, {x,
y}]}},
AccuracyGoal -> 7])
Timing[plot = ParametricPlot[{f[t], g[t]}, {t, tmin, tmax}];
list = Cases[plot, {x_, y_}, Infinity];
guess = tmin{1, 1, 1};
tvals2 = Last@i[t, #] & /@ list;
]
Select[Rest@# - Drop[#, -1], # < 0 &] &[tvals2]
100Length@%/Length@tvals2 "%" // N

DrBob

-----Original Message-----
From: DrBob [mailto:drbob at bigfoot.com]
To: mathgroup at smc.vnet.net
Subject: [mg37373] RE: [mg37316] RE: [mg37310] how to extract parameter values

Here's a method that's not blindingly fast, but still interesting:

a = {0.5, 0.25, 0.3};   b = {0.125, 0.35, 0.03};
{tmin, tmax} = {0, 2*Pi};
f[t_] := a . {Sin[t], Sin[2*t],      Sin[3*t]};
g[t_] := b . {Cos[t], Cos[2*t],     Cos[3*t]}
h[t_, {x_, y_}] = (f[t] - x)^2 +     (g[t] - y)^2;
j[t_, {x_, y_}] = Simplify[    D[h[t, {x, y}], t]];
i[t_, {x_, y_}] := guess=2*t - guess /. FindRoot[h[t,{x, y}], {t,
guess},Jacobian -> {{j[t, {x, y}]}}]
Timing[plot = ParametricPlot[{f[t], g[t]}, {t, tmin, tmax}];
list = Cases[plot, {x_, y_}, Infinity]; guess = tmin;
tvals = (i[t, #1] & ) /@ list; ]
ListPlot[tvals]

{0.281 Second, Null}

This is slightly faster, though probably not worth the extra effort:

nextGuess := guess.{1, -3, 3} (* quadratic extrapolation from previous
guesses *)
i[t_, {x_, y_}] := (guess = Join[Rest@guess, {t}] /.
FindRoot[h[t, {x, y}], {t, nextGuess}, Jacobian -> {{j[t, {x,
y}]}}])
Timing[plot = ParametricPlot[{f[t], g[t]}, {t, tmin, tmax}];
list = Cases[plot, {x_, y_}, Infinity];
guess = tmin{1, 1, 1};
tvals2 = Last@i[t, #] & /@ list;
]

{0.266 Second, Null}

DrBob

-----Original Message-----
From: Wolf, Hartmut [mailto:Hartmut.Wolf at t-systems.com]
To: mathgroup at smc.vnet.net
Subject: [mg37373] [mg37316] RE: [mg37310] how to extract parameter values

>-----Original Message-----
>From: Jan Mangaldan [mailto:jajem at yahoo.com]
To: mathgroup at smc.vnet.net
>Sent: Tuesday, October 22, 2002 10:49 AM
>To: mathgroup at smc.vnet.net
>Subject: [mg37373] [mg37316] [mg37310] how to extract parameter values
>
>
>I'd like to know how I can extract the parameter
>values used by ParametricPlot in plotting a curve,
>e.g. if ParametricPlot used points corresponding to
>parameter values 2, 4, 6, etc... I need a way to get
>the list {2, 4, 6, ...}.
>
>One solution I know is this:
>
>list={};
>ParametricPlot[(AppendTo[list,t]; {f[t]
>g[t]}),{t,tmin,tmax}];
>
>The problem with this is that I get an error that
>ParametricPlot cannot compile the function, and that
>the plotting time is considerably slowed down.
>especially if I have a high PlotPoints setting.
>
>Any ideas?
>
>                                   Jan Mangaldan (~_~)

Jan,

In[1]:= a = {0.5, 0.25, 0.3}; b = {0.125, 0.35, 0.03};
In[2]:=
f[t_] := a . {Sin[t], Sin[2*t], Sin[3*t]};
g[t_] := b . {Cos[t], Cos[2*t], Cos[3*t]}

In[4]:= {tmin, tmax} = {0, 2*Pi};
In[5]:=
list = {}; s = {f[t], g[t]};
Timing[ParametricPlot[list = {list, t}; s,
{t, tmin, tmax}]]

ParametricPlot::"ppcom": "Function \!\(\(\(list = \(\({list, t}\)\)\)\)
;
s\) \
cannot be compiled; plotting will proceed with the uncompiled function."
Out[6]= {0.231 Second, - Graphics -}

In[7]:= tt = Flatten[list];
Length[tt]
ss = -Apply[Subtract, Partition[tt, 2, 1], {1}];
Out[8]= 271

In[10]:= ListPlot[tt]
In[11]:= ListPlot[ss, PlotJoined -> True, PlotRange -> All]

In[12]:= Timing[ParametricPlot[{f[t], g[t]}, {t, tmin, tmax}]]
Out[12]= {0.25 Second, - Graphics -}

You see the uncompiled version, including registering of the t-samples
is
faster than the "compiled version"?

In[13]:=
Timing[ParametricPlot[Evaluate[{f[t], g[t]}], {t, tmin, tmax}]]
Out[13]= {0.04 Second, - Graphics -}

In[14]:=
list = {}; s = Compile[t,{f[t], g[t]}];
Timing[ParametricPlot[list = {list, t}; s[t],
{t, tmin, tmax}];]
Out[15]= {0.32 Second, Null}

In[16]:=
list = {}; s = Compile[t,Evaluate[{f[t], g[t]}]];
Timing[ParametricPlot[list = {list, t}; s[t],
{t, tmin, tmax}];]
Out[17]= {0.1 Second, Null}

--
Hartmut Wolf

```

• Prev by Date: Re: help on bootstrap sample
• Next by Date: Cylindrical coords in PlotVectorField3D
• Previous by thread: RE: RE: how to extract parameter values
• Next by thread: magnify Help Browser