5

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 .

\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
    {
        x k mul RadtoDeg cos out add /out ED
        k 1 add /k ED
    } for
    out
  }  
\end{pspicture}
\end{document}

The output seems wrong as follows.

enter image description here

How to use the PostScript for here?

4
  • 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

1 Answer 1

3
\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
        x mul RadtoDeg cos out add /out ED
    } for
    out
  }  
\end{pspicture}

\end{document}

enter image description here

the same with

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

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.