Solution:
Key idea: We integrate by parts twice, ∫uvdx=u∫vdx−∫(dxdu∫vdx)dx, each time taking the power of x as the first function.
Step 1 — First use of parts.
We take u=x2 and v=sinx, so dxdu=2x and ∫sinxdx=−cosx.
I=x2(−cosx)−∫2x(−cosx)dx
I=−x2cosx+2∫xcosxdx
Step 2 — Second use of parts.
Now we take u=x and v=cosx, so dxdu=1 and ∫cosxdx=sinx.
∫xcosxdx=xsinx−∫sinxdx
∫xcosxdx=xsinx+cosx
Step 3 — Combine.
We put the result of Step 2 into Step 1 and add the constant.
I=−x2cosx+2(xsinx+cosx)+C
I=−x2cosx+2xsinx+2cosx+C
Therefore, ∫x2sinxdx=−x2cosx+2xsinx+2cosx+C.