4

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.

Gabu
  • 284
  • 1
  • 7

1 Answers1

7

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

anderstood
  • 14,301
  • 2
  • 29
  • 80
  • 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