# How to make an animation of a body when I know its coordinates in time as a table [closed]

I'm trying to numerically solve a system of differential equations describing two pendula connected to each other by a spring, and then make an animation of their evolution in time. What I have now, is a "Table" including their Cartesian coordinates at successive instances.

How can I show them in a plane? And how can I animate it?

• example of data? – AccidentalFourierTransform Jan 21 at 22:09
• What do you mean? You need an example of my data? – N.S. Jan 21 at 22:26
• yes.${}{}{}{}{}$ – AccidentalFourierTransform Jan 21 at 23:18
• Make a list of successive graphics and use ListAnimate. – Goofy Jan 22 at 0:32
• @AccidentalFourierTransform : Since I'm using Euler method or possibly something else to solve the system of equations, I come up with a matrix or "Table." The "Table" is n×8, with n being the number of steps and 8 as the number of first-order equations built from the system of four second-order equations. So, each column of this "Table" is one of the coordinates of the problem or their derivatives with respect to time. – N.S. Jan 22 at 17:10

I'm trying to numerically solve a system of differential equations describing two pendula connected to each other by a spring, and then make an animation of their evolution in time

Here is something to get you started. You can do all this in Mathematica directly by solving the equations of motion and then use NDSolve to solve them and then do the animation. This below gives you everything you need, but I did not do the actually animation itself. This will take a little more time. May be later when I have more time will add 2D graphics and Manipulate. I made this small diagram to help derive the Lagrangian.

The only thing to do is find the Lagrangian, then Mathematica will do the rest of the algebra.

Assuming both springs have the same relaxed length of $$L$$. Starting by finding the Lagrangian $$\mathcal{L} =T-V$$. For $$m_{1}$$ \begin{align*} T_{1} & =\frac{1}{2}m_{1}\left( \dot{x}_{1}^{2}+\left( \left( L+x_{1}\right) \dot{\theta}_{1}\right) ^{2}\right) \\ V_{1} & =-m_{1}g\left( L+x_{1}\right) \cos\theta_{1}+\frac{1}{2}k_{1} x_{1}^{2} \end{align*} And for $$m_{2}$$ \begin{align*} T_{2} & =\frac{1}{2}m_{2}\left( \left( \dot{x}_{2}+\dot{x}_{1}\cos\left( \theta_{1}-\theta_{2}\right) \right) ^{2}+\left( \dot{x}_{1}\sin\left( \theta_{1}-\theta_{2}\right) \right) ^{2}\right) \\ & +\frac{1}{2}m_{2}\left( \left( \left( L+x_{2}\right) \dot{\theta} _{2}+\left( L+x_{1}\right) \dot{\theta}_{1}\cos\left( \theta_{1}-\theta _{2}\right) \right) ^{2}+\left( \left( L+x_{1}\right) \dot{\theta} _{1}\sin\left( \theta_{1}-\theta_{2}\right) \right) ^{2}\right) \\ V_{2} & =-m_{2}g\left( \left( L+x_{1}\right) \cos\theta_{1}+\left( L+x_{2}\right) \cos\theta_{2}\right) +\frac{1}{2}k_{2}x_{2}^{2} \end{align*} Hence $$\mathcal{L} =\left( T_{1}+T_{2}\right) -\left( V_{1}+V_{2}\right)$$ There are 4 generalized coordinates, $$x_{1},x_{2},\theta_{1},\theta_{2}$$. Now Mathematica is used to obtain the four equations of motion to help with the algebra. Once $$x_{1},x_{2},\theta_{1},\theta_{2}$$ are solved for, the position of each mass $$m_{1},m_{2}$$ is fully known at each time instance, and each mass motion can be animated. The four equations of motion are

\begin{align*} \frac{d}{dt}\left( \frac{\partial \mathcal{L} }{\partial\dot{x}_{1}}\right) -\frac{\partial \mathcal{L} }{\partial x_{1}} & =0\\ \frac{d}{dt}\left( \frac{\partial \mathcal{L} }{\partial\dot{x}_{2}}\right) -\frac{\partial \mathcal{L} }{\partial x_{2}} & =0\\ \frac{d}{dt}\left( \frac{\partial \mathcal{L} }{\partial\dot{\theta}_{1}}\right) -\frac{\partial \mathcal{L} }{\partial\theta_{1}} & =0\\ \frac{d}{dt}\left( \frac{\partial \mathcal{L} }{\partial\dot{\theta}_{2}}\right) -\frac{\partial \mathcal{L} }{\partial\theta_{2}} & =0 \end{align*}

Now we jump to Mathematica for help and ask it to solve the equations and make some plots.

ClearAll[x1, x2, θ1, θ2, t,m1,m2,L]
T1 = 1/2 m1 (x1'[t]^2 + ((L + x1[t]) θ1'[t])^2);
V1 = -m1 g (L + x1[t]) Cos[θ1[t]] + 1/2 k1 x1[t]^2;
T2 = 1/2 m2 ((x2'[t] + x1'[t] Cos[θ1[t] - θ2[t]])^2 +
(x1'[t] Sin[θ1[t] - θ2[t]])^2) + 1/2 m2 (((L + x2[t]) θ2'[t]
+ ((L + x1[t]) θ1'[t] Cos[θ1[t] - θ2[t]]))^2
+ ((L + x1[t]) θ1'[t] Sin[θ1[t] - θ2[t]])^2);
V2 = -m2 g ((L + x1[t]) Cos[θ1[t]] + (L + x2[t]) Cos[θ2[t]]) + 1/2 k2 x2[t]^2;


Find the Lagrangian and equations of motion

(lag = (T1 + T2) - (V1 + V2)) // Simplify


eq1 = D[D[lag, x1'[t]], t] - D[lag, x1[t]] == 0;
eq2 = D[D[lag, x2'[t]], t] - D[lag, x2[t]] == 0;
eq3 = D[D[lag,θ1'[t]],t]-D[lag,θ1[t]]==0;
eq4 = D[D[lag,θ2'[t]],t]-D[lag,θ2[t]]==0;


Numerically solve the equations of motion, using some values for spring constants and masses. (these can be changed in animation)

pars={L->1,m1->1,m2->2,g->9.8,k1->10,k2->30};
ic={θ1[0]==5 Degree,θ1'[0]==0,θ2[0]==3 Degree, θ2'[0]==0,x1[0]==0,x1'[0]==0,x2[0]==0,x2'[0]==0};
eqs=Flatten[{eq1,eq2,eq3,eq4}]/.pars;
numericalSolution=First@NDSolve[{eqs,ic},{x1,x2,θ1,θ2},{t,0,20}]


That is all. Make some plots

Plot[Evaluate[({θ1[t],θ2[t]}/.numericalSolution)*180/Pi],{t,0,20},
PlotRange->All,AxesLabel->{"time","Angle (in degree)"},
ImageSize->400,PlotLegends->{"mass 1","mass 2"}]


Plot[Evaluate[{x1[t], x2[t]} /. numericalSolution], {t, 0, 20},
PlotRange -> All,
AxesLabel -> {"time", "spring extensions in meters"},
ImageSize -> 400, PlotLegends -> {"mass 1", "mass 2"}]


• I appreciate alot dear Nasser, the solution is perfect, but I'm actually looking for the commands for animation. By the way, I'm supposed to solve it with numerical methods for systems of differential equations and not with "NDSolve." So I have a "Table" of data. – N.S. Jan 22 at 16:51
• The "Table" is n×8, with n being the number of steps and 8 as the number of first-order equations built from the system of four second-order equations. – N.S. Jan 22 at 17:03