%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
from scipy import integrate

"""
Explanation: Integration Exercise 2
Imports
End of explanation
"""


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))


assert True # leave this cell to grade the above integral

"""
Explanation: 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:

Typeset the integral using LateX in a Markdown cell.
Define an integrand function that computes the value of the integrand.
Define an integral_approx funciton that uses scipy.integrate.quad to peform the integral.
Define an integral_exact function that computes the exact value of the integral.
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_1 = \int_0^\infty \frac{dx}{x^2 + a^2} = \frac{\pi}{2a} $$
End of explanation
"""


def integrand1(x,a):
    return x/np.sinh(a*x)
def integral_approx1(a):
    I1,e1 = integrate.quad(integrand1,0,np.inf,args = (a,))
    return I1
def integral_exact1(a):
    return np.pi ** 2/(4*a**2)
print("Numerical: ", integral_approx1(1.0))
print("Exact    : ", integral_exact1(1.0))

assert True # leave this cell to grade the above integral

"""
Explanation: Integral 1
$I_{1} = \int_{0}^{\infty}\frac{x}{\sinh ax}dx = \frac{\pi^2}{4a^2}$
End of explanation
"""


def integrand2(x,a):
    return np.log(1+x)/x
def integral_approx2(a):
    I2,e2 = integrate.quad(integrand2,0,1,args = (a,))
    return I2
def integral_exact2(a):
    return np.pi**2/12
print("Numerical: ", integral_approx2(1.0))
print("Exact    : ", integral_exact2(1.0))

assert True # leave this cell to grade the above integral

"""
Explanation: Integral 2
$\int_{0}^{1}\frac{\ln(1+x)}{x}dx = \frac{\pi^2}{12}$
End of explanation
"""


def integrand3(x,a):
    return np.sqrt(a**2-x**2)
def integral_approx3(a):
    I3,e3 = integrate.quad(integrand3,0,a,args = (a,))
    return I3
def integral_exact3(a):
    return np.pi * a**2/4
print("Numerical: ", integral_approx3(1.0))
print("Exact    : ", integral_exact3(1.0))

assert True # leave this cell to grade the above integral

"""
Explanation: Integral 3
$\int_{0}^{a}\sqrt {a^2-x^2}dx = \frac{\pi a^2}{4}$
End of explanation
"""


def integrand4(x,a):
    return (np.sin(a*x))**2/x**2
def integral_approx4(a):
    I4,e4 = integrate.quad(integrand4,0,np.inf,args = (a,))
    return I4
def integral_exact4(a):
    return np.pi*a/2
print("Numerical: ", integral_approx4(1.0))
print("Exact    : ", integral_exact4(1.0))

assert True # leave this cell to grade the above integral

"""
Explanation: Integral 4
$\int_{0}^{\infty}\frac{\sin^2px}{x^2}dx = \frac{\pi p}{2}$
End of explanation
"""


def integrand5(x,a):
    return (1-np.cos(a*x))/x**2
def integral_approx5(a):
    I5,e5 = integrate.quad(integrand5,0,np.inf,args = (a,))
    return I5
def integral_exact5(a):
    return np.pi *a/2
print("Numerical: ", integral_approx5(1.0))
print("Exact    : ", integral_exact5(1.0))

assert True # leave this cell to grade the above integral

"""
Explanation: Integral 5
$\int_{0}^{\infty}\frac{1-\cos px}{x^2} = \frac{\pi p}{2}$
End of explanation
"""


