[Tex/LaTex] Calculate function with integers as input and plotting

pgfplotsplot

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

Plotting 1/2*x(x+1)

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}
  ] 
    \addplot {0.5*x*(x-1)}; 
  \end{axis}
\end{tikzpicture}
\end{figure}

So what is that I am doing wrong?

Best Answer

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
  ] 
    \addplot {0.5*x*(x-1)}; 
  \end{axis}
\end{tikzpicture}
\end{document}

Plot example