4
$\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

1 Answer 1

7
$\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$
    – Gabu
    Oct 16, 2016 at 14:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.