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:

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;
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: enter image description here

However, I'm supposed to get: enter image description here

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.

  • 4
    $\begingroup$ Add 'PlotPoints -> 200' to your plot function. $\endgroup$ – bill s Aug 8 '17 at 2:51
  • $\begingroup$ 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]? $\endgroup$ – Edmund Aug 8 '17 at 2:58
  • $\begingroup$ 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. $\endgroup$ – Miguel Duran Diaz Aug 8 '17 at 3:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.