Integration Exercise 2

Imports


In [1]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
from scipy import integrate
import math                             # From https://docs.python.org/3.3/library/math.html

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:

  1. Typeset the integral using LateX in a Markdown cell.
  2. Define an integrand function that computes the value of the integrand.
  3. Define an integral_approx funciton that uses scipy.integrate.quad to peform the integral.
  4. Define an integral_exact function that computes the exact value of the integral.
  5. 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} $$

In [2]:
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))


Numerical:  1.5707963267948966
Exact    :  1.5707963267948966

In [3]:
assert True # leave this cell to grade the above integral

Integral 1

$$ \large I_A = \int_0^\infty e^{-ax^2} dx = \frac{1}{2}\sqrt{\frac{\pi}{a}} $$

In [4]:
def integrand(x,a):
    return np.exp((-a)*(x**2))

def integral_approx(a):
    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))


Numerical:  0.8862269254527579
Exact    :  0.886226925453

In [5]:
assert True # leave this cell to grade the above integral

Integral 2

$$ \large I_B = \int_0^\infty e^{-ax}\cos{bx} dx = \frac{a}{a^2+b^2} $$

In [6]:
def integrand(x,a,b):
    return np.exp(-a*x)*np.cos(b*x)

def integral_approx(a,b):
    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,2.0))
print("Exact    : ", integral_exact(1.0,2.0))


Numerical:  0.19999999999999943
Exact    :  0.2

In [7]:
assert True # leave this cell to grade the above integral

Integral 3

$$ \large I_C = \int_0^1 x^m(\ln{x})^n dx = \frac{(-1)^nn!}{(m+1)^{n+1}} \quad m>-1,n=0,1,2,...$$

In [8]:
math.factorial(5)


Out[8]:
120

In [9]:
def integrand(x,m,n):
    return (x**m)*((np.log(x))**n)

def integral_approx(m,n):
    I, e = integrate.quad(integrand, 0, 1, args=(m,n,))
    return I

def integral_exact(m,n):
    return ((-1)**n*math.factorial(n))/((m+1)**(n+1))

print("Numerical: ", integral_approx(1.0,2.0))
print("Exact    : ", integral_exact(1.0,2.0))


Numerical:  0.25000000000000006
Exact    :  0.25

In [10]:
assert True # leave this cell to grade the above integral

Integral 4

$$ \large I_D = \int_0^{2\pi} \frac{dx}{a+b\sin{x}} = \frac{2\pi}{\sqrt{a^2-b^2}} $$

In [11]:
def integrand(x,a,b):
    return 1/(a+(b*np.sin(x)))

def integral_approx(a,b):
    I, e = integrate.quad(integrand, 0, 2*np.pi, args=(a,b,))
    return I

def integral_exact(a,b):
    return 2*np.pi/(np.sqrt((a**2)-(b**2)))
                    
print("Numerical: ", integral_approx(2.0,1.0))
print("Exact    : ", integral_exact(2.0,1.0))


Numerical:  3.6275987284684357
Exact    :  3.62759872847

In [12]:
assert True # leave this cell to grade the above integral

Integral 5

$$ \large I_E = \int_0^1 \frac{\ln{(1+x)}}{x} dx = \frac{\pi^2}{12} $$

In [13]:
def integrand(x):
    return np.log(1+x)/x

def integral_approx():
    I, e = integrate.quad(integrand, 0, 1)
    return I

def integral_exact():
    return (np.pi**2)/12

print("Numerical: ", integral_approx())
print("Exact    : ", integral_exact())


Numerical:  0.8224670334241132
Exact    :  0.8224670334241132

In [14]:
assert True # leave this cell to grade the above integral