1
$\begingroup$

I have to obtain plots of trajectories for (theta(t),u(t)) and then overlay them with a contour plot. The overlaying with the contour plot it is easy. What trubles me though is the trajectory part.

First, I solve the system, then I do a simple plot for the values and finally a parametric plot as follows:

s = NDSolve[{Theta'[t] == u[t], u'[t] == -Sin[Theta[t]], 
Theta[0] == 0, u[0] == 1.5}, {Theta, u}, {t, -4 Pi, 4 Pi}]

Plot[Evaluate[{Theta[t], u[t]} /. s], {t, -4 Pi, 4 Pi}, 
PlotLegends -> {"Theta[t]", "u[t]"}, AxesLabel -> {t, E[t]}, 
LabelStyle -> Directive[Blue, Bold]]

ParametricPlot[Evaluate[{Theta[t], u[t]} /. s], {t, -4 Pi, 4 Pi}]

Show[ContourPlot[(1/2) u^(2) + 1 - Cos[Theta], {Theta, -4 Pi, 
4 Pi}, {u, -5, 5}], 
ParametricPlot[Evaluate[{Theta[t], u[t]} /. s], {t, -4 Pi, 4 Pi}]]

After getting the the parametric plot, using Show[...] I combine it with the contour one. Instead of recurring at every closed contour, it appears only once.

Do I have to use a different kind of plotting to have it repeatedly?

$\endgroup$
  • $\begingroup$ I don't see a ContourPlot in your code. Where is the contour plot? And what do you mean by "Instead of recurring at every closed contour, it appears only once."? $\endgroup$ – march Oct 10 '15 at 3:34
  • $\begingroup$ I used this: Show[ContourPlot[(1/2) u^(2) + 1 - Cos[Theta], {Theta, -4 Pi, 4 Pi}, {u, -5, 5}], ParametricPlot[Evaluate[{Theta[t], u[t]} /. s], {t, -4 Pi, 4 Pi}]] to combine the contour plot and the parametric above, and when I do the parametric appears inside a closed contour only once. Should it appear more times, since it states "trajectory" shouldn't it repeat? @march $\endgroup$ – curious_math Oct 10 '15 at 3:39
  • $\begingroup$ For the un-damped, non-driven pendulum, the contours of constant energy are exactly the trajectories in phase space. For this reason, you could just use the solution from here. Your different trajectories (the different closed-curve ellipses) all represent the same trajectory, actually, since really, $-\pi/2\leq\theta \leq \pi/2$, so you really only need to plot in this region. $\endgroup$ – march Oct 10 '15 at 4:58
2
$\begingroup$
s = ParametricNDSolveValue[{th'[t] == u[t], u'[t] == -Sin[th[t]], 
                            th[0] == i, u[0] == 1.5}, {th, u}, {t, -4 Pi, 4 Pi}, {i}]
Show[
     ContourPlot[(1/2) u^(2) + 1 - Cos[th], {th, -4 Pi, 4 Pi}, {u, -5, 5}, 
                ColorFunction -> "Pastel"], 
     ParametricPlot[Through[s[#][t]], {t, -4 Pi, 4 Pi}] & /@ Pi Range[-4, 4, 2]]

Mathematica graphics

$\endgroup$
  • $\begingroup$ Thanks again :) answer exactly on point! $\endgroup$ – curious_math Oct 10 '15 at 4:15
  • $\begingroup$ I am having a problem with the ParametricPlot portion. I'm on 10.2 and started a new Mathematica and copied and executed your code. The ParametricPlot has a problem. I ran a test replacing ParametricPlot with Table. This Table[Through[s[#][t]], {t, -4 Pi, 4 Pi}] & /@ Pi Range[-4, 4, 2] produces {-4 \[Pi], -2 \[Pi], 0, 2 \[Pi], 4 \[Pi]}. However when I write it out a bit more Map[ (Table[Through[s[#][t]], {t, -4 \[Pi], 4 \[Pi]}]) &, Pi Range[-4, 4, 2] ] I get the right answer. I am a bit lost. $\endgroup$ – Jack LaVigne Oct 10 '15 at 21:34
  • $\begingroup$ This works Show[ ContourPlot[(1/2) u^(2) + 1 - Cos[th], {th, -4 Pi, 4 Pi}, {u, -5, 5}, ColorFunction -> "Pastel"], Map[ (ParametricPlot[Through[s[#][t]], {t, -4 \[Pi], 4 \[Pi]}]) &, Pi Range[-4, 4, 2] ] ] $\endgroup$ – Jack LaVigne Oct 10 '15 at 21:37

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.