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:
Answer
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]
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]



Comments
Post a Comment