Limit of a sum using complex analysis. I'm trying to find the limit of this sum: $$S_n =\frac{1}{n}\left(\frac{1}{2}+\sum_{k=1}^{n}\cos(kx)\right)$$
I tried to find a formula for the inner sum first and I ended up getting zero as an answer.
The sum is supposed to converge to $\cot(x\over 2)$ and that appears in my last expression but it goes to zero.
Here is what I got with a rather long and clumsy reasoning:
$$S_n = \frac{1}{2n}\left(\cot\left(\frac{x}{2}\right)\sin(nx)+\cos(nx)\right)$$
Thanks in advance.
 A: Just write $\cos (kx)$ as the real part of $e^{ikx}$.
A: You have, for all $x \neq 2k\pi$, 
$$\sum_{k=1}^n \cos(kx) = \mathrm{Re}\left( \sum_{k=1}^n e^{ikx}\right) = \mathrm{Re}\left( e^{ix} \frac{1-e^{inx}}{1-e^{ix}} \right)$$
Moreover you have
$$\left|e^{ix} \frac{1-e^{inx}}{1-e^{ix}}\right| \leq \frac{2}{|1-e^{ix}|}$$
So $$\left| \sum_{k=1}^n \cos(kx) \right| \leq \frac{2}{|1-e^{ix}|}$$
And therefore you get $S_n \rightarrow 0$.
If $x = 2k\pi$ for $k \in \mathbb{Z}$, you easily have $S_n \rightarrow 1$.
A: Another view of the question.
Instead of taking the real part of $e^{ikx}$ as given by the answers from TheSilverDoe and AlexL, the complex exponential can be more symmetric:
$$\begin{align*}
\cos kx &= \frac{e^{-ikx}+e^{ikx}}2\\
\frac12+\sum_{k=1}^n\cos kx &= \frac12\sum_{k=-n}^ne^{ikx}\\
&= \frac12 \cdot e^{-inx}\frac{e^{i(2n+1)x}-1}{e^{ix}-1}\\
&= \frac12 \cdot \frac{e^{i(n+1)x}-e^{-inx}}{e^{ix}-1}\\
&= \frac12 \cdot \frac{e^{i\left(n+\frac12\right)x}-e^{-i\left(n+\frac12\right)x}}{e^{i\frac12x}-e^{-i\frac12x}}\\
&= \frac12 \cdot \frac{\sin\left(n+\frac12\right)x}{\sin\frac12x}\\
S_n&= \frac{\sin\left(n+\frac12\right)x}{2n\sin\frac12x}\\
\end{align*}$$
When $e^{ix} \ne 1$, i.e. $x\ne 2\pi m$ for integer $m$, $S_n \to 0$.

For other $x$'s, i.e. when $x=2\pi m$,
$$\begin{align*}
\cos kx = \cos 2\pi km &= 1\\
\sum_{k=1}^n\cos kx &= n\\
\frac12 + \sum_{k=1}^n\cos kx &= \frac12+ n\\
S_n &= \frac1n\left(\frac12 + n\right)\\
&= \frac1{2n} + 1\\
&\to 1
\end{align*}$$
