# Eliminating Discontinuities When Using Mod with NDSolve and Plot for Oscillators

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 = {θ[] == θ1IC, θ[] == θ2IC};
EqsICs = Join[Eqs, ICs];
{Solution = NDSolve[EqsICs, {θ[], θ[]}, {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: • 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. – Michael Seifert Mar 12 '19 at 14:08
• Here's a much simpler example that shows the problem you've encountered: soln = NDSolve[{x'[t] == 1, x == 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.) – Michael Seifert Mar 12 '19 at 14:13
• Hey @MichaelSeifert ! Yes, it should be: TwoColours = {Red, Blue}; Thanks for the catch :) – Cameron F. Mar 12 '19 at 14:26
• 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. – Michael E2 Mar 12 '19 at 14:41
• Hey other Michael! Just disconnected, I should've been more clear. Thanks :) – Cameron F. Mar 12 '19 at 14:45

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}, x, {t, 0, 100}]
Plot[Mod[x[t] /. First[soln], 1], {t, 0, 4}, Exclusions -> "Discontinuities", ExclusionsStyle -> None] xsol[t_] = NDSolveValue[{x'[t] == 1, x == 0}, x[t], {t, 0, 100}]
Plot[Mod[xsol[t], 1], {t, 0, 4}, Exclusions -> "Discontinuities", ExclusionsStyle -> None] • 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. – Michael E2 Mar 12 '19 at 14:35
• 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? – Michael E2 Mar 12 '19 at 14:37
• 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 :) – Cameron F. Mar 12 '19 at 14:42
• Mod[soln[[1, 1, 2]][t], 1] with the NDSolve code is also plotted sorrectly. – Michael E2 Mar 12 '19 at 15:35
• I posted a question related to this: mathematica.stackexchange.com/q/193103/4999 – Michael E2 Mar 12 '19 at 16:20