I want to draw thw following sequence but I don't know how to do that.
f_n(t)=
\begin{cases}
-1,~ -1\leq t\leq -\frac1n,\\
\frac{t}{n},~~~\frac1n\leq t\leq \frac1n,\\
1,~~~~~\frac1n\leq t\leq 1.
\end{cases}
I started by this, but I don't know how to put $-\frac1n, \frac1n$
on the graph
\documentclass[tikz]{standalone}
\usepackage{pgfplots}
%\pgfplotsset{compat=1.8}
\begin{document}
\begin{tikzpicture}[
declare function={
func(\x)= -1 +
and(\x>-1-\frac1n, \x<=1/n) * (-1) +
and(\x>-1/n, \x<=1/n) * (\x/n) +
(\x>1/n, \x<=1) * (1);
}
]
\begin{axis}[
axis x line=middle, axis y line=middle,
%ymin=-, ymax=2, ytick={-2,...,2}, ylabel=$f_n(x)$,
%xmin=-1, xmax=1, xtick={-1,...,1}, xlabel=$x$,
]
%\pgfplotsinvokeforeach{-2, 1, 2}{
\addplot[blue, domain=-1:1, smooth]{func(x)};
\end{axis}
\end{tikzpicture}
\end{document}
Best Answer
As mentioned in chat,
\frac
is a macro used for typesetting math.pgfmath
doesn't understand it, but just use1/n
instead of\frac{1}{n}
.Also, you don't need explicit spaces in
cases
, just add a column separator,&
.Anyways, this is the plot for
n=2
of the equation you specified.But perhaps you were after this:
Alternatively