0
$\begingroup$

Trying to plot the angles for a double pendulum against each other such that they stay within the bounds of -Pi to Pi. The NDSolve solves the equations such that if a pendulum does a full loop the angle goes over Pi or goes below -Pi. I was able to convert the angle such that it stays between -Pi and Pi but when in the plot it connects the discontinuities with a line. I am not the best at coding and can't figure out how to get rid of those lines.

Subscript[x, 1] := Subscript[l, 1]*Sin[Subscript[\[Theta], 1][t]]
Subscript[vx, 1] := D[Subscript[x, 1], t]
Subscript[y, 1] := -Subscript[l, 1]*Cos[Subscript[\[Theta], 1][t]]
Subscript[vy, 1] := D[Subscript[y, 1], t]
T1 := 1/2*Subscript[m, 1]*(Subscript[vx, 1]^2 + Subscript[vy, 1]^2)
U1 := Subscript[m, 1]*g*Subscript[y, 1]

Subscript[x, 2] := 
 Subscript[l, 1]*Sin[Subscript[\[Theta], 1][t]] + 
  Subscript[l, 2]*Sin[Subscript[\[Theta], 2][t]]
Subscript[vx, 2] := D[Subscript[x, 2], t]
Subscript[y, 2] := -Subscript[l, 1]*Cos[Subscript[\[Theta], 1][t]] - 
  Subscript[l, 2]*Cos[Subscript[\[Theta], 2][t]]
Subscript[vy, 2] := D[Subscript[y, 2], t]
T2 := 1/2*Subscript[m, 2]*(Subscript[vx, 2]^2 + Subscript[vy, 2]^2)
U2 := Subscript[m, 2]*g*Subscript[y, 2]

T := T1 + T2

U := U1 + U2

L := T - U // FullSimplify

\[Theta]1EQ := 
 D[L, Subscript[\[Theta], 1][t]] - 
   D[D[L, Derivative[1][Subscript[\[Theta], 1]][t]], t] // FullSimplify

\[Theta]2EQ := 
 D[L, Subscript[\[Theta], 2][t]] - 
   D[D[L, Derivative[1][Subscript[\[Theta], 2]][t]], t] // FullSimplify

g := 9.8
Subscript[m, 1] := 2
Subscript[m, 2] := 2
Subscript[l, 1] := 1
Subscript[l, 2] := 1
Subscript[t, o] := 0
Subscript[t, f] := 30

C1 := Subscript[\[Theta], 1][0] == Pi
C2 := Derivative[1][Subscript[\[Theta], 1]][0] == 0
C3 := Subscript[\[Theta], 2][0] == Pi/8
C4 := Derivative[1][Subscript[\[Theta], 2]][0] == 0

s1 = NDSolve[{\[Theta]1EQ == 0, \[Theta]2EQ == 0, C1, C2, C3, 
   C4}, {Subscript[\[Theta], 1], Subscript[\[Theta], 2]}, {t, 
   Subscript[t, o], Subscript[t, f]}, AccuracyGoal -> 10, 
  PrecisionGoal -> 10]

Plot[Evaluate[{Subscript[\[Theta], 1][t], 
    Subscript[\[Theta], 2][t]} /. s1],
 {t, Subscript[t, o], Subscript[t, f]},
 ImageSize -> 300,
 PlotLegends -> {"\!\(\*SubscriptBox[\(\[Theta]\), \(1\)]\)", 
   "\!\(\*SubscriptBox[\(\[Theta]\), \(2\)]\)"}]

Plot[Evaluate[{(Subscript[\[Theta], 1][t] + Pi - 
      Floor[(Subscript[\[Theta], 1][t] + Pi)/(2 Pi)]*2 Pi - 
      Pi), (Subscript[\[Theta], 2][t] + Pi - 
      Floor[(Subscript[\[Theta], 2][t] + Pi)/(2 Pi)]*2 Pi - Pi)} /. 
   s1],
 {t, Subscript[t, o], Subscript[t, f]},
 ImageSize -> 300,
 PlotLegends -> {"\!\(\*SubscriptBox[\(\[Theta]\), \(1\)]\)", 
   "\!\(\*SubscriptBox[\(\[Theta]\), \(2\)]\)"}]

ParametricPlot[
 Evaluate[{(Subscript[\[Theta], 1][t] + Pi - 
      Floor[(Subscript[\[Theta], 1][t] + Pi)/(2 Pi)]*2 Pi - 
      Pi), (Subscript[\[Theta], 2][t] + Pi - 
      Floor[(Subscript[\[Theta], 2][t] + Pi)/(2 Pi)]*2 Pi - Pi)} /. 
   s1], {t, Subscript[t, o], Subscript[t, f]},
 PlotRange -> Automatic,
 PlotStyle -> {Blue},
 AxesLabel -> {Subscript[\[Theta], 1], Subscript[\[Theta], 2]},
 Ticks -> None,
 ImageSize -> 300]

Animate[
 Show[
  ParametricPlot[
   Evaluate[{(Subscript[\[Theta], 1][t] + Pi - 
        Floor[(Subscript[\[Theta], 1][t] + Pi)/(2 Pi)]*2 Pi - 
        Pi), (Subscript[\[Theta], 2][t] + Pi - 
        Floor[(Subscript[\[Theta], 2][t] + Pi)/(2 Pi)]*2 Pi - Pi)} /. 
     s1], {t, 0, Time},
   PlotPoints -> 100,
   PlotStyle -> Blue,
   PlotRange -> Automatic,
   PerformanceGoal -> "Quality",
   AxesLabel -> {Subscript[\[Theta], 1], Subscript[\[Theta], 2]},
   Ticks -> None],
  Graphics[{PointSize[.025], Red, 
    Point[Evaluate[{(Subscript[\[Theta], 1][Time] + Pi - 
          Floor[(Subscript[\[Theta], 1][Time] + Pi)/(2 Pi)]*2 Pi - 
          Pi), (Subscript[\[Theta], 2][Time] + Pi - 
          Floor[(Subscript[\[Theta], 2][Time] + Pi)/(2 Pi)]*2 Pi - 
          Pi)} /. s1]]}]
  ],
 {Time, 0.000000001, Subscript[t, f]}, 
 DefaultDuration -> Subscript[t, f], AnimationRepetitions -> 1, 
 AnimationRunning -> False
 ]
$\endgroup$

Your Answer

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

Browse other questions tagged or ask your own question.