3
$\begingroup$

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?

$\endgroup$
2

1 Answer 1

5
$\begingroup$

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

$\endgroup$

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.