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

assert True # leave this cell to grade the above integral

"""
Explanation: Integral 1
$$ \int_0^\infty \frac{x^mdx}{1+2x\cos\beta+x^2}=\frac{\pi}{\sin(m\pi)}\frac{\sin(m\beta)}{\sin\beta}$$
End of explanation
"""


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

assert True # leave this cell to grade the above integral

"""
Explanation: Integral 2
$$ \int_0^\infty \frac{\sin(mx)}{x(x^2+a^2)}dx=\frac{\pi}{2a^2}(1-e^ {-ma}) $$
End of explanation
"""


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

assert True # leave this cell to grade the above integral

"""
Explanation: Integral 3
$$ \int_0^\frac{\pi}{2} \frac1{a+b\cos x}dx=\frac{\cos^{-1}(b/a)}{\sqrt{a^2-b^2}}$$
End of explanation
"""


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

assert True # leave this cell to grade the above integral

"""
Explanation: Integral 4
$$ \int_0^\infty \frac{x\sin(mx)}{(x^2+a^2)}dx=\frac{\pi}{2}e^{-ma} $$
End of explanation
"""


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

assert True # leave this cell to grade the above integral

"""
Explanation: Integral 5
$$ \int_0^\infty e^{-ax^2}\cos(bx)dx=\frac1{2}\sqrt{\frac{\pi}{a}}e^{-b^2/4a} $$
End of explanation
"""


