# Calculate function with integers as input and plotting

I'd like to plot the function 1/2*x(x+1) in for integer values ranging from 3 to 20 and in particular I'd like the function to be plotted in those points. I have managed to create the following graph so far which obviously is not what I want this is the code I have written:

\begin{figure}
\begin{tikzpicture}
\begin{axis}[
xlabel={Numero di elementi},
ylabel={Numero di coppie},
xmin=3, xmax=20,
ymin=0, ymax=55,
xtick={1,...,20}
]
\end{axis}
\end{tikzpicture}
\end{figure}


So what is that I am doing wrong?

You can adjust the calculation/plotting range with the domain keyword. If you want the evaluated points only for integer values of x use the samples keyword to select the appropriate number of samples within the domain range:

\documentclass{standalone}

\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel={Numero di elementi},
ylabel={Numero di coppie},
xmin=3, xmax=20,
ymin=0, %ymax=55,
xtick={1,...,20},
domain={3:20},
samples=18
]
\end{axis}
\end{tikzpicture}
\end{document} The other solutions here try to coerce pgfplots into choosing the samples at the desired locations. There is already a very explicit way to specify where you want the samples taken at and that is to use

samples at={<list>} ## Notes:

• The samples at= has a higher precedence than samples and domain. Hence, samples and domain are ignored when the samples at= is specified.
• If you need to clear a previously set samples at= list, you can do so via samples at={}. This will re-activate the domain key (and also the samples key, but the documentation does not explicitly point this out).

## Code:

\documentclass{standalone}

\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel={Numero di elementi},
ylabel={Numero di coppie},
xmin=3, xmax=20,
ymin=0, ymax=55,
xtick={1,...,20},
samples at={3,...,20}
]
\end{axis}
\end{tikzpicture}
\end{document}

• Why do I keep forgetting this? :) – percusse Nov 18 '12 at 20:38
• @percusse: :-) Actually, I don't even know why I keep remembering this. I am pretty sure I have never used it until now, but for reason it is stuck in my head. – Peter Grill Nov 18 '12 at 20:40

You could use \foreach command.

\begin{figure}
\begin{tikzpicture}
\begin{axis}[
xlabel={Numero di elementi},
ylabel={Numero di coppie},
xmin=3, xmax=20,
ymin=0, ymax=55,
xtick={1,...,20}
]
\foreach \n in {3,4,...,20}{

But I don't get why you say ymax=55 because then you only need to plot from 3 to 11 (the rest of points don't appear). If you want all the points to appear you should comment it. 