\(% Differentiation % https://tex.stackexchange.com/a/60546/ \newcommand{\Diff}{\mathop{}\!\mathrm{d}} \newcommand{\DiffFrac}[2]{\frac{\Diff #1}{\Diff #2}} \newcommand{\DiffOp}[1]{\frac{\Diff}{\Diff #1}} \newcommand{\Ndiff}[1]{\mathop{}\!\mathrm{d}^{#1}} \newcommand{\NdiffFrac}[3]{\frac{\Ndiff{#1} #2}{\Diff {#3}^{#1}}} \newcommand{\NdiffOp}[2]{\frac{\Ndiff{#1}}{\Diff {#2}^{#1}}} % Evaluation \newcommand{\LEvalAt}[2]{\left.#1\right\vert_{#2}} \newcommand{\SqEvalAt}[2]{\left[#1\right]_{#2}} % Epsilon & Phi \renewcommand{\epsilon}{\varepsilon} \renewcommand{\phi}{\varphi} % Sets \newcommand{\NN}{\mathbb{N}} \newcommand{\ZZ}{\mathbb{Z}} \newcommand{\QQ}{\mathbb{Q}} \newcommand{\RR}{\mathbb{R}} \newcommand{\CC}{\mathbb{C}} \newcommand{\PP}{\mathbb{P}} \renewcommand{\emptyset}{\varnothing} % Probabililty \DeclareMathOperator{\Cov}{Cov} \DeclareMathOperator{\Corr}{Corr} \DeclareMathOperator{\Var}{Var} \DeclareMathOperator{\Expt}{E} \DeclareMathOperator{\Prob}{P} % Distribution \DeclareMathOperator{\Binomial}{B} \DeclareMathOperator{\Poisson}{Po} \DeclareMathOperator{\Normal}{N} \DeclareMathOperator{\Exponential}{Exp} \DeclareMathOperator{\Geometric}{Geo} \DeclareMathOperator{\Uniform}{U} % Complex Numbers \DeclareMathOperator{\im}{Im} \DeclareMathOperator{\re}{Re} % Missing Trigonometric & Hyperbolic functions \DeclareMathOperator{\arccot}{arccot} \DeclareMathOperator{\arcsec}{arcsec} \DeclareMathOperator{\arccsc}{arccsc} \DeclareMathOperator{\sech}{sech} \DeclareMathOperator{\csch}{csch} \DeclareMathOperator{\arsinh}{arsinh} \DeclareMathOperator{\arcosh}{arcosh} \DeclareMathOperator{\artanh}{artanh} \DeclareMathOperator{\arcoth}{arcoth} \DeclareMathOperator{\arsech}{arsech} \DeclareMathOperator{\arcsch}{arcsch} % UK Notation \DeclareMathOperator{\cosec}{cosec} \DeclareMathOperator{\arccosec}{arccosec} \DeclareMathOperator{\cosech}{cosech} \DeclareMathOperator{\arcosech}{arcosech} % Paired Delimiters \DeclarePairedDelimiter{\ceil}{\lceil}{\rceil} \DeclarePairedDelimiter{\floor}{\lfloor}{\rfloor} \DeclarePairedDelimiter{\abs}{\lvert}{\rvert} \DeclarePairedDelimiter{\ang}{\langle}{\rangle} % Vectors \newcommand{\vect}[1]{\mathbf{#1}} \newcommand{\bvect}[1]{\overrightarrow{#1}} % https://tex.stackexchange.com/a/28213 % \DeclareMathSymbol{\ii}{\mathalpha}{letters}{"10} % \DeclareMathSymbol{\jj}{\mathalpha}{letters}{"11} % \newcommand{\ihat}{\vect{\hat{\ii}}} % \newcommand{\jhat}{\vect{\hat{\jj}}} \newcommand{\ihat}{\textbf{\^{ı}}} \newcommand{\jhat}{\textbf{\^{ȷ}}} \newcommand{\khat}{\vect{\hat{k}}} % Other Functions \DeclareMathOperator{\sgn}{sgn} \DeclareMathOperator{\tr}{tr} % Other Math Symbols \DeclareMathOperator{\modulo}{mod} \newcommand{\divides}{\mid} \newcommand{\notdivides}{\nmid} \newcommand{\LHS}{\text{LHS}} \newcommand{\RHS}{\text{RHS}} \newcommand{\degree}{^{\circ}}\)
We have \begin {align*} \sum _{m = 1}^{n} a_m (b_{m + 1} - b_m) & = \sum _{m = 1}^{n} a_m b_{m + 1} - \sum _{m = 1}^{n} a_m b_m \\ & = -\sum _{m = 0}^{n - 1} b_{m + 1} a_{m + 1} + \sum _{m = 1}^{n} b_{m + 1} a_m \\ & = -\sum _{m = 1}^{n} b_{m + 1} a_{m + 1} + \sum _{m = 1}^{n} b_{m + 1} a_m + a_{n + 1} b_{n + 1} - a_1 b_1 \\ & = a_{n + 1} b_{n + 1} - a_1 b_1 - \sum _{m = 1}^{n} b_{m + 1}(a_{m + 1} - a_m), \end {align*}
as desired.
Let \(a_m = 1\). On one hand, we have \begin {align*} \sum _{m = 1}^{n} a_m (b_{m + 1} - b_m) & = \sum _{m = 1}^{n} \left [\sin (m + 1) x - \sin mx\right ] \\ & = \sum _{m = 1}^{n} 2 \cos \left (\frac {(m + 1)x + mx}{2}\right ) \sin \left (\frac {(m + 1)x - mx}{2}\right ) \\ & = 2 \sum _{m = 1}^{n} \cos \left (m + \frac {1}{2}\right )x \sin \frac {x}{2} \\ & = 2 \sin \frac {x}{2} \sum _{m = 1}^{n} \cos \left (m + \frac {1}{2}\right )x. \end {align*}
On the other hand, we have \begin {align*} \sum _{m = 1}^{n} a_m (b_{m + 1} - b_m) & = a_{n + 1} b_{n + 1} - a_1 b_1 - \sum _{m = 1}^{n} b_{m + 1} (a_{m + 1} - a_m) \\ & = \sin (n + 1) x - \sin x. \end {align*}
Therefore, by rearranging, we have \[ \sum _{m = 1}^{n} \cos \left (m + \frac {1}{2}\right )x = \frac {1}{2} \left [\sin (n + 1)x - \sin x\right ] \cosec \frac {1}{2} x \] as desired.
Let \(a_m = m\), and let \(b_m = \cos \left (m - \frac {1}{2}\right )x\). We have the identity \[ \cos A - \cos B = - 2 \sin \left (\frac {A + B}{2}\right ) \sin \left (\frac {A - B}{2}\right ). \] Therefore, we have \begin {align*} \sum _{m = 1}^{n} a_m (b_{m + 1} - b_m) & = \sum _{m = 1}^{n} m \cdot \left [\cos \left (m + \frac {1}{2}\right )x - \cos \left (m - \frac {1}{2}\right )x\right ] \\ & = \sum _{m = 1}^{n} -2m \sin mx \sin \frac {1}{2}x \\ & = -2 \sin \frac {1}{2} x \sum _{m = 1}^{n} m \sin mx, \end {align*}
and \begin {align*} & \phantom {=} \sum _{m = 1}^{n} a_m (b_{m + 1} - b_m) \\ & = a_{n + 1} b_{n + 1} - a_1 b_1 - \sum _{m = 1}^{n} b_{m + 1} (a_{m + 1} - a_m) \\ & = (n + 1) \cos \left (n + \frac {1}{2}\right )x - 1 \cdot \cos \frac {1}{2} x - \sum _{m = 1}^{n} \cos \left (m + \frac {1}{2}\right )x \cdot 1 \\ & = (n + 1) \cos \left (n + \frac {1}{2}\right )x - \cos \frac {1}{2} x - \sum _{m = 1}^{n} \cos \left (m + \frac {1}{2}\right )x \\ & = (n + 1) \cos \left (n + \frac {1}{2}\right )x - \cos \frac {1}{2} x - \frac {1}{2}\left (\sin (n + 1)x - \sin x\right )\cosec \frac {1}{2}x \\ & = \frac {1}{2} \cosec \frac {1}{2}x \left [2(n + 1) \cos \left (n + \frac {1}{2}\right )x \sin \frac {1}{2}x - 2 \cos \frac {1}{2}x \sin \frac {1}{2}x - \left (\sin (n + 1)x - \sin x\right )\right ] \\ & = \frac {1}{2} \cosec \frac {1}{2}x \left [(n + 1) \left (\sin (n + 1)x - \sin nx\right ) - (\sin x - \sin 0) - \left (\sin (n + 1)x - \sin x\right )\right ] \\ & = \frac {1}{2} \cosec \frac {1}{2}x \left [n \sin (n + 1)x - (n + 1) \sin nx\right ]. \end {align*}
Therefore, we have \begin {align*} -2 \sin \frac {1}{2} x \sum _{m = 1}^{n} m \sin mx & = \frac {1}{2} \cosec \frac {1}{2}x \left [n \sin (n + 1)x - (n + 1) \sin nx\right ] \\ \sum _{m = 1}^{n} m \sin mx & = - \frac {1}{4} \cosec ^2 \frac {1}{2}x \left [n \sin (n + 1)x - (n + 1) \sin nx\right ], \end {align*}
and therefore, \(p = -\frac {1}{4}n\), \(q = \frac {1}{4}(n + 1)\).