How to do $\sum_{k=1}^{\infty} \frac{\cos(kx)}{k^2}$? I'm a physicist with no clue how to calculate $S = \sum_{k=1}^{\infty} \frac{\cos(kx)}{k^2}$. 
One handbook says the answer is $\frac{1}{12}(3x^2 - 6 \pi x + 2 \pi^2)$ on the interval $0 \leq x \leq 2\pi$, which would give the value everywhere since the sum is periodic in $2\pi$. 
Altland's Condensed Matter Field Theory gives the value as
$\frac{\pi^2}{6} - \frac{\pi |x|}{2} + \frac{x^2}{4} + \dots$, giving no domain and with a weird absolute value, implying the series is valid everywhere and has higher powers. But how can this be correct? The series is periodic and those first three terms give the complete answer on $0 \leq x \leq 2\pi$, so trying to define the result everywhere with an infinite power series seems senseless to me.
Anyway, can anybody shed light on how to perform the summation? I've tried writing $S$ as a contour integration, like so:
$\frac{1}{2\pi i} \oint dz g(z) \frac{\cos(i z x)}{-z^2} $, where $g(z) = \frac{\beta}{exp(\beta z) - 1}$, a counting function with simple poles at $z = i k$, $k = ..., -2, -1, 0, 1, 2, \dots$, and the contour contains all the poles of $g(z)$ for $k = 1, 2, 3, ...$.
Now, this is not my expertise, but I want to learn. The trick now is to pick a different contour (possibly going off to infinity), such that the integral can be performed. I see that the product $g(z) \frac{\cos(i z x)}{-z^2}$ goes to zero at infinity, but I cannot see how to deform the contour such that the integral becomes tractable.
 A: It's the real part of this function:
$$f(x)=\lim_{\epsilon \to 0}\sum_{k=1}^\infty \frac{e^{ikx-\epsilon k }}{k^2}$$
where $\epsilon \to 0$. Take two derivatives over $x$, apply expansions you can afford because $\epsilon$ is differentially small, compute the sum, integrate back with appropriate boundary conditions (known values for $x=0$).
$$f''(x)=-\lim_{\epsilon \to 0}\sum_{k=1}^\infty e^{ikx-\epsilon k }=\lim_{\epsilon \to 0}\frac{e^{ix-\epsilon}}{e^{ix-\epsilon}-1}$$
$$=\lim_{\epsilon \to 0}\frac{e^{ix-\epsilon}(e^{-ix-\epsilon}-1)}{(e^{ix-\epsilon}-1)(e^{-ix-\epsilon}-1)}$$
$$=\lim_{\epsilon \to 0}\frac{e^{-2\epsilon}-e^{-\epsilon}e^{ix}}{e^{-2\epsilon}-2e^{-\epsilon}\cos x + 1}=\frac{1-e^{ix}}{2(1-\cos x)}$$
$$Re f''(x)=\frac12$$
$$\int \int Re f''(x)dx\,dx=\frac{x^2}{4}+Cx+D$$
We know from Euler's times that $f(0)=\frac{\pi^2}{6}$, which sets $D=\frac{\pi^2}{6}$. For $x=\pi$ you get another known sum $\sum (-1)^k/k^2=-\frac{\pi^2}{12}$ which gives you $C$.

