6
$\begingroup$

One end of a thread of length l = 1m is fixed, and a body of m = 1 kg is attached to the other end. The body is displaced counterclockwise by 30 degrees and then released. Plot the resulting Phi(t) function on the interval t=[0,10], then create an animation in which you animate the time on the interval t=[0,10] and in which a pendulum moves according to Phi(t) function. The solution requires a numerical Cauchy problem solution.

Here is my take on it:

l = 1;
m = 1;
fi0 = Pi/6;
g = 9.81;

pendulum[fi_] := {Line[{{0, 0}, {Cos[fi - Pi/2], 
      Sin[fi - Pi/2]}}], {Red, 
    Disk[{Cos[fi - Pi/2], Sin[fi - Pi/2]}, 0.1]}};

inga = NDSolveValue[{fi''[t] == (-g/l)*Sin[fi[t]], fi[0] == fi0, 
   fi'[0] == 0}, fi, {t, 0, 10}];

ParametricPlot[{t, inga[t]}, {t, 0, 10}]

enter image description here

Animate[Show[ParametricPlot[{t, inga[t]}, {t, 0, 10}], 
  Graphics[pendulum[t]]], {t, 0, 10}]
$\endgroup$
2
  • $\begingroup$ What exactly is your question? Are you asking if your solution is correct? The animation does not seem to be, as you probably want to draw Graphics[pendulum[inga[t]]]. $\endgroup$
    – Domen
    Dec 5 '21 at 18:41
  • $\begingroup$ I don't know if my solution is correct and i think that the animation has something wrong in it $\endgroup$
    – Angry
    Dec 5 '21 at 18:44
7
$\begingroup$

enter image description here

code

Manipulate[
 solveAgain[] := Module[{g = 9.81,u,t},
   currentTime = 0;
   sol = NDSolveValue[{u''[t] == (-g/len)*Sin[u[t]], 
      u[0] == u0 Degree, u'[0] == ud0}, u, {t, 0, maxTime}];
   ];
 
 Module[{pendulum, output},
  pendulum[u_] := {Line[{{0, 0}, {Cos[u - Pi/2], Sin[u - Pi/2]}}], {Red, 
     Disk[{Cos[u - Pi/2], Sin[u - Pi/2]}, 0.1]}};
  Which[state == "reset",currentTime = 0; solveAgain[]
   , state == "run", tick = Not[tick];
   If[currentTime + 0.1 > maxTime,
    state = "stop",
    currentTime += 0.1
    ]
   , state == "stop",
   Nothing[]
   ];
  output = Graphics[pendulum[sol[currentTime]], 
    PlotRange -> {{-1, 1}, {-1.2, 1.2}}, ImageSize -> 300, 
    Axes -> True];
  Grid[{{Row[{"Time = ", NumberForm[currentTime, {2, 2}]}]},
    {output}}]
  ]
 ,
 Grid[{{"Length", 
    Manipulator[
     Dynamic[len, {len = #; solveAgain[]; tick = Not[tick]} &], {0, 
      10, .1}, ImageSize -> Tiny], Dynamic[len]},
   {"mass", Manipulator[
     Dynamic[m, {m = #; solveAgain[]; tick = Not[tick]} &], {0, 
      10, .1}, ImageSize -> Tiny], Dynamic[m]},
   {"Initial angle (degree)", 
    Manipulator[
     Dynamic[u0, {u0 = #; solveAgain[]; tick = Not[tick]} &], {0, 90, 
      1}, ImageSize -> Tiny], Dynamic[u0]},
   {"Initial speed)", 
    Manipulator[Dynamic[ud0, {ud0 = #; solveAgain[]; tick = Not[tick]} &], {0, 
      10, .1}, ImageSize -> Tiny], Dynamic[ud0]},
   {"Max animation time", 
    Manipulator[
     Dynamic[maxTime, {maxTime = #; solveAgain[]; 
        tick = Not[tick]} &], {0, 10, .1}, ImageSize -> Tiny], 
    Dynamic[maxTime]}
   }]
 ,
 Grid[{{Button["Run", {state = "run"; tick = Not[tick]}],
    Button["Stop", {state = "stop"; tick = Not[tick]}],
    Button["Reset", {state = "reset"; tick = Not[tick]}]}}, 
  Spacings -> {1, 1}, Alignment -> Center
  ],
 {{tick, False}, None},
 {{state, "reset"}, None},
 {{currentTime, 0}, None},
 {{sol, 0}, None},
 {{len, 5}, None},
 {{m, 5}, None},
 {{u0, 30}, None},
 {{ud0, 1}, None},
 {{maxTime, 5}, None},
 TrackedSymbols :> {tick, state}
 ]
$\endgroup$
6
$\begingroup$

Try

Animate[Graphics[pendulum[inga[t]] , PlotRange -> {{-1, 1}, {-1, 1}}] , {t, 0, 10}]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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