2
$\begingroup$

I have found similar questions to mine, but dealing with piecewise and parametric plots instead and unfortunately the answers don't seem to extend to my case.

I have a solution from NDSolve for two oscillators, however I would like to plot that solution between 0 and 2pi instead of the whole set of reals. I have used Mod to keep the values between the desired range, but I am left with the discontinuities you see below. Is there anyway to eliminate these discontinuities for the general case?

Thanks :)

Code to generate solutions:

Example[θ1IC0_, θ2IC0_] := 
 Module[{θ1IC = θ1IC0, θ2IC = θ2IC0}, 
  n = 2; 
  ω = {0.95 Ω0, 1.05 Ω0};
  θ = {θ1, θ2};
  Ω0 = (2 π)/24;
  Eqs = Table[θ[[i]]'[t] == ω[[i]] + 1/n Sum[Sin[θ[[j]][t] - θ[[i]][t]], {j, 1, 2}],
         {i, 1, 2}];
  ICs = {θ[[1]][0] == θ1IC, θ[[2]][0] == θ2IC};
  EqsICs = Join[Eqs, ICs];
  {Solution = NDSolve[EqsICs, {θ[[1]], θ[[2]]}, {t, 0, 100}]}]

Here is the code to plot the solutions:

Show[Table[
  Plot[Mod[θ[[i]][τ] /. Example[0, π/2], 2 π], {τ, 0, 40},
   PlotRange -> All, AxesOrigin -> {0, 0}, 
   AxesLabel -> {"t", "\!\(\*SubscriptBox[\(θ\), \(i\)]\)"}, 
   PlotStyle -> TwoColours[[i]], 
   PlotLegends -> Placed[{ToString[θ[[i]]]}, Below]],
  {i, 1, 2}]]

And here is the output:

enter image description here

$\endgroup$
  • $\begingroup$ Note that you haven't defined TwoColours in the code you've provided, but that only seems to be necessary for the coloration of the plotted graph. $\endgroup$ – Michael Seifert Mar 12 '19 at 14:08
  • $\begingroup$ Here's a much simpler example that shows the problem you've encountered: soln = NDSolve[{x'[t] == 1, x[0] == 0}, x, {t, 0, 10}]; Plot[Mod[x[t] /. soln, 1], {t, 0, 4}, Exclusions -> "Discontinuities", ExclusionsStyle -> None] (Note that adding the Exclusions options does not solve the problem.) $\endgroup$ – Michael Seifert Mar 12 '19 at 14:13
  • $\begingroup$ Hey @MichaelSeifert ! Yes, it should be: TwoColours = {Red, Blue}; Thanks for the catch :) $\endgroup$ – Cameron F. Mar 12 '19 at 14:26
  • $\begingroup$ Do you want disconnected discontinuities, such as Michael shows below, or do you want to get rid of the discontinuities? The latter cannot be done using Mod, since it is discontinuous. $\endgroup$ – Michael E2 Mar 12 '19 at 14:41
  • $\begingroup$ Hey other Michael! Just disconnected, I should've been more clear. Thanks :) $\endgroup$ – Cameron F. Mar 12 '19 at 14:45
2
$\begingroup$

Too long for a comment, and it may not be possible to implement this in your code; but using NDSolveValue rather than NDSolve appears to make a difference in the following toy example:

soln = NDSolve[{x'[t] == 1, x[0] == 0}, x, {t, 0, 100}]
Plot[Mod[x[t] /. First[soln], 1], {t, 0, 4}, Exclusions -> "Discontinuities", ExclusionsStyle -> None]

enter image description here

xsol[t_] = NDSolveValue[{x'[t] == 1, x[0] == 0}, x[t], {t, 0, 100}]
Plot[Mod[xsol[t], 1], {t, 0, 4}, Exclusions -> "Discontinuities", ExclusionsStyle -> None]

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ The interesting thing is that the interpolating functions seem identical: Head[xsol[t]] - (x /. First@soln). But even if you Evaluate the Plot argument, it still connects the discontinuities. $\endgroup$ – Michael E2 Mar 12 '19 at 14:35
  • $\begingroup$ Stranger: Plot[Mod[x[t] /. {x[t] -> xsol[t]}, 1] // Evaluate, {t, 0, 4}, Exclusions -> "Discontinuities", ExclusionsStyle -> None] connects the discontinuities, even though NDSolveValue is used. Perhaps has to do with ReplaceAll? $\endgroup$ – Michael E2 Mar 12 '19 at 14:37
  • $\begingroup$ Thanks, Michael! Having it work generally, even if it is outside of my original structure, works perfectly for me. My PhD supervisor just wants a few of our results presented like that. Thanks again :) $\endgroup$ – Cameron F. Mar 12 '19 at 14:42
  • $\begingroup$ Mod[soln[[1, 1, 2]][t], 1] with the NDSolve code is also plotted sorrectly. $\endgroup$ – Michael E2 Mar 12 '19 at 15:35
  • $\begingroup$ I posted a question related to this: mathematica.stackexchange.com/q/193103/4999 $\endgroup$ – Michael E2 Mar 12 '19 at 16:20

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.