# Plotting a switching function

I'm trying to use a switch-function. I'm using $d(t)=d_o+\Delta d_1*UnitStep(t-0.01)+\Delta d_2*UnitStep(t-0.04)$ and a sawtooth function with period 0.002 and amplitude 1. To generate the sawtooth function, I'm using:

T=0.002;
teeth[t_] := t/T*(UnitStep[t] - UnitStep[t - T]);
Saw[t_] := teeth[Mod[t, T]];


So, the idea is the following: if $d(t)>Saw(t)\Rightarrow Switch(t)=1$ Otherwise, $Switch(t)=0$. I did the following in Mathematica:

T = 0.002;
do=0.5;
d1=-0.2;
d2=0.2
d[t_] = do + d1*UnitStep[t - 0.01] + d2*UnitStep[t - 0.04];
teeth[t_] := t/T*(UnitStep[t] - UnitStep[t - T]);
Saw[t_] := teeth[Mod[t, T]];
S[t_] := Piecewise[{{1, d[t] > Saw[t]}, {0, d[t] < Saw[t]}, {0,
d[t] == Sierra[t]}}];
Plot[S[t], {t, 0, 0.1}, Exclusions -> None]


Using that, I get: However, I'm supposed to get: In the last image the switching function is multiplied by 5. So, it seems that my function has a huge gap and I don't know why. The weird part is that when I try to evaluate, let's say, S[0.08] I get 1 as output but the graph shows a zero (the gap). So, I thought that it was a problem with Plot but I'm not sure. Thanks in advance.

• Add 'PlotPoints -> 200' to your plot function. – bill s Aug 8 '17 at 2:51
• Could it be that you just want Plot[5 SquareWave[{0, 1}, x/0.002], {x, 0, .1}, ExclusionsStyle -> Automatic, PlotPoints -> 40, AspectRatio -> 1/4, ImageSize -> Large]? – Edmund Aug 8 '17 at 2:58
• PlotPoints Fixed the problem, thanks. @Edmund it doesn't work using SquareWave because, if you carefully look at the image I posted, the period isn't always the same. Actually, between 0.01 and 0.03 the squares are a bit thinner. – Miguel Duran Diaz Aug 8 '17 at 3:11