Using Mathematica Parametric Plots to generate animated gif files with “history tails”

Charlie Freeman, SUNY Geneseo (with very slight modifications by E. Pogo, SUNY Geneseo)

ClearAll["Global`*"]

X[t_]:=Cos[t]
Y[t_]:=Sin[t]Cos[2t]
tmax=2π;

I want to make an animation that shows the position (x, y) of this particle over some time range. Both x and y depend on time, as seen above.
In a "normal" plot, we might plot x(t). But here, both x and y are dependent variables that depend only on time. 

So, I could use the above definitions to easily plot either x(t) or y(t), but not y(x).
To plot y(x), suppose I solve x(t) = cos(t) for t, resulting in t = (cos^(-1))(x)
Then, I can algebraically substitute this t into the definition of y(t) to get a formula for y(x). But, it's not enough to know y(x), because we still want to maintain the time dependence of the motion!

We use a "parametric plot" to show two simultaneous dependent variables (x and y) when they depend on a separate parameter like t. 

Let's consider a reasonable scale for x and y. Since t will range from 0 to (Subscript[t, max])= 2π,
then the particle starts at  x(0) =1 and ends at x((Subscript[t, max])) = 1. Similarly, y(0) = 0 and y ((Subscript[t, max])) = 0.
So, the particle returns to it's starting point during this time period... which is different than saying it never went anywhere at all!
The root of this problem is that y(x) is multi-valued (not a true function).

In the end, I hope to plot  x and y each ranging from -1.2 to +1.2 (so the aspect ratio = 1), so the particle never "hits" the edge of the plot.

Plot[Sin[ArcCos[x]] Cos[2 ArcCos[x]],{x,1.2,-1.2}] (* This plot shows SOME of the positions y(x) that are possible... *)

ParametricPlot[{X[t],Y[t]},{t,0,tmax}](* Here is the more appropriate parametric plot of y(x) *)

dot[t_]:=Graphics[{Red,Disk[{X[t],Y[t]},0.1]}](* This defines an object that is a disk of radius 0.1 at the coordinates X, Y. I can put this into a plot later. *)

dt=1;(*This parameter controls how long the trace is, as a duration.  It can be adjusted for personal preference. If you make it longer than tmax, then the trace would overlap with itself. *)

tail[t_]:=ParametricPlot[{X[tt],Y[tt]},{tt,t-dt,t},PlotRange->{{-1.2,1.2},{-1.2,1.2}},Axes->False,ColorFunction->"Rainbow",PlotStyle->Thickness[0.010]](* The trailing trace is just a small portion of the parametric plot we showed above. The variable tt is a dummy variable. You can see that the "duration" of the trace is from (t-dt) to (t). If we went from (t) to (t+dt), it would instead be a preceding rather than trailing line *)

Manipulate[Show[tail[t],dot[t]],{t,0,tmax}](* Let's test and examine the plot in Mathematica before saving it to an external file. *)
(* The "Show" command combines two plots. In this case, we are combining the objects called "tail" and "dot" that we defined above. Each was defined as a function of time *)

duration=4; (*This is the desired duration of the animation, in seconds. I typically adjust the speed of the Manipulate function above using the up/down arrows until I like the speed and then I count off in my head the desired period in seconds. *)
Nsteps = duration*30; (* This is the number of steps in the animation.  I usually choose 30 fps, so the number of steps is the duration*30. I choose this because it is the standard for digital video. *)

allframes=Table[Show[tail[t],dot[t]],{t,0,tmax*(((Nsteps-1)/Nsteps)),(tmax/Nsteps)}];(*Make a table of Nsteps images.  Sometimes I will temporarily set Nsteps to some low number (like 10) and remove the semicolon to show 10 frames in the animation and ensure that things look right before running the notebook for the large number of steps.  The (Nsteps-1)/Nsteps factor is in there to avoid a "double frame" when the animation is looped. *)

Export["C:\\Users\\pogo\\Desktop\\mma\\tail.gif",allframes,"DisplayDurations"->1/30,"AnimationRepetitions"->∞];(*This makes the animated gif and exports the file to the desktop.  This can take quite a while for complicated animations, so don't perform this action until you're sure everything is perfect! *)