Animations in Mathematica

We have used Mathematica to solve some mechanics problems ...

▪▪ projectiles
▪▪ oscillations
▪▪ planetary orbits
▪▪ the simple pendulum

These problems start with one or more differential equations for the time-dependence of the position coordinate. We may solve the differential equations exactly with DSolve or numerically with NDSolve.

When we have a solution, it is interesting to visualize the time dependence  using animation. Mathematica has several commands for this purpose. The easiest to use are Animate and ListAnimate.

(A) Syntax for the command Animate

Animate generates an animation of an expression that depends on a continuous variable u. For our purposes, u is the time t. Here is the “Help!” information:

?Animate

Let’s try an example.

Consider a harmonic oscillator; x(t) = a cos(ω t). 
Let the amplitude of oscillation be a = 1.0; and let the angular frequency be ω = 2π., which corresponds to one cycle per second.

First execute Plot[ Cos[2 π* t] , {t,0,5} ].

Plot[Cos[2π *t],{t,0,5}]

This gives a static plot of x as a function of t.

Now execute Animate[Cos[2π*t],{t,0,5}].

Animate[Cos[t],{t,0,5}]

The animation runs from t = 0 to 5, then returns to t = 0 and repeats. To stop the calculation you need to click the stop button. (The other buttons control the speed faster/slower and the direction forward/backward in time.)

This result isn’t useful -- it just runs through the numerical values of Cos[2π*t] as t runs from 0 to 5. But what we want is a pictorial representation of the motion of the mass at x(t). Make sure the earlier animation is stopped. Then try this ...

Animate[
Graphics[
Disk[{Cos[2Pi*t],0},0.1],
PlotRange->{{-1.2,1.2},{-0.5,0.5}}],
{t,0,5}]

What did we do? We animated “something” for time t = 0 to 5. The expression that we animated (≡“something”) is a graphical display of a disk of radius 0.1, located at position {  cos 2π*t , 0 }, in a coordinate system with -1.2 ≤ x ≤1.2 and -0.5 ≤ y ≤ 0.5.

The running output is an animation of a harmonic oscillator.

One problem with animation is that it uses a lot of CPU time. Therefore, you should not leave animations running; they may slow down the computer, or even crash the computer. When you are finished with an animation, click the stop button. Or to be even safer, delete the Output Cell where the animation is running; as long as you don’t; delete the input Cell, you can always run the animation again.

Stop the animation before continuing.

Options

The command Animate can accept many options. Here is a list of the available options ...

Options[Animate]//TableForm

There are really too many options to look at them all. Here are some interesting  ones ...

({{AnimationRunning->False, the animation does not start immediately;
it will start when you click the start button}, {AnimationRate->r, controls speed of the animation; 
r = time for one unit of t, in seconds}, {Frame->True, draws a frame around the picture}, {FrameLabel->{"x","y"},  horiz and vertical labels}, {LabelStyle->{ ... }, label style}})

So try this ...

Animate[
Graphics[
Disk[{Cos[2Pi*t],0},0.1],
PlotRange->{{-1.2,1.2},{-0.5,0.5}}],
{t,0,5},
AnimationRunning->False,AnimationRate->1,Frame->True,
FrameLabel->{"x","y"},
LabelStyle->{FontFamily->"Courier",FontWeight->"Bold",18}]

(B) Animation of a pendulum

Now that we know how it works, we’ll make an animation of a simple pendulum with length = 1 meter and amplitude of swinging = Pi/2 radians.

First, solve the equations of motion

The position of the bob is { x(t) , y(t) } = { sin θ(t),  – cos θ(t) }  where the angle θ is shown in the picture. Now we need to know this function. The equation of motion is
	(Overscript[θ, ◼◼])(t) = – (ω^2) sin θ(t)	where ω = (Sqrt[g/L]) = (Sqrt[9.81]) (sec^(-1)) = 3.132.
We can solve the differential equation (with initial values θ(0) = 3π/4 radians =  135 degrees, and (Overscript[θ, ◼]) (0) = 0 ) using NDSolve. The result will be an Interpolating Function for θ(t).

