Search code examples
functionif-statementwolfram-mathematicaplotpiecewise

Mathematica: Use Piecewise instead of If


I have this periodic defined function via "If":

T = 1;

Tt[t_] := Quotient[t, T]*T
f[t_] := If[t >= Tt[t] && t < ((Tt[t] + T/2)), 1, -1]

I need to redefinie it with "Piecewise". My try looks like this:

g[t_] := Piecewise[{
        {1, (t >= Tt[t] && t < ((Tt[t] + T/2)))}
    },
    -1
];

If I Plot f[t] I get this perfect periodic graph. If I Plot g[t] I get a dashed line at y=1 and a other dashed line at y=-1 (shifted to the other one).

It looks like mathematica plots the pieces of the function correct but does not connect the pieces.


Solution

  • You can use :

    Plot[{g[t]},{t,-5,5}, Exclusions->None]