# Integration Exercise 2

## Imports



In [3]:

%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
from scipy import integrate



## Indefinite integrals

Here is a table of definite integrals. Many of these integrals has a number of parameters $a$, $b$, etc.

Find five of these integrals and perform the following steps:

1. Typeset the integral using LateX in a Markdown cell.
2. Define an integrand function that computes the value of the integrand.
3. Define an integral_approx funciton that uses scipy.integrate.quad to peform the integral.
4. Define an integral_exact function that computes the exact value of the integral.
5. Call and print the return value of integral_approx and integral_exact for one set of parameters.

Here is an example to show what your solutions should look like:

### Example

Here is the integral I am performing:

$$I_1 = \int_0^\infty \frac{dx}{x^2 + a^2} = \frac{\pi}{2a}$$


In [4]:

def integrand(x, a):
return 1.0/(x**2 + a**2)

def integral_approx(a):
# Use the args keyword argument to feed extra arguments to your integrand
I, e = integrate.quad(integrand, 0, np.inf, args=(a,))
return I

def integral_exact(a):
return 0.5*np.pi/a

print("Numerical: ", integral_approx(1.0))
print("Exact    : ", integral_exact(1.0))




Numerical:  1.5707963267948966
Exact    :  1.5707963267948966




In [ ]:

assert True # leave this cell to grade the above integral



### Integral 1

Here is an integral from the hyperbolic subsection:

\begin{equation*} \int_{0}^{\infty} \frac{\sin ax}{\sinh bx} dx = \frac{\pi}{2b}\tanh \frac{a\pi}{2b} \end{equation*}


In [27]:

def integrand(x,a,b):
return np.sin(a*x)/np.sinh(b*x)

def integrate_approx(a,b):
return I

def integrate_exact(a,b):
return np.pi/(2*b)*np.tanh(a*np.pi/(2*b))

print('Numerical:', integrate_approx(1.0,2.0))
print('Exact:', integrate_exact(1.0,2.0))




Numerical: 0.5150595623143949
Exact: 0.515059562314




In [ ]:

assert True # leave this cell to grade the above integral



### Integral 2

Here is an integral from the exponential functions subsection: \begin{equation*} \int_{0}^{\infty} e^{-ax} \cos bx \space dx = \frac{a}{a^{2}+b^{2}} \end{equation*}



In [11]:

def integrand(x,a,b):
return np.exp(-a*x)*np.cos(b*x)

def integrate_approx(a,b):
return I

def integrate_exact(a,b):
return a/(a**2+b**2)

print('Numerical:', integrate_approx(1.0,2.0))
print('Exact:', integrate_exact(1.0,2.0))




Numerical: 0.19999999999999943
Exact: 0.2




In [ ]:

assert True # leave this cell to grade the above integral



### Integral 3

Here is an integral from the trigonometric functions subsection: \begin{equation*} \int_{0}^{\infty} \frac{1-cospx}{x^{2}} dx = \frac{\pi p}{2} \end{equation*}



In [15]:

def integrand(x,p):
return (1-np.cos(p*x))/x**2

def integrate_approx(p):
return I

def integrate_exact(p):
return p*np.pi/2

print('Numerical:', integrate_approx(4.0))
print('Exact:', integrate_exact(4.0))




Numerical: 6.283702976429656
Exact: 6.283185307179586




In [ ]:

assert True # leave this cell to grade the above integral



### Integral 4

Here is an integral from the logarithmic functions subsection: \begin{equation*} \int_{0}^{\infty} \frac{\ln (a^{2}+x^{2})}{b^{2}+x^{2}} dx = \frac{\pi}{b}ln(a+b) \space \space a,b>0 \end{equation*}



In [25]:

def integrand(x,a,b):
return np.log(a**2+x**2)/(b**2+x**2)

def integrate_approx(a,b):
return I

def integrate_exact(a,b):
return np.pi/b*np.log(a+b)

print('Numerical:', integrate_approx(3.0,4.0))
print('Exact:', integrate_exact(3.0,4.0))




Numerical: 1.5283142572045822
Exact: 1.5283142572




In [ ]:

assert True # leave this cell to grade the above integral



### Integral 5

Here is an integral from the rational and irrational functions subsection: \begin{equation*} \int_{0}^{a} \sqrt{a^{2}-x^{2}} dx = \frac{\pi a^{2}}{4} \end{equation*}



In [29]:

def integrand(x,a,b):
return np.sqrt(a**2-x**2)

def integrate_approx(a,b):
return I

def integrate_exact(a,b):
return np.pi*a**2/4

print('Numerical:', integrate_approx(1.0,2.0))
print('Exact:', integrate_exact(1.0,2.0))




Numerical: 0.7853981633974481
Exact: 0.7853981633974483




In [ ]:

assert True # leave this cell to grade the above integral