# Integration Exercise 2

## Imports



In [2]:

%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 [ ]:

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))




In [ ]:

assert True # leave this cell to grade the above integral



### Integral 1

$$\int_0^\infty \frac{x^mdx}{1+2x\cos\beta+x^2}=\frac{\pi}{\sin(m\pi)}\frac{\sin(m\beta)}{\sin\beta}$$


In [25]:

def integrand1(x,m,beta):
return (x**m)/(1+(2*x*np.cos(beta))+x**2)
def integral_approx1(m,beta):
I,e=integrate.quad(integrand1,0,np.inf,args=(m,beta,))
return I
def integral_exact1(m,beta):
return (np.pi*np.sin(m*beta))/(np.sin(m*np.pi)*np.sin(beta))
print("Numerical: ", integral_approx1(2.5,np.pi/4))
print("Exact    : ", integral_exact1(2.5,np.pi/4))




Numerical:  4.104691433375202
Exact    :  4.10468861191




In [ ]:

assert True # leave this cell to grade the above integral



### Integral 2

$$\int_0^\infty \frac{\sin(mx)}{x(x^2+a^2)}dx=\frac{\pi}{2a^2}(1-e^ {-ma})$$


In [27]:

def integrand2(x,m,a):
return np.sin(m*x)/(x*(x**2+a**2))
def integral_approx2(m,a):
I,e=integrate.quad(integrand2,0,np.inf,args=(m,a,))
return I
def integral_exact2(m,a):
return (np.pi/2*a**2)*(1-np.exp(-m*a))
print("Numerical: ", integral_approx2(2.0,2.0))
print("Exact    : ", integral_exact2(2.0,2.0))




Numerical:  0.3855072229134279
Exact    :  6.16810475402




In [ ]:

assert True # leave this cell to grade the above integral



### Integral 3

$$\int_0^\frac{\pi}{2} \frac1{a+b\cos x}dx=\frac{\cos^{-1}(b/a)}{\sqrt{a^2-b^2}}$$


In [18]:

def integrand3(x,a,b):
return 1/(a+b*np.cos(x))
def integral_approx3(a,b):
I,e=integrate.quad(integrand3,0,np.pi/2,args=(a,b,))
return I
def integral_exact3(a,b):
return np.arccos(b/a)/np.sqrt(a**2-b**2)
print("Numerical: ", integral_approx3(np.pi,np.pi/2))
print("Exact    : ", integral_exact3(np.pi,np.pi/2))




Numerical:  0.3849001794597505
Exact    :  0.38490017946




In [ ]:

assert True # leave this cell to grade the above integral



### Integral 4

$$\int_0^\infty \frac{x\sin(mx)}{(x^2+a^2)}dx=\frac{\pi}{2}e^{-ma}$$


In [29]:

def integrand4(x,m,a):
return (x*np.sin(m*x))/(x**2+a**2)
def integral_approx4(m,a):
I,e=integrate.quad(integrand4,0,np.inf,args=(m,a,))
return I
def integral_exact4(m,a):
return (np.pi/2)*np.exp(-m*a)
print("Numerical: ", integral_approx4(1.5,2.0))
print("Exact    : ", integral_exact4(1.5,2.0))




Numerical:  -0.6194941725815885
Exact    :  0.0782053441141




In [ ]:

assert True # leave this cell to grade the above integral



### Integral 5

$$\int_0^\infty e^{-ax^2}\cos(bx)dx=\frac1{2}\sqrt{\frac{\pi}{a}}e^{-b^2/4a}$$


In [23]:

def integrand5(x,a,b):
return np.exp(-a*x**2)*np.cos(b*x)
def integral_approx5(a,b):
I,e=integrate.quad(integrand5,0,np.inf,args=(a,b,))
return I
def integral_exact5(a,b):
return .5*np.sqrt(np.pi/a)*np.exp(-b**2/4*a)
print("Numerical: ", integral_approx5(1.0,1.0))
print("Exact    : ", integral_exact5(1.0,1.0))




Numerical:  0.6901942235215709
Exact    :  0.690194223522




In [ ]:

assert True # leave this cell to grade the above integral