2

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 2\pi$,$3\pi$},
    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 :)

1 Answer 1

2

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 2\pi$, $3\pi$},
    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).

2
  • Thank you very much! I will definitely take note about writing down the equations in a better way next time Mar 17, 2021 at 1:00
  • You are welcome. However, I could not answer why this does not work. Maybe someone else can explain this ... Mar 17, 2021 at 1:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .