This related problem, ``\int \log(x) dx``, uses the same idea, though perhaps harder to see at first glance, as setting `dv=dx` is almost too simple to try:
```math
\begin{align*}
u &= \log(x) & dv &= dx\\
du &= \frac{1}{x}dx & v &= x
\end{align*}
```
```math
\begin{align*}
\int \log(x) dx
&= \int u dv\\
&= uv - \int v du\\
&= (\log(x) \cdot x) - \int x \cdot \frac{1}{x} dx\\
&= x \log(x) - \int dx\\
&= x \log(x) - x
\end{align*}
```
Were this a definite integral problem, we would have written:
In fact, it isn't hard to see that an integral of $x^m e^x$, $m$ a positive integer, can be handled in this manner. For example, when $m=10$, `SymPy` gives:
The visual interpretation of integration by parts breaks area into two pieces, the one labeled "B" looks like it would be labeled "A" for an inverse function for $f$. Indeed, integration by parts gives a means to possibly find antiderivatives for inverse functions.
Let $uv = x f^{-1}(x)$. Then we have $[uv]' = u'v + uv' = f^{-1}(x) + x [f^{-1}(x)]'$.
So, up to a constant ``uv = \int [uv]'dx = \int f^{-1}(x) + \int x [f^{-1}(x)]'``. Re-expressing gives:
```math
\begin{align*}
\int f^{-1}(x) dx
&= xf^{-1}(x) - \int x [f^{-1}(x)]' dx\\
&= xf^{-1}(x) - \int f(u) du.\\
\end{align*}
```
The last line follows from the $u$-substitution:
$u=f^{-1}(x)$ for then $du = [f^{-1}(x)]' dx$ and $x=f(u)$.
We use this to find an antiderivative for $\sin^{-1}(x)$:
```math
\begin{align*}
\int \sin^{-1}(x) dx &= x \sin^{-1}(x) - \int \sin(u) du \\
&= x \sin^{-1}(x) + \cos(u) \\
&= x \sin^{-1}(x) + \cos(\sin^{-1}(x)).
\end{align*}
```
Using right triangles to simplify, the last value
$\cos(\sin^{-1}(x))$ can otherwise be written as $\sqrt{1 - x^2}$.
##### Example
The [trapezoid](http://en.wikipedia.org/wiki/Trapezoidal_rule) rule is an approximation to the definite integral like a
Riemann sum, only instead of approximating the area above
$[x_i, x_i + h]$ by a rectangle with height $f(c_i)$ (for some $c_i$),
it uses a trapezoid formed by the left and right endpoints. That is,
this area is used in the estimation: $(1/2)\cdot (f(x_i) + f(x_i+h))
\cdot h$.
Even though we suggest just using `quadgk` for numeric integration,
estimating the error in this approximation is still of some
theoretical interest.
Recall, just using *either* $x_i$ or $x_{i-1}$
for $c_i$ gives an error that is "like" $1/n$, as $n$ gets large, though the exact rate depends on the function and the length of the interval.
This [proof](http://www.math.ucsd.edu/~ebender/20B/77_Trap.pdf) for the
error estimate is involved, but is reproduced here, as it nicely
integrates many of the theoretical concepts of integration discussed so far.
First, for convenience, we consider the interval $x_i$ to $x_i+h$. The
actual answer over this is just $\int_{x_i}^{x_i+h}f(x) dx$. By a
$u$-substitution with $u=x-x_i$ this becomes $\int_0^h f(t + x_i)
dt$. For analyzing this we integrate once by parts using $u=f(t+x_i)$ and
$dv=dt$. But instead of letting $v=t$, we choose to add - as is our prerogative - a constant of integration
This gives the bound: $\vert \text{error}_i \rvert \leq K h^3/12$. The
*total* error may be less, but is not more than the value found by
adding up the error over each of the $n$ intervals. As our bound does not
depend on the $i$, we have this sum satisfies:
```math
\lvert \text{error}\rvert \leq n \cdot \frac{Kh^3}{12} = \frac{K(b-a)^3}{12}\frac{1}{n^2}.
```
So the error is like $1/n^2$, in contrast to the $1/n$ error of the
Riemann sums. One way to see this, for the Riemann sum it takes twice
as many terms to half an error estimate, but for the trapezoid rule
only $\sqrt{2}$ as many, and for Simpson's rule, only $2^{1/4}$ as
many.
## Area related to parameterized curves
The figure introduced to motivate the integration by parts formula also suggests that areas described parametrically (by a pair of functions ``x=u(t), y=v(t)`` for ``a \le t \le b``) can have their area computed.
When ``u(t)`` is strictly *increasing*, and hence having an inverse function, then re-parameterizing by ``\phi(t) = u^{-1}(t)`` gives a ``x=u(u^{-1}(t))=t, y=v(u^{-1}(t))`` and integrating this gives the area by ``A=\int_a^b v(t) u'(t) dt``
However, the correct answer requires understanding a minus sign. Consider the area enclosed by ``x(t) = \cos(t), y(t) = \sin(t)``:
We added a rectangle for a Riemann sum for ``t_i = \pi/3`` and ``t_{i+1} = \pi/3 + \pi/8``. The height of this rectangle if ``y(t_i)``, the base is of length ``x(t_i) - x(t_{i+1})`` *given* the orientation of how the circular curve is parameterized (counter clockwise here).
Taking this Riemann sum approach, we can approximate the area under the curve parameterized by ``(u(t), v(t))`` over the time range ``[t_i, t_{i+1}]`` as a rectangle with height ``y(t_i)`` and base ``x(t_{i}) - x(t_{i+1})``. Then we get, as expected:
```math
\begin{align*}
A &\approx \sum_i y(t_i) \cdot (x(t_{i}) - x(t_{i+1}))\\
So with a counterclockwise rotation, the actual answer for the area includes a minus sign. If the area is traced out in a *clockwise* manner, there is no minus sign.
This is a case of [Green's Theorem](https://en.wikipedia.org/wiki/Green%27s_theorem#Area_calculation) to be taken up in [Green's Theorem, Stokes' Theorem, and the Divergence Theorem](file:///Users/verzani/julia/CalculusWithJulia/html/integral_vector_calculus/stokes_theorem.html).
##### Example
Apply the formula to a parameterized circle to ensure, the signed area is properly computed. If we use ``x(t) = r\cos(t)`` and ``y(t) = r\sin(t)`` then we have the motion is counterclockwise:
([Galileo](https://mathshistory.st-andrews.ac.uk/Curves/Cycloid/) was thwarted in finding this answer exactly and resorted to constructing one from metal to *estimate* the value.)
##### Example
Consider the example ``x(t) = \cos(t) + t\sin(t), y(t) = \sin(t) - t\cos(t)`` for ``0 \leq t \leq 2\pi``.
Find the value of $\int_1^4 x \log(x) dx$. You can integrate by parts.
```julia; hold=true; echo=false
f(x) = x*log(x)
a,b = 1,4
val,err = quadgk(f, a, b)
numericq(val)
```
###### Question
Find the value of $\int_0^{\pi/2} x\cos(2x) dx$. You can integrate by parts.
```julia; hold=true; echo=false
f(x) = x*cos(2x)
a,b = 0, pi/2
val,err = quadgk(f, a, b)
numericq(val)
```
###### Question
Find the value of $\int_1^e (\log(x))^2 dx$. You can integrate by parts.
```julia; hold=true; echo=false
f(x) = log(x)^2
a,b = 1,exp(1)
val,err = quadgk(f, a, b)
numericq(val)
```
###### Question
Integration by parts can be used to provide "reduction" formulas, where an antiderivative is written in terms of another antiderivative with a lower power. Which is the proper reduction formula for $\int (\log(x))^n dx$?
```julia; hold=true; echo=false
choices = [
"``x(\\log(x))^n - n \\int (\\log(x))^{n-1} dx``",