3

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}
5

As mentioned in chat, \frac is a macro used for typesetting math. pgfmath doesn't understand it, but just use 1/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.

enter image description here

\documentclass{article} 
\usepackage{pgfplots,amsmath} 
%\pgfplotsset{compat=1.8} 
\begin{document} 
\begin{equation}
f_n(t)= 
\begin{cases} 
-1, & -1 < t < -\frac{1}{n},\\ 
\frac{t}{n}, &-\frac{1}{n}\leq t\leq \frac{1}{n},\\ 
1, & \frac{1}{n} < t < 1. 
\end{cases}
\end{equation}

\begin{tikzpicture}[ 
declare function={ 
    func(\x,\n)=
and(\x >=-1, \x <= -1/\n) * (-1) +
and(\x > -1/\n, \x< 1/\n) * (\x/\n) +
and(\x >= 1/\n, \x<=1) * (1) 
;
} 
] 
\begin{axis}[ 
axis x line=middle, axis y line=middle, 
ymin=-2, ymax=2, ytick={-2,...,2}, ylabel=$f_n(x)$, 
xmin=-1, xmax=1, xtick={-1,...,1}, xlabel=$x$,
legend entries={$n=2$},
legend pos=north west,legend style={draw=none}
] 
%\pgfplotsinvokeforeach{1, 2, 3, 4}{  
%\addplot+[mark=none,thick,domain=-1:1,samples=100]{func(x,#1)}; 
%}
\addplot+[mark=none,thick,domain=-1:1,samples=100]{func(x,2)}; 

\end{axis} 
\end{tikzpicture} 
\end{document}

But perhaps you were after this:

enter image description here

\documentclass{article} 
\usepackage{pgfplots,amsmath} 
%\pgfplotsset{compat=1.8} 
\begin{document} 
\begin{equation}
f_n(t)= 
\begin{cases} 
-1, &   t < -n,\\ 
\frac{t}{n}, &-n\leq t\leq n,\\ 
1, &  n < t . 
\end{cases}
\end{equation}

\begin{tikzpicture}[ 
declare function={ 
    func(\x,\n)=
(\x <= -\n) * (-1) +
and(\x > -\n, \x< \n) * (\x/\n) +
(\x >= \n) * (1) 
;
} 
] 
\begin{axis}[ 
axis x line=middle, axis y line=middle, 
ymin=-2, ymax=2, ytick={-2,...,2}, ylabel=$f_n(x)$, 
%xmin=-1, xmax=1, xtick={-1,...,1}, 
xlabel=$x$,
legend entries={$n=1$,$n=2$,$n=3$,$n=4$},
legend pos=north west,legend style={draw=none}
] 
\pgfplotsinvokeforeach{1, 2, 3, 4}{  
\addplot+[mark=none,thick,samples=100]{func(x,#1)}; 
}
\end{axis} 
\end{tikzpicture} 
\end{document}

Alternatively

enter image description here

\documentclass{article} 
\usepackage{pgfplots,amsmath} 
%\pgfplotsset{compat=1.8} 
\begin{document}

\noindent\begin{minipage}{0.45\linewidth}
\begin{equation}
f_n(t)= 
\begin{cases} 
-1, &   t < -\frac{1}{n},\\ 
tn, &-\frac{1}{n}\leq t\leq \frac{1}{n},\\ 
1, &  \frac{1}{n} < t . 
\end{cases}
\end{equation}
\end{minipage}\hfill
\begin{minipage}{0.5\linewidth}
\centering
\begin{tikzpicture}[ 
declare function={ 
    func(\x,\n)=
(\x <= -1/\n) * (-1) +
and(\x > -1/\n, \x< 1/\n) * (\x*\n) +
(\x >= 1/\n) * (1) 
;
} 
] 
\begin{axis}[ 
axis x line=middle, axis y line=middle, 
ymin=-2, ymax=2, ytick={-2,...,2}, ylabel=$f_n(t)$, 
%xmin=-1, xmax=1, xtick={-1,...,1}, 
domain=-1.2:1.2,samples=200,
xlabel=$t$,
legend entries={$n=1$,$n=2$,$n=3$,$n=4$},
legend pos=north west,legend style={draw=none},
width=\linewidth,scale only axis
] 
\pgfplotsinvokeforeach{1, 2, 3, 4}{  
\addplot+[mark=none,thick]{func(x,#1)}; 
}
\end{axis} 
\end{tikzpicture} 
\end{minipage}
\end{document}
  • it is not the right graph, t/n is a line passing from 0 – Vrouvrou May 14 '16 at 21:20
  • i poter in the chat what i need to have – Vrouvrou May 14 '16 at 21:36
  • @Vrouvrou Added another variation for good measure. Similar to your original definition, except that the function value is t*n not t/n in the middle part. – Torbjørn T. May 15 '16 at 6:50
  • please i want to introduce the graph in the left the sequence and the right the graph how to do please – Vrouvrou May 15 '16 at 11:02
  • @Vrouvrou tabular (Image next to Equation) or minipage (How can I an place a figure next to an equation?) for example. (In the latter case without the SCfigure and \caption of course.) – Torbjørn T. May 15 '16 at 11:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.