In [3]:
    
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
from scipy import integrate
    
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:
integrand function that computes the value of the integrand.integral_approx funciton that uses scipy.integrate.quad to peform the integral.integral_exact function that computes the exact value of the integral.integral_approx and integral_exact for one set of parameters.Here is an example to show what your solutions should look like:
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))
    
    
In [5]:
    
assert True # leave this cell to grade the above integral
    
In [28]:
    
# YOUR CODE HERE
def integrand1(x, a, b, c):
    return np.exp(-(a*x**2 + b*x + c))
def integral1_approx(a, b, c):
    I, e = integrate.quad(integrand1, -np.inf, np.inf, args=(a,b,c,))
    return I
def integral1_exact(a, b, c):
    return np.sqrt(np.pi/a) * np.exp((b**2 - 4*a*c)/(4*a))
print("Numerical: ", integral1_approx(1.0, 1.0, 1.0))
print("Exact:     ", integral1_exact(1.0, 1.0, 1.0))
    
    
In [14]:
    
assert True # leave this cell to grade the above integral
    
In [15]:
    
# YOUR CODE HERE
def integrand2(x, p):
    
    return (np.sin(p*x))**2
def integral2_approx(p):
    I, e = integrate.quad(integrand2, 0, 0.5*np.pi, args=(p,))
    return I
def integral2_exact(p):
        return 0.25*np.pi
    
print("Numerical: ", integral2_approx(1.0))
print("Exact:     ", integral2_exact(1.0))
#numerical result is around 10* actual result
    
    
In [16]:
    
assert True # leave this cell to grade the above integral
    
In [18]:
    
def integrand3(x, p):
    return (1 - np.cos(p*x))/x**2
def integral3_approx(p):
    I, e = integrate.quad(integrand3, 0, np.inf, args=(p,))
    return I
def integral3_exact(p):
    return 0.5*p*np.pi
print("Numerical: ", integral3_approx(3.0))
print("Exact:     ", integral3_exact(3.0))
    
    
In [ ]:
    
assert True # leave this cell to grade the above integral
    
In [20]:
    
def integrand4(x):
    return np.log(x)/(1 + x)
def integral4_approx():
    I, e = integrate.quad(integrand4, 0, 1)
    return I
def integral4_exact():
    return -(np.pi**2)/12
print("Numerical: ", integral4_approx())
print("Exact:     ", integral4_exact())
    
    
In [ ]:
    
assert True # leave this cell to grade the above integral
    
In [22]:
    
def integrand5(x, a):
    return x/np.sinh(a*x)
def integral5_approx(a):
    I, e = integrate.quad(integrand5, 0, np.inf, args=(a,))
    return I
def integral5_exact(a):
    return (np.pi**2)/(4*a**2)
print("Numerical: ", integral5_approx(2.0))
print("Exact:     ", integral5_exact(2.0))
    
    
In [ ]:
    
assert True # leave this cell to grade the above integral