Computing the indefinite integral $\int x^n \sin x\,dx$
$\newcommand{\term}[3]{ \sum_{k=0}^{\lfloor #1/2 \rfloor} (-1)^{#2} x^{#3} \frac{n!}{(#3)!} }$ I am trying to prove that for $n \in\mathbb N$, $$ \int x^n \sin x \, dx = \cos x \term{n}{k+1}{n-2k} + \sin x \term{(n-1)}{k}{n-2k-1} $$ I started with differentiation, and this is what I got:
If we define $f(x)$ as $$ f(x) = \cos x \term{n}{k+1}{n-2k} + \sin x \term{n-1}{k}{n-2k-1} $$ then we have $$ \begin{align*} f’(x) &= \cos x \term{n}{k+1}{n-2k-1} - \sin x \term{n}{k+1}{n-2k} \\ &\qquad + \sin x \term{(n-1)}{k}{n-2k-2} + \cos x \term{(n-1)}{k}{n-2k-1} \\[8pt] &= \cos x \left[ \term{n}{k+1}{n-2k-1} + \term{(n-1)}{k}{n-2k-1} \right] \\ &\qquad + \sin x \left[ \term{(n-1)}{k}{n-2k-2} - \term{n}{k+1}{n-2k} \right] \end{align*} $$ I don't know how to go on, because of the different limits of the sum with $\lfloor{n/2}\rfloor$ and $\lfloor{(n-1)/2}\rfloor$.
Solution 1:
Instead, let's use induction and (iterated) integration by parts. (Note that there should be a "${}+C$ " in that formula, but I'm not going to worry about that.)
First, let's take any $n\ge 1$ and integrate $\int x^n\sin x\,dx$ by parts to see what happens. By the LIATE Rule, we should take $u_1=x^n$ and $dv_1=\sin x\,dx$, giving us $du_1=nx^{n-1}\,dx$ and $v_1=-\cos x$. Then $$\int x^n\sin x\,dx=\int u_1\,dv_1=u_1v_1-\int v_1\,du_1=-x^n\cos x+n\int x^{n-1}\cos x\,dx.$$ The integral on the far right is easy when $n=1$, but if $n\ge 2$ then it's only slightly less problematic than the integral we started with. Still, it's an improvement, so we'll bear it in mind: $$\int x^n\sin x\,dx=-x^n\cos x+n\int x^{n-1}\cos x\,dx\quad\quad\text{for }n\ge 1.\tag{1}$$ Now let's suppose $n\geq 2$, and integrate $\int x^{n-1}\cos x\, dx$ by parts. Take $u_2=x^{n-1}$ and $dv_2=\cos x\,dx$, so $du_2=(n-1)x^{n-2}\, dx$ and $v_2=\sin x$. Then $$\int x^{n-1}\cos x\,dx=\int u_2\,dv_2=u_2v_2-\int v_2\,du_2=x^{n-1}\sin x-(n-1)\int x^{n-2}\sin x\,dx,$$ so by $(1)$, we have $$\begin{align}\int x^n\sin x\, dx &= -x^n\cos x+n\int x^{n-1}\cos x\,dx\\ &= -x^n\cos x+nx^{n-1}\sin x-n(n-1)\int x^{n-2}\sin x\,dx.\end{align}$$ Hence, we've rewritten the original integral in terms of polynomial combinations of $\sin x$ and $\cos x$, together with an integer multiple of an integral much like the one we started with, but with a power of $x$ that is $2$ smaller. This will allow us to make an inductive argument, but with jumps of $2$, so we'll need $2$ base cases instead of $1$. Namely, we'll need base cases $n=1,2$, and we'll induce along the odd $n$ and the even $n$ separately. Let's bear it in mind: $$\int x^n\sin x\, dx=-x^n\cos x+nx^{n-1}\sin x-n(n-1)\int x^{n-2}\sin x\,dx\quad\text{for }n\ge 2.\tag{2}$$
For the $n=1$ case, we can simply use $(1)$ to get $$\int x\sin x\,dx=-x\cos x+\int\cos x\,dx=-x\cos x+\sin x.$$ On the other hand, the following $4$ lines are all equal:
$$\sum_{k=0}^{\lfloor{1/2}\rfloor}(-1)^{k+1}x^{1-2k}{1!\over(1-2k)!}\cos x+\sum_{k=0}^{\lfloor{(1-1)/2}\rfloor}(-1)^kx^{1-2k-1}{1!\over(1-2k-1)!}\sin x$$
$$\sum_{k=0}^0(-1)^{k+1}x^{1-2k}{1\over(1-2k)!}\cos x+\sum_{k=0}^0(-1)^kx^{-2k}{1\over(-2k)!}\sin x$$
$$(-1)^{0+1}x^{1-0}{1\over(1-0)!}\cos x+(-1)^0x^{0}{1\over(0)!}\sin x$$ $$-x\cos x+\sin x,$$ so we're okay in the first base case.
For the $n=2$ case, we can similarly use $(2)$ and calculate the sums explicitly to confirm that the formula holds.
Now, let's do the odd induction. We're considering all $n=2m-1$ ($m\in\Bbb N$). Let's substitute this into the desired formula to get what we're trying to prove in terms of $m$, instead. Observing that that $\lfloor\frac{n}2\rfloor=\lfloor m-\frac12\rfloor=m-1$ and $\lfloor\frac{n-1}2\rfloor=\lfloor m-1\rfloor=m-1,$ we want to show that $$\begin{align}\int x^{2m-1}\sin x\,dx=\sum_{k=0}^{m-1}(-1)^{k+1}x^{2m-1-2k}{(2m-1)!\over(2m-1-2k)!}\cos x\\+\sum_{k=0}^{m-1}(-1)^kx^{2m-2-2k}{(2m-1)!\over(2m-2-2k)!}\sin x\end{align}\tag{3}$$ for all $m\in\Bbb N$. We already know the formula holds in the $m=1$ ($n=1$) case, and by $(2)$, we have $$\begin{align}\int x^{2(m+1)-1}\sin x\, dx=-x^{2(m+1)-1}\cos x+\bigl(2(m+1)-1\bigr)x^{2(m+1)-2}\sin x\\-\bigl(2(m+1)-1\bigr)\bigl(2(m+1)-2\bigr)\int x^{2m-1}\sin x\,dx\end{align}\tag{4}$$ for all $m\geq 1$.
Suppose for some $m$ that $(3)$ holds. Note that for any function $f(x)$, we have $\sum\limits_{k=0}^{m-1}f(k)=\sum\limits_{k=1}^mf(k-1)$. Using this reindexing trick, we have by $(3)$ that $$\begin{align}\int x^{2m-1}\sin x\,dx &=\sum_{k=0}^{m-1}(-1)^{k+1}x^{2m-1-2k}{(2m-1)!\over(2m-1-2k)!}\cos x\\ &{}\qquad+\sum_{k=0}^{m-1}(-1)^kx^{2m-2-2k}{(2m-1)!\over(2m-2-2k)!}\sin x\\ &=\sum_{k=1}^{m}(-1)^{(k-1)+1}x^{2m-1-2(k-1)}{(2m-1)!\over(2m-1-2(k-1))!}\cos x\\ &{}\qquad+\sum_{k=1}^{m}(-1)^{k-1}x^{2m-2-2(k-1)}{(2m-1)!\over(2m-2-2(k-1))!}\sin x\\ &=(-1)^{-1}\sum_{k=1}^{m}(-1)^{k+1}x^{2m+1-2k}{(2m-1)!\over(2m+1-2k))!}\cos x\\ &{}\qquad+(-1)^{-1}\sum_{k=1}^{m}(-1)^{k}x^{2m-2k}{(2m-1)!\over(2m-2k)!}\sin x\\ &=-\sum_{k=1}^{(m+1)-1}(-1)^{k+1}x^{2(m+1)-1-2k}{(2(m+1)-3)!\over(2(m+1)-1-2k))!}\cos x\\ &{}\qquad-\sum_{k=1}^{(m+1)-1}(-1)^{k}x^{2(m+1)-2k}{(2(m+1)-3)!\over(2(m+1)-2-2k)!}\sin x,\end{align}$$ and so $$\begin{align}-\bigl(2(m+1)-1\bigr)\bigl(2(m+1)-2\bigr)\int x^{2m-1}\sin x\,dx =\sum_{k=1}^{(m+1)-1}(-1)^{k+1}x^{2(m+1)-1-2k}{(2(m+1)-1)!\over(2(m+1)-1-2k))!}\cos x\\ {}\qquad+\sum_{k=1}^{(m+1)-1}(-1)^{k}x^{2(m+1)-2k}{(2(m+1)-1)!\over(2(m+1)-2-2k)!}\sin x.\end{align}$$ Thus, since $$-x^{2(m+1)-1}\cos x=(-1)^{0+1}x^{2(m+1)-1-0}{(2(m+1)-1)!\over(2(m+1)-1-0)!}\cos x$$ and $$(2(m+1)-1)x^{2(m+1)-2}\sin x=(-1)^{0}x^{2(m+1)-2-0}{(2(m+1)-1)!\over(2(m+1)-2-0)!}\sin x,$$ we have by $(4)$ and the above work that $$\begin{align}\int x^{2(m+1)-1}\sin x\,dx=\sum_{k=0}^{(m+1)-1}(-1)^{k+1}x^{2(m+1)-1-2k}{(2(m+1)-1)!\over(2(m+1)-1-2k)!}\cos x\\+\sum_{k=0}^{(m+1)-1}(-1)^kx^{2(m+1)-2-2k}{(2(m+1)-1)!\over(2(m+1)-2-2k)!}\sin x,\end{align}$$ and so the desired formula holds in the $m+1$ case, too.
For the even induction, we'll proceed in a similar fashion to the odd induction, except that we'll be considering $n=2m$ for $m\in\Bbb N$.
Solution 2:
I finally got my proof with differentiation finished, too.
If $$f(x) = \sum_{k=0}^{\lfloor{n/2}\rfloor}(-1)^{k+1}x^{n-2k}{n!\over(n-2k)!}\cos x+\sum_{k=0}^{\lfloor{(n-1)/2}\rfloor}(-1)^kx^{n-2k-1}{n!\over(n-2k-1)!}\sin x$$
with $n\in \Bbb N$.
then $$\begin{align}f'(x) &= \left(\sum_{k=0}^{\lfloor{n/2}\rfloor}(-1)^{k+1}x^{n-2k-1}{n!\over(n-2k-1)!}-\sum_{k=0}^{\lfloor{(n-1)/2}\rfloor}(-1)^{k+1}x^{n-2k-1}{n!\over(n-2k-1)!}\right)\cos x\\&{}\quad+\left(\sum_{k=0}^{\lfloor{(n-1)/2}\rfloor}(-1)^{k}x^{n-2k-2}{n!\over(n-2k-2)!}-\sum_{k=0}^{\lfloor{n/2}\rfloor}(-1)^{k+1}x^{n-2k}{n!\over(n-2k)!}\right)\sin x\end{align}$$
$$\begin{align}&= \left(\sum_{k=0}^{\lfloor{n/2}\rfloor}(-1)^{k+1}x^{n-2k-1}{n!\over(n-2k-1)!}-\sum_{k=0}^{\lfloor{(n-1)/2}\rfloor}(-1)^{k+1}x^{n-2k-1}{n!\over(n-2k-1)!}\right)\cos x\\&{}\quad+\left(\sum_{k=2}^{\lfloor{(n-1)/2}\rfloor+2}(-1)^{k}x^{n-2k+2}{n!\over(n-2k+2)!}-\sum_{k=1}^{\lfloor{n/2}\rfloor+1}(-1)^{k}x^{n-2k+2}{n!\over(n-2k+2)!}\right)\sin x\end{align}$$
$$\begin{align}&=\left(\sum_{k=0}^{\lfloor{n/2}\rfloor}(-1)^{k+1}x^{n-2k-1}{n!\over(n-2k-1)!}-\sum_{k=0}^{\lfloor{(n-1)/2}\rfloor}(-1)^{k+1}x^{n-2k-1}{n!\over(n-2k-1)!}\right)\cos x\\ &{}\quad+\left(\sum_{k=2}^{\lfloor{(n-1)/2}\rfloor+2}(-1)^{k}x^{n-2k+2}{n!\over(n-2k+2)!}-\sum_{k=2}^{\lfloor{n/2}\rfloor+1}(-1)^{k}x^{n-2k+2}{n!\over(n-2k+2)!}\right)\sin x\\ &{}\quad+ x^n\sin x\end{align}$$
Now we have to show that (1) $$ \sum_{k=0}^{\lfloor{n/2}\rfloor}(-1)^{k+1}x^{n-2k-1}{n!\over(n-2k-1)!}-\sum_{k=0}^{\lfloor{(n-1)/2}\rfloor}(-1)^{k+1}x^{n-2k-1}{n!\over(n-2k-1)!} = 0$$ and (2) $$\sum_{k=2}^{\lfloor{(n-1)/2}\rfloor+2}(-1)^{k}x^{n-2k+2}{n!\over(n-2k+2)!}-\sum_{k=2}^{\lfloor{n/2}\rfloor+1}(-1)^{k}x^{n-2k+2}{n!\over(n-2k+2)!} = 0$$
For even numbers: $$ \lfloor{n/2}\rfloor = n/2 $$ $$ \lfloor{(n-1)/2}\rfloor = (n-2)/2 $$
For odd numbers: $$ \lfloor{n/2}\rfloor = (n-1)/2 $$ $$ \lfloor{(n-1)/2}\rfloor = (n-1)/2 $$
Also $n!$ is only defined for $$n\ge 0$$
(1) The odd case is trivial because of $$\lfloor{(n-1)/2}\rfloor = \lfloor{n/2}\rfloor$$
Even: We need $$ n -2k -1 \ge 0 $$ so $$ k \le \lfloor{(n-1)/2}\rfloor = (n-2)/2 $$
We get: $$ \sum_{k=0}^{(n-2)/2}(-1)^{k+1}x^{n-2k-1}{n!\over(n-2k-1)!}-\sum_{k=0}^{(n-2)/2}(-1)^{k+1}x^{n-2k-1}{n!\over(n-2k-1)!} = 0$$
With the same argumentation you can show (2).
It remains: $$= 0\cos x+0\sin x+ x^n\sin x= x^n\sin x$$
Solution 3:
Well.. I think I have a different way
$$ \int_a^b e^{izt} dz = \frac{e^{izt} }{it} |_{z=a}^{z=b}$$
Differentiate both sides with $t$ n times (*) and apply leibniz product rule(**) and some rearranging::
$$ \int_a^b z^n e^{izt} dz = \frac{1}{i^{n+1}} \sum_{k=0}^n \frac{1}{t^{k+1}} \binom{n}{k} (-1)^k k! (iz)^{n-k} e^{izt} |_{z=a}^{z=b}$$
Simplfying and putting t=1:
$$ \int_a^b z^n e^{iz} dz= \sum_{k=0}^n \frac{n!}{(n-k)!} (i)^{k-1} z^{n-k} e^{iz} |_{z=a}^{z=b}$$
Let's see if this formula really works, sub in $a=0$ and $b= \frac{\pi}{2}$ and $n=2$:
$$ \int_0^{\frac{\pi}{2}} z^2 e^{iz} dz= \sum_{k=0}^2 \frac{2!}{(2-k)!} (i)^{k-1} z^{2-k} e^{iz} |_{z=0}^{z= \frac{\pi}{2}}= 2!e^{iz} \left[ \frac{-iz^2}{2!} + \frac{z}{1!} + \frac{i}{0!}\right]_{0}^{\frac{\pi}{2} }=e^{iz} \left[ 2i + 2z -iz^2 \right]_0^{\frac{\pi}{2} }= i(2i + \pi - i\frac{\pi^2}{4}) - (2i)=( \frac{\pi^2}{4} -2)+i(\pi -2) $$
Hence, by taking imaginary on both sides we get:
$$ \int_0^{\frac{\pi}{2} } z^2 sin(z) dz = \pi -2$$
And similar results for cosine :)
P.s: I nominate the above result to be called the trigonometric gamma function (incase this wasn't discovered before)
Oh btw I this also gives the results for $x^n sin(ax)$ :P
*: Feynman's trick
**: Leibniz product rule
Solution 4:
Integrate by parts twice, and use two inductions (odd and even case). You may be able to unite these if you are skillful.