In [14]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
from scipy import integrate
import math as m
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 [15]:
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 [16]:
assert True # leave this cell to grade the above integral
Here is the integral I am performing:
$$ I_1 = \int_0^\frac{\pi}{2} sin^2 x dx = \frac{\pi}{4} $$
In [17]:
# YOUR CODE HERE
# raise NotImplementedError()
def integrand(x):
return (np.sin(x))**2
def integral_approx(a):
I, err = integrate.quad(integrand, 0, np.pi)
return I
def integral_exact(a):
return np.pi/4
print("Numerical: ", integral_approx(1.0))
print("Exact : ", integral_exact(1.0))
In [18]:
assert True # leave this cell to grade the above integral
In [19]:
# YOUR CODE HERE
def integrand(x, p):
return ((np.sin(p*x))**2)/x
def integral_approx(p):
# Use the args keyword argument to feed extra arguments to your integrand
I, e = integrate.quad(integrand, 0, np.inf, args=(p,))
return I
def integral_exact(p):
return 0.5*np.pi*p/2
print("Numerical: ", integral_approx(1.0))
print("Exact : ", integral_exact(1.0))
In [20]:
assert True # leave this cell to grade the above integral
In [21]:
# YOUR CODE HERE
def integrand(x, p):
return (1.0-np.cos(p*x))/x
def integral_approx(p):
# Use the args keyword argument to feed extra arguments to your integrand
I, e = integrate.quad(integrand, 0, np.inf, args=(p,))
return I
def integral_exact(p):
return 0.5*np.pi*p/2
print("Numerical: ", integral_approx(1.0))
print("Exact : ", integral_exact(1.0))
In [22]:
assert True # leave this cell to grade the above integral
In [23]:
# YOUR CODE HERE
def integrand(x):
return x/(np.exp(x)-1)
def integral_approx(a):
I, err = integrate.quad(integrand, 0, np.inf)
return I
def integral_exact(a):
return np.pi**2/6
print("Numerical: ", integral_approx(1.0))
print("Exact : ", integral_exact(1.0))
In [24]:
assert True # leave this cell to grade the above integral
In [25]:
# YOUR CODE HERE
# YOUR CODE HERE
def integrand(x):
return x/(np.exp(x)+1)
def integral_approx(a):
I, err = integrate.quad(integrand, 0, np.inf)
return I
def integral_exact(a):
return np.pi**2/12
print("Numerical: ", integral_approx(1.0))
print("Exact : ", integral_exact(1.0))
In [70]:
assert True # leave this cell to grade the above integral