# How to make a pendulum simulation draw the bob's trajectory?

This is the code for a simple pendulum simulation

g = 9.81;
l = 2;
Theta0 = 15 Pi/180

Animate[Graphics[
List[{Line[{{0, 0},
length {l Sin[T], -l Cos[T]}} /. {T -> Theta0 Cos[Sqrt[g/l] t]}],PointSize[0.1],
Point[{length {l Sin[T], -l Cos[T]}} /. {T ->Theta0 Cos[Sqrt[g/l] t]}]}],
PlotRange -> {{-0.3, 0.3}, {-0.5, 0}}], {t, 0, 5},
AnimationRate -> 1]


Do you know how can I draw the bob's trajectory as it moves? Thank you very much.

• What is length? – anderstood Oct 16 '16 at 1:30
• Closely related, possible duplicate: Animation of double pendulum – Jens Oct 16 '16 at 3:00

This should do what you are looking for:

g = 9.81;
l = 2;
theta0 = 15 Pi/180;
sol[t_] = x[t] /. NDSolve[{x''[t] + Sqrt[g/l] x[t] == 0, x[0] == theta0,
x'[0] == 0}, x, {t, 0, 10}] // First;
Manipulate[
Show[{Graphics[Line[{{0, 0}, {l*Sin[sol[t]], -l*Cos[sol[t]]}}]],
ParametricPlot[{l*Sin[sol[s]], -l*Cos[sol[s]]}, {s, 0., t}]},
PlotRange -> {{-l, l}, {-1.1 l, 0}}], {t, 10^-10, 10}]


The approach is different from yours; here Mathematica solves the differential equation of motion (you can add damping very easily, if needed -- of course the exact solutions are known too). Then the animation is the superposition of the pendulum (Graphics) and the trajectory (ParametricPlot).

• It works perfectly. I'm doing this as practice for a double pendulum so your solution was better than I was hoping for. Thank you very much. – Gabu Oct 16 '16 at 14:08