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 so that its power drops by one.
Step 1 — First use of parts.
We take u=x2 and v=ex, so dxdu=2x and ∫exdx=ex.
I=x2ex−∫2xexdx
I=x2ex−2∫xexdx
Step 2 — Second use of parts.
Now we take u=x and v=ex, so dxdu=1.
∫xexdx=xex−∫1⋅exdx
∫xexdx=xex−ex
Step 3 — Combine.
We put the result of Step 2 into Step 1 and add the constant.
I=x2ex−2(xex−ex)+C
I=ex(x2−2x+2)+C
Therefore, ∫x2exdx=ex(x2−2x+2)+C.