# Lecture 07

## Integration II:

### Integration by Parts and by Partial Fractions


```python
import numpy as np
import sympy as sp
sp.init_printing()
##################################################
##### Matplotlib boilerplate for consistency #####
##################################################
from ipywidgets import interact
from ipywidgets import FloatSlider
from matplotlib import pyplot as plt

%matplotlib inline

from IPython.display import set_matplotlib_formats
set_matplotlib_formats('svg')

global_fig_width = 10
global_fig_height = global_fig_width / 1.61803399
font_size = 12

plt.rcParams['axes.axisbelow'] = True
plt.rcParams['axes.edgecolor'] = '0.8'
plt.rcParams['axes.grid'] = True
plt.rcParams['axes.labelpad'] = 8
plt.rcParams['axes.linewidth'] = 2
plt.rcParams['axes.titlepad'] = 16.0
plt.rcParams['axes.titlesize'] = font_size * 1.4
plt.rcParams['figure.figsize'] = (global_fig_width, global_fig_height)
plt.rcParams['font.sans-serif'] = ['Computer Modern Sans Serif', 'DejaVu Sans', 'sans-serif']
plt.rcParams['font.size'] = font_size
plt.rcParams['grid.color'] = '0.8'
plt.rcParams['grid.linestyle'] = 'dashed'
plt.rcParams['grid.linewidth'] = 2
plt.rcParams['lines.dash_capstyle'] = 'round'
plt.rcParams['lines.dashed_pattern'] = [1, 4]
plt.rcParams['xtick.labelsize'] = font_size
plt.rcParams['xtick.major.pad'] = 4
plt.rcParams['xtick.major.size'] = 0
plt.rcParams['ytick.labelsize'] = font_size
plt.rcParams['ytick.major.pad'] = 4
plt.rcParams['ytick.major.size'] = 0
##################################################
```

## Wake Up Exercise

$\displaystyle \int_1^2 x~dx=$

$\displaystyle \int_1^{\sqrt[3]3} t^2~dt =$

$\displaystyle \int_2^4 \frac{1}{\sqrt z}~dz=$

## Integration reverses the process of differentiation

### In general:

$${d(a\thinspace x^{n+1})\over dx}=(n+1)a\thinspace~x^n~~~~~
\Rightarrow ~~~~\int_{x_1}^{x_2} a\thinspace x^n~dx = \biggr[{a\over
(n+1)} \thinspace x^{(n+1)}\biggl]_{x_1}^{x_2}$$ 

### Reminder: method 1: integration by substitution

This method can be thought of as an integral version of the chain rule.
Suppose we wish to integrate:

$$I=\int f(g(x))~dx= \int f(u)~dx$$

$$I =\int f(u){dx\over du}~du$$

## Integration Method 2: Integration by Parts

Recall that:
$${d\over dx}f(x)~g(x)=f(x)~g'(x)+ g(x)~f'(x)$$
Integrate and rearrange to get:

$$ \int\limits_a^b
f(x)~g'(x)~dx=\biggr[\;f(x)~g(x)\;\biggl]_a^b-\int\limits_a^b
g(x)~f'(x)~dx $$

 This is known as the formula for
**'integrating by parts'** 

can also be written:

$$\int\limits_a^b u~{dv\over dx}~dx ~~= ~~
\biggr[uv\biggl]^b_a-\int\limits_a^b v~{du\over dx}~dx$$

or

$$\int\limits_a^b u~v'~dx~~ =~~
\biggr[uv\biggl]^b_a-\int\limits_a^b v~u'~dx$$ with $f(x) \equiv u$ and $g(x) \equiv v$

## Example 1:

$$\int\limits_a^b x~\sqrt{(x+1)}~dx=\int\limits_a^b x~(x+1)^{1/2}~dx$$

Choose
$$u=x\qquad\mbox{and}\qquad v'=\sqrt{(x+1)}$$

so that:
$$u'=1\qquad\mbox{and}\qquad v={2 \over 3}(x+1)^{3/2}$$

