2
$\begingroup$

first post and somewhat new to Mathematica. Extremely new to making animations in Mathematica. My goal is to make a 3D animation of a Spherical Spring Pendulum system for a personal project. I received help and added the 'First' function when evaluating my functions {x[t],y[t],z[t]}, which helped me plot the path of motion. Now what I am struggling with is plotting the path along with the 3D animation I created at the same time. Also, if anyone would be able to help me make the pendulum rope look like a spring I would greatly appreciate. Thanks in advance for any help you can offer !

(* Defining Constants *)
g = 9.81;
m = 2;
k = 5;
r0 = 1;
\[Phi]0 = (Pi/2);
\[Theta]0 = (Pi/2);
r1 = 0.5;
\[Phi]1 = 0.1;
\[Theta]1 = Pi;


(* Solving Equations of Motion *)

Solutions = 
  NDSolve[{\[Theta]''[
      t] == (Sin[\[Theta][t]]*
      Cos[\[Theta][t]]*(\[Phi]'[t]^2)) - ((g*Sin[\[Theta][t]])/
        r[t]) - ((2*r'[t]*\[Theta]'[t])/r[t]), 
    r''[t] == ( 
       r[t]*(\[Theta]'[t]^2)) + ((r[t]*(Sin[\[Theta][t]]^2))*(\
[Phi]'[
           t]^2)) + g*Cos[\[Theta][t]] - ((k/m)*(r[t] - 1)), \[Phi]''[
  t] == -\[Phi]'[t]*((2*r'[t])/(r[t])) - (2*
    Cot[\[Theta][t]]*\[Theta]'[t]*\[Phi]'[t]), 
r[0] == r0, \[Phi][0] == \[Phi]0, \[Theta][0] == \[Theta]0, 
r'[0] == r1, \[Phi]'[0] == \[Phi]1, \[Theta]'[0] == \[Theta]1}, {r, \[Phi], \[Theta]}, {t, 0, 60}];

x[t_] := First[Evaluate[(r[t]*(Sin[\[Theta][t]]*Cos[\[Phi][t]])) /. Solutions]]
y[t_] := First[Evaluate[(r[t]*Sin[\[Phi][t]]*Sin[\[Theta][t]]) /. Solutions]]
z[t_] := First[Evaluate[-(r[t]*Cos[\[Theta][t]]) /. Solutions]]


(* Plotting Path *)

Motion = Table[ ParametricPlot3D[{x[t], y[t], z[t]}, {t, 0, tmax}, 
PlotRange -> {{-5, 5}, {-5, 5}, {-5, 5}}, MeshStyle -> None, 
PerformanceGoal -> "Quality", Boxed -> True], {tmax, 0.1, 
30, .1}];

(* 3D Animation *)
Animate[Show[Graphics3D[{{Red, PointSize[.03], 
 Point[{x[t], y[t], z[t]}]}, {Black, PointSize[.01], 
 Point[{0, 0, 0}]}, {Black, PointSize[.01], 
 Point[{0, 0, 
   5}]}, {Line[{{0, 0, 0}, {0, 0, 5}}]}, {Line[{{0, 0, 0}, {x[t], 
    y[t], z[t]}}]}}],PlotRange -> {{-20, 20}, {-20, 20}, {-20, 20}},Boxed -> True], {t, 0.1, 40, 0.1}]
$\endgroup$
2
  • 1
    $\begingroup$ Try evaluating e.g. {x[0], y[0], z[0]} and you'll see that you have a list in the format {{x}, {y}, {z}} while the format you want to have, for ParametricPlot3D, is {x, y, z}. You can solve this by wrapping Evaluate with First in your definitions for x, y, and z. $\endgroup$
    – C. E.
    Dec 2, 2018 at 1:22
  • $\begingroup$ You can use Show to combine ParametricPlot3D and Graphics3D. $\endgroup$
    – C. E.
    Dec 2, 2018 at 9:39

1 Answer 1

5
$\begingroup$
g = 9.81;
m = 2;
k = 5;
r0 = 1;
\[Phi]0 = (Pi/2);
\[Theta]0 = (Pi/2);
r1 = 0.5;
\[Phi]1 = 0.1;
\[Theta]1 = Pi;
eq = {\[Theta]''[t] == 
    Sin[\[Theta][t]]*Cos[\[Theta][t]]*\[Phi]'[t]^2 - 
     g*Sin[\[Theta][t]]/r[t] - 2*r'[t]*\[Theta]'[t]/r[t], 
   r''[t] == 
    r[t]*\[Theta]'[t]^2 + (r[t]*(Sin[\[Theta][t]]^2)*\[Phi]'[t]^2) + 
     g*Cos[\[Theta][t]] - (k/m)*(r[t] - 1), \[Phi]''[
     t] == -\[Phi]'[t]*2*r'[t]/r[t] - 
     2*Cot[\[Theta][t]]*\[Theta]'[t]*\[Phi]'[t]};
ic = {r[0] == r0, \[Phi][0] == \[Phi]0, \[Theta][0] == \[Theta]0, 
   r'[0] == r1, \[Phi]'[0] == \[Phi]1, \[Theta]'[0] == \[Theta]1};

Solutions = 
 Flatten[NDSolve[{eq, ic}, {r, \[Phi], \[Theta]}, {t, 0, 60}]]

x[t_] := Evaluate[r[t]*Sin[\[Theta][t]]*Cos[\[Phi][t]] /. Solutions]
y[t_] := Evaluate[r[t]*Sin[\[Phi][t]]*Sin[\[Theta][t]] /. Solutions]
z[t_] := Evaluate[-r[t]*Cos[\[Theta][t]] /. Solutions]
ListAnimate[
 Table[Show[
   Graphics3D[{{Red, PointSize[.03], 
      Point[{x[t], y[t], z[t]}]}, {Black, PointSize[.01], 
      Point[{0, 0, 0}]}, {Black, PointSize[.01], 
      Point[{0, 0, 
        2}]}, {Line[{{0, 0, 0}, {0, 0, 2}}]}, {Line[{{0, 0, 0}, {x[t],
          y[t], z[t]}}]}}], PlotRange -> {{-5, 5}, {-5, 5}, {-11, 2}},
    Boxed -> True], {t, 0.1, 40, 0.1}]]

fig1

$\endgroup$
3
  • $\begingroup$ Yes Alex Trounev, Thank you so much! I was able to get the 3D plot working eventually but your method seems much cleaner. Do you know of a way to make the pendulum string look like a spring or coil? $\endgroup$ Dec 2, 2018 at 21:24
  • $\begingroup$ Should it be a deformable spring or not? $\endgroup$ Dec 3, 2018 at 11:53
  • $\begingroup$ Yeah, if it could look like its stretching as it goes up and down with x[t],y[t], and z[t] that would be great ! $\endgroup$ Dec 5, 2018 at 0:03

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.