# Graph of Triangle wave

I am trying to graph the following:

Using the following function:

And using this code:

\documentclass[14pt]{extarticle}
\usepackage{pgfplots}
\usepackage{amsmath}
\usepackage{xltabular}
\begin{document}

\begin{center}
\begin{tikzpicture}%[scale=1.5]
\begin{axis}[
domain = -1 : 3,
xlabel = $x$,
ylabel = $y$,
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 = {-1, 1, 3},
xticklabels = {$-1$, $1$, $3$},
declare function = {
T(\x) = (2 / pi) * (1 / sin) * (sin * deg(pi * \x));
}, ]
\end{axis}
\end{tikzpicture}
\end{center}

\end{document}


However, I get the following error: \! Package PGF Math Error: Could not parse input '' as a floating point number, sorry. The unreadable part was near ''. (in '(2/pi)*(1/sin)*(sin*deg(pi*2Y1.0e0 ]))').

• By the way, your sketch doesn't quite correspond to your function I think. For example with x=0, you have asin(sin(0)) which is 0, so the function passes through the origin. In your sketch, the function value is 1 at x=0. – Torbjørn T. May 1 '18 at 17:38
• Could be that the function itself is wrong! – user151562 May 1 '18 at 17:43
• It is probably simpler to draw abs(Mod(\x,2)-1), which is the same function. – Kpym May 1 '18 at 18:02

sin is a function, not a variable, and sin^(-1) is the inverse sine function, or arcsine, which is not the same as 1/sin(..). I think you want T(\x) = (2 / pi) * rad(asin(sin(deg(pi * \x))));. The rad() is needed because asin returns an angle in degrees, and you want it in radians I think.

\documentclass[14pt]{extarticle}
\usepackage{pgfplots}
\begin{document}

\begin{center}
\begin{tikzpicture}%[scale=1.5]
\begin{axis}[
samples at={-1,-0.5,...,3}, % gives a data point every pi/4
xlabel = $x$,
ylabel = $y$,
axis lines = center,
enlargelimits,
every axis x label/.append style = {below},
every axis y label/.append style = {left},
xtick = {-1, 1, 3},
%xticklabels = {$-1$, $1$, $3$}, % not needed, numbers are set in math mode by default
declare function = {
T(\x) = (2 / pi) * rad(asin(sin(deg(pi * \x))));
}
]
\end{axis}
\end{tikzpicture}
\end{center}

\end{document}


# Edit

\documentclass[14pt]{extarticle}
\usepackage{pgfplots}
\usepackage{amsmath}
\begin{document}
\begin{center}
\begin{tikzpicture}%[scale=1.5]
\begin{axis}[
xlabel = $x$,
ylabel = $y$,
axis lines = center,
enlargelimits,
every axis x label/.append style = {below},
every axis y label/.append style = {left},
xtick = {-1, 1, 3},
xticklabel style={fill=white},
clip=false,
axis on top,
name=ax
]

\pgfplotsinvokeforeach{-1,1,3}{
}

\draw  [<->] (-1,-0.5) --
node[below]{
\begin{aligned} 2L &=2 \\ L&=1 \end{aligned}} (1, -0.5);

\end{axis}

\node [right=3mm,yshift=1cm] at (ax.outer east) {$f(x) = \begin{cases} 1+x, & x< 0 \\ 1-x, & x >0 \end{cases}$};
\end{tikzpicture}
\end{center}

\end{document}

• Thank you @Torbjørn T. Another question, how do I do the part below? Where is 2L=2 and L=1? – user151562 May 1 '18 at 17:42
• @user151562 See update for one option. – Torbjørn T. May 1 '18 at 17:56