Integration Exercise 2

Imports


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

Integral 1

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

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

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

def integral_exact(a):
    return 0.25*np.pi

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


Numerical:  0.7853981633974481
Exact    :  0.7853981633974483

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

Integral 2

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

In [26]:
def integrand(x):
    return np.sin(x)**2

def integral_approx():
    I, e = integrate.quad(integrand, 0, np.pi/2)
    return I

def integral_exact():
    return 0.25*np.pi

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


Numerical:  0.7853981633974483
Exact    :  0.7853981633974483

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

Integral 3

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

In [66]:
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(10,0))
print("Exact    : ", integral_exact(10,0))


Numerical:  0.6283185307179587
Exact    :  0.628318530718

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

Integral 4

$$ I_4 = \int_0^{\infty} \frac{x}{e^{x}+1} = {\frac{\pi^2}{12}} $$

In [72]:
def integrand(x):
    return x/(np.exp(x)+1)

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

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

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


Numerical:  0.822467033424113
Exact    :  0.8224670334241131

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

Integral 5

$$ I_5 = \int_0^{\infty} \frac{x}{e^{x}-1} = {\frac{\pi^2}{6}} $$

In [74]:
def integrand(x):
    return x/(np.exp(x)-1)

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

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

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


Numerical:  1.6449340668482264
Exact    :  1.6449340668482262

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