It's really important to give a minimum working example (MWE) as opposed to a code snippet. It wasn't clear to me if you were unable to get your code to compile or you just didn't like the output for some reason (too jagged?) and want a better algorithm to make it look like your picture. Adding up 16 cosine functions that have to be evaluated at multiple values using the built in system that lacks great mathematical precision along with samples=200 makes me think your picture will be jagged. A recent question wanted to add up, as an example, 101 terms. Questions like that, and yours, require an engine that can handle the calculations (such as lua). I've used the sagetex package which lets a computer algebra system (CAS) do the calculations. It also has lots of mathematics built in and gives you the Python programming language as well.
\documentclass{standalone}
\usepackage{sagetex,pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{sagesilent}
f=0
n=16
for i in range(0,n+1):
f+= (1+cos(i*2*pi*x))
f = f/n
step = .01
x_coords = [x for x in srange(0,2+step,step)]
y_coords = [f(x=x).n(digits=4) for x in x_coords]
output = r"\begin{tikzpicture}"
output += r"\begin{axis}[xmin=0,xmax=2,ymin=0,ymax=3,"
#output += r"xlabel=$x$,ylabel=$y$,axis x line=middle,axis y line=middle,"
output += r"grid style=dashed]"
output += r"\addplot[thin, blue, smooth] coordinates {"
for i in range(0,len(x_coords)-1):
output += r"(%f , %f) "%(x_coords[i],y_coords[i])
output += r"};"
output += r"\end{axis}"
output += r"\end{tikzpicture}"
\end{sagesilent}
\sagestr{output}
\end{document}

The line x_coords = [x for x in srange(0,2+step,step)] is forcing my xvalues to go from 0,.01,...,1.99,2.0 and y_coords is calculated using SAGE, a free, open source CAS. With SAGE calculating around 200 points that I've specifically told it and the smooth option to remove some of the jaggedness, the result is what you have in your picture. No special algorithm to connect the plotted points needed.
Since SAGE is not part of a LaTeX distribution the easiest way to work with it is through a free Cocalc account.