Integration Exercise 2

Imports


In [67]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
from scipy import integrate

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 [68]:
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 [69]:
assert True # leave this cell to grade the above integral

Integral 1

$$ I_1 = \int_0^\frac{\pi}{2} \sin^2{x} {dx} = \frac{\pi}{4} $$

In [70]:
def integrand(x, a):
    return np.sin(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.pi/2, args=(a,))
    return I

def integral_exact(a):
    return np.pi/4

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


Numerical:  0.7853981633974483
Exact    :  0.7853981633974483

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

Integral 2

$$ I_2 = \int_0^\infty \frac{x\sin{mx}}{x^2 + a^2} {dx} = \frac{\pi}{2} e^{-ma} $$

In [127]:
def integrand(x, m, a):
    return (x*np.sin((m*x)))/(x**2 + a**2)

def integral_approx(m, a):
    # Use the args keyword argument to feed extra arguments to your integrand
    I, e = integrate.quad(integrand, 0, np.inf, args=(m,a,))
    return I

def integral_exact(m, a):
    return ((np.pi)*.5) *(np.exp(-1*m*a))

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


Numerical:  1.2549750860426012
Exact    :  0.577863674895

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

Integral 3

$$ I_3 = \int_0^\frac{\pi}{2} \sin{ax^2} {dx} = \frac{1}{2} \sqrt{\frac{\pi}{2 \pi}} $$

In [120]:
def integrand(a,x):
    return np.sin((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.pi/2), args=(a))
    return I

def integral_exact(a):
    return .5*(np.sqrt((np.pi)/np.pi*2*np.pi))

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


Numerical:  0.9999999999999999
Exact    :  1.25331413732

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

Integral 4

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

In [125]:
def integrand(a, x, 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,1.0))
print("Exact    : ", integral_exact(1.0,1.0))


Numerical:  0.5403023058681399
Exact    :  0.5

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

Integral 5

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

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


Numerical:  0.8862269254527579
Exact    :  0.886226925453

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