3

I'm solving the damped driven pendulum:

{γ, g, ω} = {0.5, 1.5, 2/3};
{X0, V0} = {0.6184, 0.};
tmax = 1000;
sol = NDSolve[{x''[t] + γ x'[t] + Sin[x[t]] == g Cos[ω t], x[0] == X0, x'[0] == V0}, x, {t, 0, tmax}, MaxSteps -> Infinity]
sol1[t_] := x[t] /. sol[[1]]
sol2[t_] := x'[t] /. sol[[1]]

I then want to plot the solution $(x,x')$ constraining $x$ to the interval $(-\pi,\pi)$. I came with modulo to make one part $(-2\pi,0)$, the other $(0,2\pi)$, and then restrict the range to $(-\pi,\pi)$. First, it is inelegant. Second, I end with a gap in the middle that even with a big number of PlotPoints I cannot get rid of.

ParametricPlot[{{Mod[sol1[t], -2 π], sol2[t]}, {Mod[sol1[t], 2 π], sol2[t]}}, {t, 0, tmax}, 
 Frame -> True, Axes -> False, PlotStyle -> Black, PlotRange -> {{-π, π}, {-3, 3}}, PlotPoints -> 500]

enter image description here

Using an alternative formulation for modulo:

normalize[angle_?(NumericQ[#] && Im[#] == 0 &)] := angle - 2 Pi Floor[(angle + Pi)/(2 Pi)]

to plot

ParametricPlot[{normalize@sol1[t], sol2[t]}, {t, 0, tmax}, 
 Frame -> True, Axes -> False, PlotStyle -> Black, PlotRange -> {{-π, π}, {-3, 3}}, PlotPoints -> 500]

I get redundant horizontal lines:

enter image description here

How to make such plot elegantly?

corey979
  • 23,947
  • 7
  • 58
  • 101

1 Answers1

5

Perhaps it's easier to use NDSolveValue and Mod[...,2 Pi,-Pi] with offset:

{γ, g, ω} = {0.5, 1.5, 2/3};
{X0, V0} = {0.6184, 0.};
tmax = 1000;
X = NDSolveValue[{x''[t] + γ x'[t] + Sin[x[t]]==g Cos[ω t], x[0] == X0, x'[0] == V0},x, {t, 0, tmax},MaxSteps -> Infinity]

ParametricPlot[ {Mod[X[t], 2 Pi, -Pi], X'[t]}, {t, 0, tmax}]

enter image description here

AsukaMinato
  • 9,758
  • 1
  • 14
  • 40
Ulrich Neumann
  • 53,729
  • 2
  • 23
  • 55