Integration Exercise 2

Imports


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

Integral 1

$$ I = \int_{-\infty}^\infty e^{-(ax^2 + bx + c)} dx = \sqrt{\frac{\pi}{2}} e^\frac{b^2-4ac}{4a} $$

In [28]:
# YOUR CODE HERE
def integrand1(x, a, b, c):
    return np.exp(-(a*x**2 + b*x + c))

def integral1_approx(a, b, c):
    I, e = integrate.quad(integrand1, -np.inf, np.inf, args=(a,b,c,))
    return I

def integral1_exact(a, b, c):
    return np.sqrt(np.pi/a) * np.exp((b**2 - 4*a*c)/(4*a))

print("Numerical: ", integral1_approx(1.0, 1.0, 1.0))
print("Exact:     ", integral1_exact(1.0, 1.0, 1.0))


Numerical:  0.8372479154447754
Exact:      0.837247915445

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

Integral 2

$$ I = \int_0^\frac{\pi}{2} \sin^2 px dx = \frac{\pi}{4} $$

In [15]:
# YOUR CODE HERE
def integrand2(x, p):
    
    return (np.sin(p*x))**2

def integral2_approx(p):
    I, e = integrate.quad(integrand2, 0, 0.5*np.pi, args=(p,))
    return I

def integral2_exact(p):
        return 0.25*np.pi

    
print("Numerical: ", integral2_approx(1.0))
print("Exact:     ", integral2_exact(1.0))
#numerical result is around 10* actual result


Numerical:  0.7853981633974483
Exact:      0.7853981633974483

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

Integral 3

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

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

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

def integral3_exact(p):
    return 0.5*p*np.pi

print("Numerical: ", integral3_approx(3.0))
print("Exact:     ", integral3_exact(3.0))


Numerical:  4.713393375355865
Exact:      4.71238898038469

In [ ]:
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 [20]:
def integrand4(x):
    return np.log(x)/(1 + x)

def integral4_approx():
    I, e = integrate.quad(integrand4, 0, 1)
    return I

def integral4_exact():
    return -(np.pi**2)/12

print("Numerical: ", integral4_approx())
print("Exact:     ", integral4_exact())


Numerical:  -0.8224670334241143
Exact:      -0.8224670334241132

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

Integral 5

$$ I = \int_0^\infty \frac{x}{\sinh ax} dx = \frac{\pi^2}{4a^2} $$

In [22]:
def integrand5(x, a):
    return x/np.sinh(a*x)

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

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

print("Numerical: ", integral5_approx(2.0))
print("Exact:     ", integral5_exact(2.0))


Numerical:  0.6168502750680847
Exact:      0.6168502750680849

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