# Solving a Cauchy problem related to the pendulum

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}]


Animate[Show[ParametricPlot[{t, inga[t]}, {t, 0, 10}],
Graphics[pendulum[t]]], {t, 0, 10}]

• 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]]]. Commented Dec 5, 2021 at 18:41
• I don't know if my solution is correct and i think that the animation has something wrong in it Commented Dec 5, 2021 at 18:44

## 2 Answers

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}
]


Try

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