Remove["Global`*"]
eqs={f''[t]==-9.81 * Sin[f[t]],f[0]==0.75Pi,f'[0]==0}
θ=NDSolveValue[eqs,f,{t,0,6}]
Plot[θ[t],{t,0,6}]

Then animate the solution

Animate[
Graphics[
Disk[{Sin[θ[t]],-Cos[θ[t]]},0.1],
PlotRange->{{-1.2,1.2},{-1.2,1.2}}],
{t,0,6},
AnimationRunning->False,AnimationRate->1,Frame->True,
FrameLabel->{"x","y"},
LabelStyle->{FontFamily->"Courier",FontWeight->"Bold",18}]

Or, you can make it look a little fancier ...

Animate[
Graphics[{
{Red,Disk[{Sin[θ[t]],-Cos[θ[t]]},0.1]},
{Red,Thickness[0.02],Line[{{0,0},{Sin[θ[t]],-Cos[θ[t]]}}]}
},
PlotRange->{{-1.2,1.2},{-1.2,1.2}}],
{t,0,6},
AnimationRunning->False,AnimationRate->0.3,Frame->True,
FrameLabel->{"x","y"},
LabelStyle->{FontFamily->"Courier",FontWeight->"Bold",18}]

Kinetic energy, potential energy and phase plot of a pendulum

(* first make a static plot of energies versus time *)
KE=1/2*θ'[t]^2
PE=9.81 (1-Cos[θ[t]])
Plot[{KE,PE},{t,0,6},PlotStyle->{Red,Blue}]

(* now make a static phase plot = dθ/dt versus θ *)
pp=ParametricPlot[{θ[t],θ'[t]},{t,0,6},AspectRatio->1,
AxesLabel->{"θ","dθ/dt"},
BaseStyle->{FontFamily->"Courier",FontWeight->"Bold",18}]

(* now animate the phase plot *)
Animate[
Graphics[
Disk[{θ[t],θ'[t]},0.1],
PlotRange->{{-6,6},{-6,6}}],
{t,0,6},
AnimationRunning->False,AnimationRate->0.2,Frame->True,
FrameLabel->{"θ","dθ/dt"},
LabelStyle->{FontFamily->"Courier",FontWeight->"Bold",18}]

(C) Syntax for the command ListAnimate

There is another way to make animations, using ListAnimate.

Here is the “Help!” information on ListAnimate ...

?ListAnimate

Note the difference compared to Animate.
▪ Animate animates an expression that varies continuously with a variable u (such as time)
▪ ListAnimate animates a discrete list of expressions, labeled by an integer j.

So, to use ListAnimate we start by creating a list of expressions,
listtoanimate = { expr[1], expr[2], expr[3], expr[4], ... expr[j], ... exprN }
For example, listtoanimate could be a list of 100 graphs. Each graph in the list differs slightly from the one before it. Then ListAnimate shows the 100 graphs sequentially, and we get an animation.   (That is how a cartoonist makes a cartoon.)

Template for a list animation program...
/1/ Use a do loop to make a bunch of figures;
/2/ Use the Table command to put those figures in a list;
/3/ Apply ListAnimate to the list.

Try this ...

Remove["Global`*"]
{δt,v0x,v0y}={0.1,30.0,50.0};
p=ParametricPlot[{v0x*t,v0y*t-1/2*9.81*t^2},{t,0,7},
PlotRange->{{0,200},{0,100}}];
Do[
t=δt*j; x=v0x*t; y=v0y*t-1/2*9.81*t^2;
fig[j]=Show[p,Graphics[{Red,Disk[{x,y},4]},
PlotRange->{{0,200},{0,100}},Frame->True]];
Null,{j,0,60}]
tableoffigs=Table[fig[j],{j,0,60}];
ListAnimate[tableoffigs,AnimationRunning->False]

(D) The command Manipulate

Manipulate is similar to Animate, but instead of a running animation, we get a slider bar  for the time. In other words,  with Animate we don’t control the time; it just advances at a constant rate. But with Manipulate we control the time with the slider bar. Some options work with both Animate and Manipulate,  but some only work with one of the commands.

Apply Manipulate to the Pendulum Problem

(* first solve the equations of motion *)
Remove["Global`*"];
eqs={f''[t]==-9.81 * Sin[f[t]],f[0]==0.75Pi,f'[0]==0};
θ=NDSolveValue[eqs,f,{t,0,3}];

(* then animate with a time slider *)
Manipulate[
Graphics[{
{Text[StringJoin["t = ",ToString[t]],{0,1.0},{0,0}]},
{Red,Disk[{Sin[θ[t]],-Cos[θ[t]]},0.1]},
{Red,Thickness[0.02],Line[{{0,0},{Sin[θ[t]],-Cos[θ[t]]}}]}
},
PlotRange->{{-1.2,1.2},{-1.2,1.2}}],
{t,0,3,0.3},
FrameLabel->{"x","y"},
LabelStyle->{FontFamily->"Courier",FontWeight->"Bold",18}]

(E) Celestial Mechanics

The parametric equations for a planet in orbit around the sun are

list={{"Quantity", "parametric expression"},
{"t","T/(2π)(ψ–e sin ψ)"},
{"r","a ( 1 – e cos ψ) "},
{"tan(ϕ/2)"," (Sqrt[((1 + e)/(1 - e))]) tan(ψ/2)"},
{"x","a (cos ψ - e)"},
{"y","a (Sqrt[1–(e^2)]) sin ψ"}};
Style[Grid[list,Frame->All],FontFamily->"Futura",FontSize->18]

where a = semimajor axis, e = eccentricity, T = period = (a^(3/2)) in solar system units.

Example. For Mars, a = 1.52 AU and e = 0.0934 and T = 1.87 y. Make an animation of Mars in orbit around the Sun.

Here is the difficulty. We want to animate with respect to time t, not with respect to the Euler parameter  ψ. So we need to know ψ as a function of t. Unfortunately, we know t as a function of ψ. And the equation is a transcendental equation! So there is no simple formula for t as a function of ψ.  To proceed, we’ll construct an interpolating function.

(* step 1 : create an interpolation function for ψ as a function of t *)
Remove["Global`*"];
am=1.52;em=0.0934; Tm=am^1.5
Do[psi[j]=0.01*(4*Pi)*j;
time[j]=Tm/(2*Pi)*(psi[j]-em*Sin[psi[j]]),
{j,0,100}]
{time[0],time[50],time[100]}
tbl=Table[{time[j],psi[j]},{j,0,100}];
p1=ListPlot[tbl,Frame->True]
ψm=Interpolation[tbl]
p2=Plot[ψm[t],{t,0,4.0},Frame->True,PlotStyle->Red]
Show[p1,p2]

(* step 2 : do the animation *)
Remove[α];
xm[α_]:=am*(Cos[α]-em)
ym[α_]:=am*Sqrt[1-em^2]*Sin[α]
p=ParametricPlot[{xm[α],ym[α]},{α,0,2Pi}];
Animate[
Show[p,Graphics[{Red,Disk[{xm[ψm[t]],ym[ψm[t]]},0.1]}],
PlotRange->{{-2,2},{-2,2}}],
{t,0,4.0},AnimationRunning->False,AnimationRate->0.1]

(* step 1 : create an interpolation function for ψ as a function of t *)
ae=1.00;ee=0.0167; Te=ae^1.5
Do[psi[j]=0.01*(4*Pi)*j;
time[j]=Te/(2*Pi)*(psi[j]-ee*Sin[psi[j]]),
{j,0,100}]
{time[0],time[50],time[100]}
tbl=Table[{time[j],psi[j]},{j,0,100}];
p1=ListPlot[tbl,Frame->True]
ψe=Interpolation[tbl]
p2=Plot[ψe[t],{t,0,4.0},Frame->True,PlotStyle->Red]
Show[p1,p2]

(* step 2 : do the animation *)
em
{am*(1-em),am*(-1-em),-am*em}
0.142+1.378
1.662-0.142

Remove[α];
xe[α_]:=ae*(Cos[α]-ee)
ye[α_]:=ae*Sqrt[1-ee^2]*Sin[α]
xm[α_]:=am*(Cos[α]-em)
ym[α_]:=am*Sqrt[1-em^2]*Sin[α]
Animate[  
Graphics[{
{Red,Circle[{-0.142,0},{1.52,1.52}]},
{Blue,Circle[{0,0},{1,1}]},
{Red,Disk[{xm[ψm[t]],ym[ψm[t]]},0.1]},
{Blue,Disk[{xe[ψe[t]],ye[ψe[t]]},0.1]}},
PlotRange->{{-2,2},{-2,2}}],
{t,0,4.0},
AnimationRunning->False,AnimationRate->0.1]

?Circle

?Translate