In [20]:
%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 [21]:
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 [22]:
assert True # leave this cell to grade the above integral
YOUR ANSWER HERE: $$ \LARGE \int_0^\infty e^{-ax} cos(bx) dx = \frac{a}{a^2+b^2} $$
In [23]:
# YOUR CODE HERE
def integrand(x, a, b):
return np.exp(-a*x)*np.cos(b*x)
def integral_approx(a, b):
# Use the args keyword argument to feed extra arguments to your integrand
I, e = integrate.quad(integrand, 0, np.inf, args=(a,b,))
return I
def integral_exact(a, b):
return a/(a**2 + b**2)
print("Numerical: ", integral_approx(1.0, 3.0))
print("Exact : ", integral_exact(1.0,3.0))
In [24]:
assert True # leave this cell to grade the above integral
YOUR ANSWER HERE: $$ \LARGE{ \int_0^\infty \frac{dx}{\sqrt{a^2 - x^2}} = \frac{\pi}{2} }$$
In [41]:
# YOUR CODE HERE
def integrand(x, a):
v = np.sqrt(a**2 - x**2)
return v**-1
def integral_approx(a):
# Use the args keyword argument to feed extra arguments to your integrand
I, e = integrate.quad(integrand, 0, a, args=(a,))
return I
def integral_exact(a):
return 0.5*np.pi
print("Numerical: ", integral_approx(3.0))
print("Exact : ", integral_exact(3.0))
In [26]:
assert True # leave this cell to grade the above integral
YOUR ANSWER HERE: $$ \LARGE{ \int_0^a \sqrt{a^2 - x^2}dx = \frac{\pi a^2}{4} }$$
In [29]:
# YOUR CODE HERE
def integrand(x, a):
return np.sqrt(a**2 - x**2)
def integral_approx(a):
# Use the args keyword argument to feed extra arguments to your integrand
I, e = integrate.quad(integrand, 0, a, args=(a,))
return I
def integral_exact(a):
return 0.25*np.pi*(a**2)
print("Numerical: ", integral_approx(1.0))
print("Exact : ", integral_exact(1.0))
In [28]:
assert True # leave this cell to grade the above integral
YOUR ANSWER HERE: $$ \LARGE \int_0^\infty e^{-ax} sin(bx) dx = \frac{b}{a^2+b^2} $$
In [37]:
# YOUR CODE HERE
def integrand(x, a, b):
return np.exp(-a*x)*np.sin(b*x)
def integral_approx(a, b):
# Use the args keyword argument to feed extra arguments to your integrand
I, e = integrate.quad(integrand, 0, np.inf, args=(a,b,))
return I
def integral_exact(a, b):
return b/(a**2 + b**2)
print("Numerical: ", integral_approx(1.0, 3.0))
print("Exact : ", integral_exact(1.0,3.0))
In [ ]:
assert True # leave this cell to grade the above integral
YOUR ANSWER HERE: $$ \LARGE \int_0^\infty e^{-ax^2} dx = \frac{1}{2} \sqrt{\frac{\pi}{a}} $$
In [38]:
# YOUR CODE HERE
def integrand(x, a):
return np.exp(-a*(x**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.sqrt((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