# Integration Exercise 2

## Imports



In [33]:

%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 = \int_0^\infty \frac{dx}{x^2 + a^2} = \frac{\pi}{2a}$$


In [34]:

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

assert True # leave this cell to grade the above integral



### Integral 1

$$I_1 = \int_{0}^{\infty} \frac{\sin ^{2}px}{x^{2}}\ dx=\frac{\pi p}{2}$$


In [36]:

def integrand1(x, p):
return np.sin(p*x)**2/(x**2)

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

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

print("Numerical: ", integral_approx1(1.0))
print("Exact    : ", integral_exact1(1.0))




Numerical:  1.5708678849453777
Exact    :  1.5707963267948966




In [37]:

assert True # leave this cell to grade the above integral



### Integral 2

$$I_2 = \int_0^\infty \frac {x}{e^{x}-1}\ dx= \frac {\pi^2}{6}$$


In [38]:

def integrand2(x):
return x/(np.exp(x)-1)

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

def integral_exact2():
return np.pi**2/6

print("Numerical: ", integral_approx2())
print("Exact    : ", integral_exact2())




Numerical:  1.6449340668482264
Exact    :  1.6449340668482264




In [39]:

assert True # leave this cell to grade the above integral



### Integral 3

$$I_3 = \int_0^a \frac{dx}{\sqrt{a^{2}-x^{2}}}=\frac{\pi }{2}$$


In [40]:

def integrand3(x, a):
return 1.0/((a**2-x**2 )**(.5))

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

def integral_exact3(a):
return np.pi/2

print("Numerical: ", integral_approx3(17))
print("Exact    : ", integral_exact3(17))




Numerical:  1.570796326793784
Exact    :  1.5707963267948966




In [41]:

assert True # leave this cell to grade the above integral



### Integral 4

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


In [42]:

def integrand4(x, m, a):
return (x*np.sin(m*x))/(x**2+a**2)

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

def integral_exact4(m, a):
return (np.pi/2)*np.exp(-1*m*a)

print("Numerical: ", integral_approx4(.001,.001))
print("Exact    : ", integral_exact4(.001,.001))




Numerical:  2.209712068886707
Exact    :  1.570794756




In [43]:

assert True # leave this cell to grade the above integral



### Integral 5

$$I_5 = \int_{-\infty}^\infty e^{-x^2}\,dx=\sqrt{\pi}$$


In [44]:

def integrand5(x):
return (np.exp(-1*(x**2)))

def integral_approx5():
# Use the args keyword argument to feed extra arguments to your integrand
I, e = integrate.quad(integrand5, -1*np.inf, np.inf)
return I

def integral_exact5():
return np.pi**(1/2)

print("Numerical: ", integral_approx5())
print("Exact    : ", integral_exact5())




Numerical:  1.7724538509055159
Exact    :  1.7724538509055159




In [45]:

assert True # leave this cell to grade the above integral