Find the upper bound of $\limsup_{M\to\infty}\left|\frac{1}{\sqrt{M}}\sum\limits_{m=1}^{M}\sqrt{m}\cos(m\theta)\right|$ I want to calculate the upper bound of this $\limsup_{M\to\infty}\left|\frac{1}{\sqrt{M}}\sum\limits_{m=1}^{M}\sqrt{m}\cos(m\theta)\right|$. 
There is a constraint that $\theta\neq2n\pi$ where $n$ is integer.
I tried to evaluate the upper bound of this by using the mid point rule, but it doesn't work.   
 A: We can check that the following asymptotic expansion holds:

$$ \sum_{m=1}^{M} \sqrt{m} \cos(m\theta) = \frac{\sqrt{M}}{2} \frac{\sin(M+\tfrac{1}{2})\theta}{\sin(\tfrac{1}{2}\theta)} + \mathcal{O}(1). \tag{1} $$

Numerical calculation also supports the formula (1) as you can see from the graph of the difference between two for first 500 terms (with $\theta = \frac{1}{3}$):

This can be checked heuristically as follows: Formally, the Dirac comb has the following Fourier series
$$ \sum_{n \in \Bbb{Z}} \delta(x - n) = \sum_{n \in \Bbb{Z}} e^{2\pi i n x} = 1 + 2\sum_{n=1}^{\infty} \cos (2\pi n x). \tag{2} $$
Using this, we can write
\begin{align*}
\sum_{m=1}^{M} \sqrt{m}\cos(m\theta)
&= \int_{0}^{M+1/2} \sqrt{x}\cos(\theta x) \left( \sum_{n \in \Bbb{Z}} \delta(x - n) \right) \, dx \\
&= \int_{0}^{M+1/2} \sqrt{x}\cos(\theta x) \, dx  \\
&\qquad + 2\sum_{n=1}^{\infty} \int_{0}^{M+1/2} \sqrt{x}\cos(\theta x)\cos(2\pi n x) \, dx. \tag{3}
\end{align*}
Now define $J_M (\varphi)$ as
$$ \color{blue}{J_M (\varphi) = \int_{0}^{M+1/2} \sqrt{x}\cos(\varphi x) \, dx.} $$
Performing integration by parts, we can write 
$$ J_M (\varphi) = \sqrt{M+\tfrac{1}{2}} \, \frac{\sin(M+\tfrac{1}{2})\varphi}{\varphi} - \int_{0}^{M+1/2} \frac{\sin (\varphi x)}{2\varphi \sqrt{x}} \, dx. $$
Now the latter integral can be dealt with by applying the substitution $\varphi x \mapsto x$ as follows:
$$ \int_{0}^{M+1/2} \frac{\sin (\varphi x)}{2\varphi \sqrt{x}} \, dx
= \frac{1}{2\varphi^{3/2}} \int_{0}^{(M+1/2)\varphi} \frac{\sin x}{\sqrt{x}} \, dx = \mathcal{O}\left(\frac{1}{\varphi^{3/2}} \right). $$
Thus this gives the following asymptotic formula for $J_M(\varphi)$:

$$ J_M (\varphi) = \sqrt{M+\tfrac{1}{2}} \, \frac{\sin(M+\tfrac{1}{2})\varphi}{\varphi} + \mathcal{O}\left(\frac{1}{\varphi^{3/2}}\right). \tag{4} $$

Now using the function $J_M$ it is straightforward to check that (3) can be written as
$$ \color{blue}{\sum_{m=1}^{M} \sqrt{m}\cos(m\theta) = J_M (\theta) + \sum_{n=1}^{\infty} [ J_M(2\pi n+\theta) + J_M(2\pi n-\theta) ].} $$
Plugging (4) to this formula, we get
$$ \sum_{m=1}^{M} \sqrt{m}\cos(m\theta) = \sqrt{M+\tfrac{1}{2}} \left( \frac{1}{\theta} - 2\theta \sum_{n=1}^{\infty} \frac{(-1)^n}{(2\pi n)^2 - \theta^2} \right) \sin(M+\tfrac{1}{2})\theta + \mathcal{O}(1). $$
The infinite summation inside the bracket can be simplified using several techniques such as complex analysis, we can simplify this as (1).
Remark. When turning this argument into a rigorous calculation, the only unclear part is the use of the formal identity (2), but this can be justified by using the following regularized version of (2)
$$ \frac{1-r^2}{1 - 2r\cos(2\pi x) + r^2} = \sum_{n \in \Bbb{Z}} r^{|n|}e^{2\pi i n x} = 1 + 2 \sum_{n=1}^{\infty} r^n \cos(2\pi n x), \quad |r| < 1 $$
and letting $r \uparrow 1$.
