7

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?

van abel
  • 1,235
  • 1
  • 11
  • 27

4 Answers4

7

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

ssch
  • 16,590
  • 2
  • 53
  • 88
  • can I make it without controls? – van abel Nov 16 '12 at 12:51
  • @van, you could use Table[] in place of Animate[] and export as a GIF file... – J. M.'s missing motivation Nov 16 '12 at 12:58
  • @vanabel Try Animate[Plot[Sin[x + a], {x, 0, 10}], {a, 0, 5, AppearanceElements -> None}] – Sasha Nov 16 '12 at 13:01
  • 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. – jVincent Nov 16 '12 at 15:15
  • @jVincent, Yes I'll make it more clear :) – ssch Nov 16 '12 at 15:51
6

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}]
   ]
  ]
jVincent
  • 14,766
  • 1
  • 42
  • 74
  • I like the use of Clock I couldn't figure out how to get it neat like that :) – ssch Nov 16 '12 at 17:00
  • 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? – murray Nov 16 '12 at 18:54
  • @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]. – jVincent Nov 16 '12 at 19:02
  • @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. – murray Nov 17 '12 at 15:35
4

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]]}]]
Sjoerd C. de Vries
  • 65,815
  • 14
  • 188
  • 323
3

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.)

kglr
  • 394,356
  • 18
  • 477
  • 896