Tell me more ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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]

enter image description here

but if I increase my Plot interval beyond 16 I get

enter image description here

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]

enter image description here

Ideas anyone?

share|improve this question
4  
Increase PlotPoints. – J. M. Aug 10 '12 at 16:16
It's just a plotting problem. As @J.M. says - increase PlotPoints and all will be well. – Jens Aug 10 '12 at 16:17
@J.M. - Solved! Post it as an answer and I'll accept it. You posted before bill s. – stevenvh Aug 10 '12 at 16:20
No, it's fine; he posted an answer first. :) (Actually, I was hoping you'd answer yourself.) – J. M. Aug 10 '12 at 16:21
some details here – acl Aug 10 '12 at 17:08
show 1 more comment

2 Answers

up vote 12 down vote accepted

Looks as if it might be that you need to increase the number of points plotted, as in:

Plot[pwm[t, 5], {t, 0, maxt}, Exclusions -> None, PlotRange -> All, PlotPoints -> 100]
share|improve this answer

This problem crops up again and again. A better understanding of Mathematica's plot algorithm and options will prevent it from being a surprise. I highly recommend reading the great answers to these questions:


I realize this answer is in essence a couple of links, and such answers are discouraged. Nevertheless the alternatives in this case appear less desirable. The decision has been made not to migrate the linked questions.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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