I need to draw a wave that is the summation of these cosine waves
This is my attempt in LuaLaTeX:
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{luacode}
\begin{luacode*}
function p(x)
assert(x == math.floor(x), "x must be an integer")
res = 0
for W = 0, 4000 do
res = res + (1/2) * (1 + math.cos(2math.pixW))
end
tex.sprint(res)
end
\end{luacode}
\begin{document}
\begin{tikzpicture}[
declare function={p(\n) = \directlua{p(\n)};}
]
\begin{axis}[
use fpu=false, % very important!
xlabel=$x$, ylabel=$p(x)$,
samples at={0,...,21},
only marks,
]
\addplot {p(x)};
\end{axis}
\end{tikzpicture}
\end{document}
The shape of the graph I expect held a central absolute maximum, with local maximums that decrease as the central maximum moves away
The graph show nothing




\sum_{0}^{\infty} 0.5that goes to infty, and then you also have the cosines. (well either your expectation or the formula you show) – Skillmon Jul 09 '23 at 20:50xyou're only ever evaluating multiples of 2pi in the cosine, which is 1, and hence you go to infty for that as well. – Skillmon Jul 09 '23 at 20:55