# Rectangle wave OK in calculations, wrong in Plot

I have a Piecewise function consisting of two rectangle waves with different duty cycles:

dc1 = 0.7; dc2 = 0.3; period = 1; maxt = 15;
rectangleWave[t_, period_, duty_] := UnitBox[Mod[t/period, 1.]/(2. duty)];
pwm1[t_] := rectangleWave[t, period, dc1];
pwm2[t_] := rectangleWave[t, period, dc2];
pwm[t_, tsplit_] := Piecewise[{{pwm1[t], t < tsplit}, {pwm2[t], t >= tsplit}}];
Plot[pwm[t, 5], {t, 0, maxt}, Exclusions -> None, PlotRange -> All]


but if I increase my Plot interval beyond 16 I get

Looks like there's an error in my definition of rectangleWave. However, when I use the Piecewise function in a differential equation it is interpreted correctly:

dc1 = 0.7; dc2 = 0.3; period = 1; y0 = 0.65; maxt = 18;
rectangleWave[t_, period_, duty_] := UnitBox[Mod[t/period, 1.]/(2. duty)];
pwm1[t_] := rectangleWave[t, period, dc1];
pwm2[t_] := rectangleWave[t, period, dc2];
pwm[t_, tsplit_] := Piecewise[{{pwm1[t], t < tsplit}, {pwm2[t], t >= tsplit}}];
c1 = 1; r1 = 2;
s = NDSolve[{r1 c1 y'[t] + y[t] == pwm[t, 5], y[0] == y0}, y, {t, 0, maxt}];
Plot[{pwm[t, 5], Evaluate[y[t] /. s]}, {t, 0, maxt},
Exclusions -> None, PlotRange -> All]


Ideas anyone?

-
some details here – acl Aug 10 '12 at 17:08
– Mr.Wizard Aug 10 '12 at 23:43

Plot[pwm[t, 5], {t, 0, maxt}, Exclusions -> None, PlotRange -> All, PlotPoints -> 100]