I'm struggling to find the sum of $S_n=\cos{x}+2\cos{2x}+3\cos{3x}...+n\cos{nx}$
I know that for $z=e^{ix}$, $2\cos{nx}=z^n+\frac{1}{z^n}$.
So I've tried $2S_n=(z+2z^2+3z^3+...+nz^n)+(\frac{1}{z}+\frac{2}{z^2}+\frac{3}{z^3}+...+\frac{n}{z^n})$ but I don't know how to find the sum of $\sum_{r=1}^nrz^r$ or $\sum_{r=1}^nrz^{-r}$
Is there a way to find that sum? Is the way I'm doing it going in the right direction?
Edit for the answer using Thomas Andrews' comment
$S_n=\frac{d}{dx}\frac{\sin{\frac{n+1}{2}x}\sin{\frac{n}{2}x}}{\sin{\frac{x}{2}}}$
$=\frac{\sin{\frac{x}{2}}(a\sin{bx}\cos{ax}+b\sin{ax}\cos{bx})-\sin{ax}\sin{bx}\cos{\frac{x}{2}}}{\sin^{2}{\frac{x}{2}}}$ where $a=\frac{n+1}{2}$ and $b=\frac{n}{2}$