5
$\begingroup$

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.

$\endgroup$
2
  • $\begingroup$ What is length? $\endgroup$ Commented Oct 16, 2016 at 1:30
  • $\begingroup$ Closely related, possible duplicate: Animation of double pendulum $\endgroup$ Commented Oct 16, 2016 at 3:00

1 Answer 1

8
$\begingroup$

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

$\endgroup$
1
  • $\begingroup$ 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. $\endgroup$ Commented Oct 16, 2016 at 14:08

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.