Choose
$$u=x\qquad\mbox{and}\qquad v'=\sqrt{(x+1)}$$

so that:
$$u'=1\qquad\mbox{and}\qquad v={2 \over 3}(x+1)^{3/2}$$

$$\int\limits_a^b x~\sqrt{(x+1)}~dx =\biggr[x~{2\over 3}(x+1)^{3/2}\biggl]_a^b - \int\limits_a^b 1 \times {2\over 3}(x+1)^{3/2}~dx$$

$$=\biggr[{2\over 3}~x~(x+1)^{3/2}\biggl]_a^b-
\biggr[{2\over 3}\cdot {2\over 5}(x+1)^{5/2}\biggl]_a^b$$

If choose the other option for $u$ and $v'$,
get:

$$\biggr[(x+1)^{1/2}~{x^2\over 2}\biggl]_a^b
- \int\limits_a^b {1\over 2}~{1\over \sqrt{(x+1)}}~{x^2\over
2}~dx$$

The second term is **worse** than the integral we started with!
It's important to choose $u$ and $v$ carefully.
This comes with practice.

## Example 2:

Let us calculate the indefinite integral,

$$\int \ln x~dx$$
 

We can do a bit of a trick.  Let:

$$u=\ln x \quad\Longrightarrow\quad u'={1\over x}\qquad\mbox{and}\qquad v'=1\Longrightarrow v=x$$

Putting these into our equation:
$$\int \ln x~dx\quad=\quad\ln x~\cdot x - \int x~\frac{1}{x}dx\quad=\quad x \ln x - \int 1 dx\quad=\quad x \ln x - x + C$$


```python
x = sp.symbols('x')
sp.integrate(sp.log(x),x)

```

## Example 3: $\int\limits^{\infty}_0 x^3~e^{-x}~dx$:

$$\biggl(\int u\thinspace v'\thinspace dx = [u\thinspace v]-\int v\thinspace u'\thinspace dx\biggr)$$

Choose
$$u=x^3\qquad\mbox{and}\qquad v'=e^{-x}$$

so that:
$$u'=3x^2\qquad\mbox{and}\qquad v=-e^{-x}$$

$$\int\limits^{\infty}_0 x^3~e^{-x}~dx=-\biggr[x^3~e^{-x}\biggl]^{\infty}_0
+\int\limits^{\infty}_0 3x^2~e^{-x}~dx$$

Now apply integration by parts to the right-hand side:

Choose
$$u=3x^2\qquad\mbox{and}\qquad v'=e^{-x}$$

so that:
$$u'=6x\qquad\mbox{and}\qquad v=-e^{-x}$$

$$\int\limits^{\infty}_0 3x^2~e^{-x}~dx=-\biggr[3x^2~e^{-x}\biggl]^{\infty}_0 +\int\limits^{\infty}_0 6x~e^{-x}~dx$$ 

And, once more:

Choose
$$u=6x\qquad\mbox{and}\qquad v'=e^{-x}$$

so that:
$$u'=6\qquad\mbox{and}\qquad v=-e^{-x}$$

$$\int\limits^{\infty}_0 6x~e^{-x}~dx =-\biggr[6x~e^{-x}\biggl]^{\infty}_0 +\int\limits^{\infty}_0 6~e^{-x}~dx$$

$$\Longrightarrow \int\limits^{\infty}_0 6~e^{-x}~dx=
-\biggr[6~e^{-x}\biggl]^{\infty}_0=-6e^{-\infty}+6e^0=6$$

(Since $e^{-\infty}=0$ and $e^0=1$)

The other terms all go to zero:

$$-\bigr[x^3~e^{-x}\bigl]^{\infty}_0 =-{\infty}^3~e^{-\infty} + 0 =0$$

$$-\bigr[3x^2~e^{-x}\bigl]^{\infty}_0 =-3{\infty}^2~e^{-\infty} + 0 =0$$

So, to answer our original question:
$$\int\limits^{\infty}_0 x^3~e^{-x}~dx=6$$


```python
sp.integrate(x**3 * sp.exp(-x),(x,0,sp.oo))

```

