7
$\begingroup$

I almost remember an example that has a moving red point on a given curve, which will moving time and time again, with no controls...

now, here is my question: given a curve by

u[y_] := y^4 - 4 y^2 + 3
v[y_] := 2 y - y^3
curv=ParametricPlot[{u[y], v[y]}, {y, -2, 2}]

then, how to attach a "big red" moving point on it, which are moving again and again?

$\endgroup$
1
  • $\begingroup$ How do we decide which curve to put the point on at say x=0? $\endgroup$
    – JohnD
    Nov 16, 2012 at 12:46

4 Answers 4

7
$\begingroup$

You can use Animate

u[y_] := y^4 - 4 y^2 + 3
v[y_] := 2 y - y^3
curv = ParametricPlot[{u[y], v[y]}, {y, -2, 2}];
Animate[
 Show[
  curv,
  Graphics[{PointSize[Large], Red, Point[Dynamic[{u[t], v[t]}]]}]
  ]
 , {t, -2, 2,AppearanceElements->None}]

Animate None

$\endgroup$
5
  • $\begingroup$ can I make it without controls? $\endgroup$
    – van abel
    Nov 16, 2012 at 12:51
  • $\begingroup$ @van, you could use Table[] in place of Animate[] and export as a GIF file... $\endgroup$ Nov 16, 2012 at 12:58
  • $\begingroup$ @vanabel Try Animate[Plot[Sin[x + a], {x, 0, 10}], {a, 0, 5, AppearanceElements -> None}] $\endgroup$
    – Sasha
    Nov 16, 2012 at 13:01
  • $\begingroup$ I really think you should review your last solution, what you are producing is the product of an invisible element and a graph, which is perfectly valid in Mathematica, but definitely not what someone reading the code expects. When you want to tie a dynamic expression to a different display form, you should properly use DynamicWrapper. $\endgroup$
    – jVincent
    Nov 16, 2012 at 15:15
  • $\begingroup$ @jVincent, Yes I'll make it more clear :) $\endgroup$
    – ssch
    Nov 16, 2012 at 15:51
6
$\begingroup$

A potential way of doing this is by using DynamicModule to keep a scoped variable time, and use Dynamic wrapper to keep it updated:

 curve

 DynamicModule[{t},
   DynamicWrapper[
      Show[curv, Graphics[{PointSize[Large], Red, Point[Dynamic[{u[t], v[t]}]]}]],
   t = Clock[{-2, 2}]
   ]
  ]
$\endgroup$
4
  • $\begingroup$ I like the use of Clock I couldn't figure out how to get it neat like that :) $\endgroup$
    – ssch
    Nov 16, 2012 at 17:00
  • $\begingroup$ Any way with Dynamic and Clock to do that yet have the point traverse the curve just once? Just as AnimationRepetitions -> 1 would do inside an Animate? $\endgroup$
    – murray
    Nov 16, 2012 at 18:54
  • $\begingroup$ @murray Have a look at the documentation for Clock it has a quite useful span of usages. to set it to only run through the animation once in this case, simply use Clock[{-2, 2}, 4, 1]. $\endgroup$
    – jVincent
    Nov 16, 2012 at 19:02
  • $\begingroup$ @jVincent: Yes, I see that Clock allows running just once. It's just that I've always been annoyed that Animate, and the Manipulate animation control, produce a loop. $\endgroup$
    – murray
    Nov 17, 2012 at 15:35
4
$\begingroup$

You don't want to have controls? How about using timed tasks?

p=0;
t = CreateScheduledTask[p = Mod[p + 0.1, 4, -2], 0.1];

Show[
 curv,
 Graphics[{Red, PointSize[0.03], Dynamic@Point@{u[p], v[p]}}]
 ]

StartScheduledTask[t];

Stop it using:

RemoveScheduledTask[t];

Alternatively, you could use Refresh to take care of the timing.

Show[
 curv,
 Graphics[{Red, PointSize[0.03], 
   Dynamic[Refresh[p = Mod[p + 0.05, 4, -2]; Point@{u[p], v[p]}, 
     UpdateInterval -> 0.05, TrackedSymbols -> {}]]}]
 ]

Note the TrackedSymbols -> {} option which prevents the p = Mod[p + 0.05, 4, -2] part from self-triggering another iteration.


It's even easier using Clock:

Show[curv, 
 Graphics[{Red, PointSize[0.03], 
   Dynamic[Point@{u[#], v[#]} &@Clock[{-2, 2, .05}, 5]]}]]
$\endgroup$
3
$\begingroup$

Another way to use Clock: with a Mesh + MeshFunctions combination:

 Dynamic @ ParametricPlot[{u[x], v[x]}, {x, -2, 2},
  MeshFunctions -> {#3 &}, 
  Mesh -> {{Clock[{-2, 2}]}}, 
  MeshStyle -> {Red, PointSize[Large]}]

enter image description here

Yet another way: Using ListAnimate:

 ListAnimate[Table[Show[ParametricPlot[{u[x], v[x]}, {x, -2, 2}],
    Graphics[{PointSize[Large], Red, Point[{u[t], v[t]}]}]],
    {t, -2, 2, .01}], Paneled -> False] /. 
 HoldPattern[AppearanceElements -> _] -> (AppearanceElements -> None)

enter image description here

(the trick to remove AppearanceElements from Vitaly's answer to a related question.)

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.