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

In [4]:
def X(x):
    return x**2

In [6]:
I,e=integrate.quad(X,0,3)
I


Out[6]:
9.000000000000002

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 [2]:
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 [3]:
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 [4]:
def integrand(x,a):
    return (a**2-x**2)**(1/2)

def integral_approx(a):
    I1,e1=integrate.quad(integrand,0,a,args=(a,))
    return I1

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

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


Numerical:  0.7853981633974481
Exact    :  0.7853981633974483

In [5]:
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 [6]:
def integrand(x):
    return np.sin(x)**2

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

def integral_exact():
    return np.pi/4

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


Numerical:  0.7853981633974483
Exact    :  0.7853981633974483

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

Integral 3

$$ I_3 = \int_0^\infty e^{-ax} cos(bx) dx = \frac{a}{a^2+b^2} $$

In [8]:
def integrand(x,a,b):
    return np.exp(-a*x)*np.cos(b*x)

def integral_approx(a,b):
    I3,e3=integrate.quad(integrand,0,np.inf,args=(a,b,))
    return I3

def integral_exact(a,b):
    return a/(a**2+b**2)

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


Numerical:  0.5
Exact    :  0.5

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

Integral 4

$$ I_4 = \int_0^\infty e^{-ax^2 - b/x^2} dx = \frac{1}{2}\sqrt{\frac{\pi}{a}} e^{-2\sqrt{ab}} $$

In [10]:
def integrand(x,a,b):
    return np.exp(-a*x**2-(b/x**2))

def integral_approx(a,b):
    I4,e4=integrate.quad(integrand,0,np.inf,args=(a,b,))
    return I4

def integral_exact(a,b):
    return (1/2)*(np.pi/a)**(1/2)*np.exp(-2*(a*b)**(1/2))

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


Numerical:  0.11993777196806152
Exact    :  0.119937771968

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

Integral 5

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

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

def integral_approx(a):
    I5,e5=integrate.quad(integrand,0,np.inf,args=(a,))
    return I5

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

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


Numerical:  2.467401100272339
Exact    :  2.4674011002723395

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