and in general:
$$\int\limits^{\infty}_0 x^n~e^{-x}~dx=n!$$

## Example 4. Integrating Trig functions by parts.

Recall that:
$${d\over dx}(\sin x)=\cos x$$

$${d\over dx}(\cos x)=-\sin x$$

$$\int\limits^b_a~\cos x\;e^{-x}~dx = I$$

Choose
$$u=\cos x\qquad\mbox{and}\qquad v'=e^{-x}$$

so that:
$$u'=-\sin x\qquad\mbox{and}\qquad v=-e^{-x}$$

$$I =\biggr[-\cos x~\; e^{-x}\biggl]^b_a
-\int\limits^b_a ~(-)\sin x~(-)e^{-x}~dx$$

$$I =\biggr[-\cos x~\; e^{-x}\biggl]^b_a~~-~~\int\limits^b_a ~(-)\sin x~(-)e^{-x}~dx$$

Choose
$$u=\sin x\qquad\mbox{and}\qquad v'=e^{-x}$$

so that:
$$u'=\cos x\qquad\mbox{and}\qquad v=-e^{-x}$$

$$I =\biggr[-\cos x~\;~e^{-x}\biggl]^b_a~~-~~\biggr[\sin x~(-)e^{-x}\biggl]^b_a ~~+~~\int\limits^b_a ~\cos x~(-)e^{-x}~dx$$

The last term is the integral we started with:
$$\Longrightarrow~~~2~\int\limits^b_a ~\cos x~e^{-x}~dx~ =~\biggr[\sin x~\; e^{-x}\biggl]^b_a~~ -~~\biggr[\cos x~\; e^{-x}\biggl]^b_a$$

## Integration Method 3: Partial Fractions

**Example 7**

$$\int {dx \over (2x+1)(x-5)}$$
$${\rm Let~~~~~} {1 \over (2x+1)(x-5)}={A\over (2x+1)} + {B\over (x-5)}$$

Multiply both sides by $(2x+1)(x-5)$ to get:
$A(x-5)+B(2x+1)=1$ so $Ax-5A+B2x+B=1$

 Equate coefficients of $x$:
$$A+2B=0~~~~{\rm thus~~} A=-2B~~~~~~~~~~\rm (A)$$
Equate units (coefficients of $x^0$):
$$-5A+B=1 ~~{\rm~~so~from~(A):~~} 10B+B=1,~~B={1\over 11}~,~~A=-{2\over 11}$$

Thus:
$$\int {dx \over (2x+1)(x-5)}=-\int {2 dx\over 11(2x+1)} + \int {dx\over 11(x-5)}$$
Now use method of substitution on this: 

$u=2x+1$
and $du/dx=2$ so $dx/du=1/2$
 
 
 $w=x-5$ so $dw/dx=1$ and $dx/dw=1$

$$\int {2 du \over 2 \times 11 \times u} + \int {dw\over 11w}= -{\ln u\over 11} + {\ln w \over 11}$$

$$=-{\ln |2x+1|\over 11} + {\ln |x-5| \over 11}$$


```python
sp.integrate(1/((2*x + 1)*(x - 5)),x) # answer seems different because of the arbitrary constant!

```

912f3690a15a8ab8bf598920b16907bc0eafd7d9
ipynb
Jupyter Notebook
lectures/lecture-07-integration2.ipynb
SABS-R3/2020-essential-maths
5a53d60f1e8fdc04b7bb097ec15800a89f67a047
Apache-2.0
2021-11-27T12:07:13.000Z
2021-11-27T12:07:13.000Z
lectures/lecture-07-integration2.ipynb
SABS-R3/2021-essential-maths
8a81449928e602b51a4a4172afbcd70a02e468b8
Apache-2.0
lectures/lecture-07-integration2.ipynb
SABS-R3/2021-essential-maths
8a81449928e602b51a4a4172afbcd70a02e468b8
Apache-2.0
2020-10-30T17:34:52.000Z
2020-10-30T17:34:52.000Z
Qwen/Qwen-72B
1. YES
2. YES