TITLE: Integrating $\sin(x)x^2$ by parts, why do we only add $C$ at the end?
QUESTION [5 upvotes]: Let $f(x)=\sin(x)x^2$.
If you were to do integration by parts you would get:
$$\int \sin(x)x^2dx=\int\sin(x)dx\times x^2-\iint\sin(x)dx\times\frac{d}{dx}x^2dx$$
$$\int \sin(x)x^2dx=-\cos(x)x^2-\int-2\cos(x)xdx$$
$$\int\sin(x)x^2dx=-\cos(x)x^2+2(\int\cos(x)dx\times x-\iint\cos(x)dx\times\frac{d}{dx}xdx)$$
$$\int\sin(x)x^2dx=-\cos(x)x^2+2(\sin(x)x+\cos(x))+C$$
The part that I don't understand is why do we add the constant only at the end? For example, in row #$1$ you need to find the $\int \sin(x)dx$.  We take it to be $-\cos(x)$ but in reality, it is $-\cos(x)+C_1$. If you were to compute this with $\int \sin(x)dx=-\cos(x)+C_1$, you would get the wrong result because $\int \sin(x)dx$ needs to be integrated again, and $C_1$ would turn in $x$, therefore you would get the wrong result. How do we know that $C_1=0$? I get that the solution will be correct that way, but why?

REPLY [2 votes]: Let's keep those $C_{i}$'s in those steps and show you that things still cancel out:
$$\int{x^{2}sinxdx}=-x^{2}cosx+C_{1}x^{2}-2C_{1}\int{xdx}+2\int{xcosxdx}$$
We get
$$\int{x^{2}sinxdx}=-x^{2}cosx+C_{1}x^{2}-2C_{1}\int{xdx}+2xsinx+2C_{2}x-2\int{sinxdx}-2C_{2}\int{dx}$$
Then 
$$\int{x^{2}sinxdx}=-x^{2}cosx+C_{1}x^{2}-C_{1}(2)\frac{x^{2}}{2}+C_{3}+2xsinx+2xC_{2}+2cosx+C_{4}-2C_{2}x+C_{5}$$
Collection of like terms and letting $C=C_{3}+C_{4}+C_{5}$, we get
$$\int{x^{2}sinxdx}=-x^{2}cosx+2xsinx+2cosx+C$$

REPLY [2 votes]: For $f $ and $ g$ $ C^1$ at some intervalle,
The integration by parts is based on the identity
$$(fg)'=f'g + fg'$$
which yields to
$$\int f'g = fg - \int fg'$$
If you write
$$\int f'g = (f+C_1)g - \int fg'$$
the result will be false.
You should write
$$\int f'g =  (f+C_1)g - \int (f+C_1)g'$$
To satisfy the first identity.