Integration Exercise 2

Imports


In [13]:
%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:c

$$ I_1 = \int_0^\infty \frac{dx}{x^2 + a^2} = \frac{\pi}{2a} $$

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

Integral 1

$$ I_1=\int_0^\infty \frac{x^{p-1}dx}{1+x}=\frac{\pi}{\sin p\pi}, 0<p<1 $$

In [17]:
def integrand1(x,p):
    return (x**(p-1))/(1+x)

def integral_approx1(p):
    I,e=integrate.quad(integrand1, 0, np.inf, args=(p,))
    return I

def integral_exact1(p):
    return np.pi/(np.sin(p*np.pi))

print('Numerical:', integral_approx1(0.5))
print('Exact:', integral_exact1(0.5))


Numerical: 3.141592653591144
Exact: 3.14159265359

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

Integral 2

$$ I_2=\int_0^a \sqrt{a^{2}-x^{2}}dx=\frac{\pi a^{2}}{4} $$

In [19]:
def integrand2(x,a):
    return (a**2-x**2)**(1/2)

def integral_approx2(a):
    I,e=integrate.quad(integrand2, 0,a,args=(a,))
    return I

def integral_exact2(a):
    return np.pi*(a**2)/4

print('Numerical:', integral_approx2(1.0))
print('Exact:', integral_exact2(1.0))


Numerical: 0.7853981633974481
Exact: 0.7853981633974483

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

Integral 3

$$ I_3=\int_0^\infty \frac{dx}{\sqrt{a^{2}-x^{2}}}=\frac{\pi}{2} $$

In [24]:
def integrand3(x,a):
    return 1/((a**2-x**2)**(1/2))

def integral_approx3(a):
    I,e=integrate.quad(integrand3, 0,np.inf,args=(a,))
    return I

def integral_exact3(a):
    return np.pi/2

#print('Numerical:', integral_approx3(1.0))
#print('Exact:', integral_exact3(1.0))

#this integral seems to be flawed, for there will always be an x which makes the denominator 0 and causes the function being integrated to be discontinuos

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

Integral 4

$$ I_4=\int_0^\infty \frac{\sin^{2}px}{x^{2}}dx=\frac{\pi p}{2} $$

In [27]:
def integrand4(x,p):
    return (np.sin(p*x)**2)/(x**2)

def integral_approx4(p):
    I,e=integrate.quad(integrand4, 0,np.inf,args=(p,))
    return I

def integral_exact4(p):
    return np.pi*p/2

print('Numerical:', integral_approx4(1.0))
print('Exact:', integral_exact4(1.0))


Numerical: 1.5708678849453777
Exact: 1.5707963267948966
/usr/local/lib/python3.4/dist-packages/scipy/integrate/quadpack.py:352: IntegrationWarning: The maximum number of subdivisions (50) has been achieved.
  If increasing the limit yields no improvement it is advised to analyze 
  the integrand in order to determine the difficulties.  If the position of a 
  local difficulty can be determined (singularity, discontinuity) one will 
  probably gain from splitting up the interval and calling the integrator 
  on the subranges.  Perhaps a special-purpose integrator should be used.
  warnings.warn(msg, IntegrationWarning)

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

Integral 5

$$ I_4=\int_0^\infty \frac{1-\cos px}{x^{2}}dx=\frac{\pi p}{2} $$

In [30]:
def integrand5(x,p):
    return (1-np.cos(p*x))/(x**2)

def integral_approx5(p):
    I,e=integrate.quad(integrand5, 0,np.inf,args=(p,))
    return I

def integral_exact5(p):
    return np.pi*p/2

print('Numerical:', integral_approx5(1.0))
print('Exact:', integral_exact5(1.0))


Numerical: 1.5709620201480032
Exact: 1.5707963267948966

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