TransWikia.com

Fixing the unit step function in ifthenelse statement

TeX - LaTeX Asked by Maria Kuznetsov on May 26, 2021

Consider the following line of code :

documentclass[border = 5pt]{standalone}

usepackage{pgfplots}
usepackage{ifthen}
pgfplotsset{compat=1.14}
begin{document}
begin{center}
begin{tikzpicture}
  begin{axis}[
    xmin = 0, xmax = 4 * pi,
    ymin = -1.5, ymax = 1.5,
    domain = 0 : 3 * pi,
    xlabel = $t$,
    ylabel = $f(t)$,
 legend style={at={(0.8,1)},anchor=north west},
    axis x line = center, 
    axis y line = center,
    every axis x label/.append style = {below},
    every axis y label/.append style = {left},
    samples = 100,
    xtick = {0, 3.14, 6.28, 9.42},
    xticklabels = {$0$, $quad;;pi$, $quad 2pi$,$3pi$},
    declare function = {
      f(x) = ifthenelse(pi<x < 2*pi ,-1,1);
      f_{1}(x) = (4 / pi) * sin(deg(x);
      f_{3}(x) = f_{1}(x) + (4 / pi) * sin(3 * deg(x)) / 3.0));
      f_{5}(x) = f_{3}(x) + (4 / pi) * sin(5 * deg(x)) / 5.0));
    }, ]
    addplot[thick, black] {f(x)};
    addplot[thick,dotted, ultramarine] {f_{1}(x)};
    addplot[thick,dotted, retropurple] {f_{3}(x)};
    addplot[thick,dotted, cyan] {f_{5}(x)};
    legend{$f(t)$, $f_1(t)$, $f_3(t)$, $f_5(t)$};    
     draw[gray, dashed] (5*pi, 0.5) -- (5 * pi, 0.5);
    draw[gray, dashed] (5* pi, 0) -- (5 * pi, 1);
  end{axis}
end{tikzpicture}
end{center}

(Please ignore the color definition here). The output of this code gives :
enter image description here

The issue is with the step function being graphically incorrect (thich black line) because of the ifthenelse statement I don’t know how to fix this small issue where I should obtain the following :

An output of 1 for t between 0 and pi

An output of -1 for t between pi and 2pi

An output of 1 for t between 2pi and 3pi

and any help would be much appreciated. I further apologize for my previous post 🙂

One Answer

Welcome to TeX.SX! Your approach does work, however, it seems you need to rephrase the ifthenelse statement a little bit. Also, note that to use this statement, you don't need to load the ifthen package. The following should work as expected (I changed the colors to default ones and commented out the center environment):

documentclass[border = 5pt]{standalone}

usepackage{pgfplots}
pgfplotsset{compat=1.14}

begin{document}

%begin{center}
begin{tikzpicture}
  begin{axis}[
    xmin = 0, xmax = 4 * pi,
    ymin = -1.5, ymax = 1.5,
    domain = 0 : 3*pi,
    xlabel = $t$,
    ylabel = $f(t)$,
    legend style = {at = {(0.8,1)}, anchor = north west},
    axis x line = center, 
    axis y line = center,
    every axis x label/.append style = {below},
    every axis y label/.append style = {left},
    samples = 100,
    xtick = {0, 3.14, 6.28, 9.42},
    xticklabels = {$0$, $quad;;pi$, $quad 2pi$, $3pi$},
    declare function = {
      f(x) = ifthenelse(and(pi < x, x < 2 * pi), -1, 1);
      f_{1}(x) = (4 / pi) * sin(deg(x);
      f_{3}(x) = f_{1}(x) + (4 / pi) * sin(3 * deg(x)) / 3.0));
      f_{5}(x) = f_{3}(x) + (4 / pi) * sin(5 * deg(x)) / 5.0));
    }, ]
    addplot[thick, black] {f(x)};
    addplot[thick, dotted, blue] {f_{1}(x)};
    addplot[thick, dotted, purple] {f_{3}(x)};
    addplot[thick, dotted, cyan] {f_{5}(x)};
    legend{$f(t)$, $f_1(t)$, $f_3(t)$, $f_5(t)$};
    draw[gray, dashed] (5 * pi, 0.5) -- (5 * pi, 0.5);
    draw[gray, dashed] (5 * pi, 0) -- (5 * pi, 1);
  end{axis}
end{tikzpicture}
%end{center}

end{document}

enter image description here

I don't fully understand, why you need to split up the inequation chain, but I guess that it always returns true since pi is always smaller than 2 * pi (regardless of what x is).

Correct answer by Jasper Habicht on May 26, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP