Integration Exercise 2

Imports


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

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

Integral 1

\begin{equation*} I_1 = \int_0^{2\pi} \frac{dx}{1 - 2a\cos x + a^2} = \frac{2\pi}{1 - a^2} ,0 < a < 1 \end{equation*}

In [21]:
def integrand_1(x, a):
    return (1) / (1 - 2 * a * np.cos(x) + a**2)

def integral_approx_1(a):
    I, e = integrate.quad(integrand_1, 0, 2 * np.pi, args = (a,))
    return I

def integral_exact_1(a):
    return (2 * np.pi) / (1 - a**2)

print("Numerical:", integral_approx_1(0.5))
print("Exact:", integral_exact_1(0.5))


Numerical: 8.377580409572781
Exact: 8.377580409572781

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

Integral 2

\begin{equation*} I_2 = \int_0^a \sqrt{a^2 - x^2} dx = \frac {\pi a^2}{4} \end{equation*}

In [22]:
def integrand_2(x, a):
    return sqrt(a**2 - x**2)

def integral_approx_2(a):
    I, e = integrate.quad(integrand_2, 0, a, args = (a,))
    return I

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

print("Numerical:", integral_approx_2(0.5))
print("Exact:", integral_exact_2(0.5))


Numerical: 0.19634954084936201
Exact: 0.19634954084936207

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

Integral 3

\begin{equation*} I_3 = \int_0^\infty \frac{\sin^2 px}{x^2} dx = \frac{\pi p}{2} \end{equation*}

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

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

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

print("Numerical:", integral_approx_3(5))
print("Exact:", integral_exact_3(5))


Numerical: 7.866414060790807
Exact: 7.853981633974483

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

Integral 4

\begin{equation*} I_4 = \int_0^\infty \frac{1 - \cos px}{x^2} dx = \frac{\pi p}{2} \end{equation*}

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

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

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

print("Numerical:", integral_approx_4(10))
print("Exact:", integral_exact_4(10))


Numerical: 15.732828121581713
Exact: 15.707963267948966

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

Integral 5

\begin{equation*} I_5 = \int_0^\infty \frac{\ln x}{x^2 + a^2} dx = \frac{\pi \ln a}{2a} ,a > 0 \end{equation*}

In [33]:
def integrand_5(x, a):
    return (np.log(x)) / (x**2 + a**2)

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

def integral_exact_5(a):
    return ((np.pi) * np.log(a)) / (2 * a)

print("Numerical:", integral_approx_5(25))
print("Exact:", integral_exact_5(25))


Numerical: 0.20224793288454052
Exact: 0.202247932884

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