# How to use PostScript for to plot the sum of cos(n*x) for n=0 to n=100?

I want to plot the function f(x) which is the sum of cos(n*x) for n=0 to n=100. The domain is from 0 to 2π.

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-plot}

\begin{document}
\begin{pspicture}(-5,-5)(5,5)
\psplot[plotpoints=100]{0}{TwoPi}{%
/k 0 def
/out 0 def
0 1 10
{
} for
out
}
\end{pspicture}
\end{document}


The output seems wrong as follows. How to use the PostScript for here?

• there is an exact formula for this sum, it is 1/2 + half of the Dirichlet kernel, hence: 1/2+\sin(100.5 x)/(2\sin(0.5 x)).
– user4686
Feb 1, 2014 at 17:56
• @jfbu: OK. But I am learning how to use for in this question. :-) Feb 1, 2014 at 18:00
• ah yes, then why not something fun like sin(nx)/log n and the same with cos(nx)/log n :)
– user4686
Feb 1, 2014 at 18:06
• @jfbu: Because n=0 is dangerous. Feb 1, 2014 at 18:14

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-plot}

\begin{document}
\begin{pspicture}(-1,-7)(7,7)
\psaxes{->}(0,0)(0,-7)(7,7)
\psplot[plotpoints=5000,linewidth=0.2pt]{0}{TwoPi}{%
/out 0 def
0 1 100
{ % loop variable is on the stack
} for
out
}
\end{pspicture}

\end{document} the same with

\psplot[plotpoints=5000,plotstyle=dots,dotscale=0.4]{0}{TwoPi}{% 