In [2]:
%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 [ ]:
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
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))
In [ ]:
assert True # leave this cell to grade the above integral
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))
In [ ]:
assert True # leave this cell to grade the above integral
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))
In [ ]:
assert True # leave this cell to grade the above integral
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))
In [ ]:
assert True # leave this cell to grade the above integral
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))
In [ ]:
assert True # leave this cell to grade the above integral