Differentiating under integral sign -- trig counterexample I really hate integration by parts, so when faced with $\int_{-\pi}^\pi x^2 \cos n x \, dx$ I tried writing it as
$$\int_{-\pi}^\pi x^2 \cos n x \, dx =
\frac{d}{dn} \int_{-\pi}^\pi x \sin n x \, dx = 
\frac{d^2}{dn^2} \int_{-\pi}^\pi  \cos n x \, dx $$1
I have done something wrong.  The integrand is continuously differentiable with respect to n and I thought that was enough.  How can I get differentiating under the integral sign to work?
 A: It did work (except you are missing a negative sign).   Remember $n$ is not always an integer, so that $$-\int_{-\pi}^\pi \cos(nx)dx=-\frac{2\sin(\pi n)}{n}.$$
Then $$\frac{d^2}{dn^2} \left(-\int_{-\pi}^\pi \cos(nx)dx\right) =\frac{d}{dn} \left( -\frac{2\pi\cos (\pi n)}{n}+\frac{2\sin(\pi n)}{n^2}\right)$$
$$=\frac{2\pi^2\sin(\pi n)}{n}-\frac{4\sin(\pi n)}{n^3}+\frac{4\pi\cos(\pi n)}{n^2}.$$
A: Well, if there are cases in which you can't avoid integration by parts, then please use the below formula:


*

*If $u$ and $v$ are functions of $x$ and dashes denote differentiation and suffixes integration with respect to $x$, then $$\small\int uv \ dx = uv_{1} -u'v_{2}+ u''v_{3} - u'''v_{4} + \cdots + (-1)^{n-1}u^{(n-1)}v_{n} + (-1)^{n} \int u^{(n)}\cdot v_{n} \ dx$$

A: Differentiating under the integral sign is fine here, but for such situations ("a polynomial times a trig function") I prefer to use this method.  The basic idea is that you know ahead of time how integration by parts will affect the sign of each consecutive integral, so clever bookkeeping saves you from having to use excessive parentheses and making errors.
