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

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

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

Integral 1

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

In [2]:
def integrand(x, p):
    return x**(p-1)/(1+x)
def integral_approx(p):
    I, e = integrate.quad(integrand, 0, np.inf, args=(p,))
    return I
def integral_exact(p):
    return np.pi/np.sin(p*np.pi)

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


Numerical:  3.141592653591144
Exact    :  3.14159265359

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

Integral 2

$$ I = \int_0^{2\pi} \frac{dx}{a+bsin(x)} = \frac{2\pi}{\sqrt{a^2+b^2}}$$

In [5]:
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/(a**2+b**2)**.5

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


Numerical:  inf
Exact    :  8.885765876316732

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

Integral 3

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

In [7]:
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 .5*(np.pi/a)**.5

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


Numerical:  1.2533141373154997
Exact    :  1.2533141373155001

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

Integral 4

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

In [11]:
def integrand(x):
    return np.log(x)/(1+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.8224670334241143
Exact    :  -0.8224670334241132

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

Integral 5

$$I = \int_{-\infty}^\infty \frac{1}{cosh x}dx = \pi $$

In [13]:
def integrand(x):
    return 1/np.cosh(x)
def integral_approx():
    I, e = integrate.quad(integrand, -np.inf, np.inf)
    return I
def integral_exact():
    return np.pi

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


Numerical:  3.1415926535897936
Exact    :  3.141592653589793

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