Disclaimer: we can't integrate past singularities of the $f''(x)$ sum, which is ill-defined when $x$ is a multiple of $2\pi$. The integrating constants $C$ and $D$ are different on each subdomain, and the ones above hold between $0$ and $2\pi$. The solution is then periodically extended, which is obvious from the properties of $\cos$ function. Notice a sharp peak (derivative discontinuity) at $x=0$. If you use an absolute value $|x|$ in the linear term (because you know it must be an even function), you only extend the validity to $[-2\pi,2\pi]$ interval. Adding extra series terms is misleading - the error between true values and this expression is not polynomial, it's step-function, and it would be quite strange to choose to write periodic steps as a power series.
A: I had evaluated this exact series a long time ago and just stumbled upon this question and noticed that this method wasn’t given, so here is the way I evaluated it.
Define $$F(x) = \sum_{n=1}^{\infty} \frac{\cos (n x)}{n^2}$$ where $x \in \mathbb{R}$. Since $F(x)$ is $2\pi$-periodic, WLOG, we may confine ourselves to $0<x<2\pi$. By a simple calculation:
\begin{align} F(x) - F(0) &= \int_{0}^{x} \left(-\sum_{n=1}^{\infty} \frac{\sin (n t)}{n}\right)\, dt \\
&= \Im \int_{0}^{x} \left(-\sum_{n=1}^{\infty} \frac{e^{i n t}}{n} \right) \, dt \\
&=\Im \int_{0}^{x} \ln(1-e^{i t}) \, dt \\
&= \Im \int_{0}^{x} \ln \left(2\sin\left(\frac{t}{2}\right)e^{i(t-\pi)/2}\right) \, dt \\
&=\Im \int_{0}^{x} \left[ \ln \left(2\sin\left(\frac{t}{2}\right)\right)+\frac{i(t-\pi)}{2}\right]\, dt \\
&= \int_{0}^{x} \frac{t-\pi}{2} \, dt = \frac{x^2}{4}-\frac{\pi x}{2}\end{align}
Since $F(0)=\frac{\pi^2}{6}$, this implies that $$\boxed{\sum_{n=1}^{\infty} \frac{\cos (n x)}{n^2} = \frac{x^2}{4}-\frac{\pi x}{2}+\frac{\pi^2}{6}}$$
For self-containment, here is a proof that $F(0)=\zeta(2)=\frac{\pi^2}{6}$ via this method:
Notice $\cos(\pi n) = (-1)^{n}$ for $n \in \mathbb{N}$.
Recall the trivial property that $$\eta (s) = \sum_{n=1}^{\infty} \frac{(-1)^{n-1}}{n^s} = (1-2^{1-s})\zeta(s)$$
Take $x \to \pi$:
$$-\sum_{n=1}^{\infty} \frac{(-1)^{n-1}}{n^2} - \sum_{n=1}^{\infty} \frac{1}{n^2} = \frac{\pi^2}{4} - \frac{\pi^2}{2} = -\frac{\pi^2}{4}$$
$$\implies \zeta (2) (2^{-1}-1) - \zeta(2) = -\frac{\pi^2}{4} \implies -\frac{3}{2} \zeta(2) = -\frac{\pi^2}{4}$$ $$\implies \zeta(2) = \frac{\pi^2}{6}$$ $\square$
A: HINT :
Compute the coefficients of the Fourier series for an arc of parabola 
$$y(x)=(x-\pi)^2 \:\:\:\:\text{for}\:\:\:\:0<x<2\pi$$
You will identify $\:\:\sum_{k=1}^\infty\frac{\cos(kx)}{k^2}\:\:$ in it and then found :
$$\sum_{k=1}^\infty\frac{\cos(kx)}{k^2}=\frac{1}{4}(x-\pi)^2-\frac{\pi^2}{12}$$
A: This is not a very rigorous answer at all.
Let us consider $$I=\sum_{k=1}^{\infty} \frac{\cos(kx)}{k^2}\qquad ,\qquad J =\sum_{k=1}^{\infty} \frac{\sin(kx)}{k^2}$$ $$I+iJ=\sum_{k=1}^{\infty} \frac{e^{ikx}}{k^2}=\sum_{k=1}^{\infty} \frac{(e^{ix})^k}{k^2}=\text{Li}_2\left(e^{i x}\right)$$ where appears the polylogarithm function.
So, $$I=\Re\left(\text{Li}_2\left(e^{i x}\right)\right)=\frac{1}{2} \left(\text{Li}_2\left(e^{+i x}\right)+\text{Li}_2\left(e^{-i
   x}\right)\right)$$ $$J=\Im\left(\text{Li}_2\left(e^{i x}\right)\right)=\frac{1}{2} i \left(\text{Li}_2\left(e^{-i x}\right)-\text{Li}_2\left(e^{+i
   x}\right)\right)$$
Since, at this point, being stuck, I used brute force generating a table of $201$ equally spaced values $(0\leq x \leq 2 \pi)$ and performed a quadratic regression which led to $$I=0.25 x^2-1.5708 x+1.64493$$ for a residual sum of squares equal to $1.11\times 10^{-27}$ which means a perfect fit.
The second coefficient is obviously $\frac \pi 2$; concerning the third, if $x=0$ or $x=2\pi$, $I=\frac{\pi^2} 6$. So $$I=\frac 14 x^2-\frac \pi 2 x+\frac{\pi^2} 6$$ which is the formula from the handbook.
I hope and wish that you will get some more founded answers (as you are, I am a physicist and not an real mathematician).
