Integration Exercise 3

Imports


In [1]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
from scipy import integrate
from scipy.integrate import dblquad

2d polar integration

The 2d polar integral of a scalar function $f(r, \theta)$ is defined as:

$$ I(r_{max}) = \int_0^{r_{max}} \int_0^{2\pi} f(r, \theta) r d\theta $$

Write a function integrate_polar(f, rmax) that performs this integral numerically using scipy.integrate.dblquad.


In [61]:
def integrate_polar(f, rmax):

    from scipy.integrate import dblquad

#NOTE: the order of arguments matters - inner to outer
    integrand = lambda x,y: x

    ymin = 0
    ymax = 2*np.pi

#The callable functions for the x limits are just constants in this case:
    xmin = lambda y : 0
    xmax = lambda y : rmax

#See the help for correct order of limits
    I, err = dblquad(integrand, ymin, ymax, xmin, xmax)
    return(I)

In [63]:
assert np.allclose(integrate_polar(lambda r,t: 1, 1.0), np.pi)
assert np.allclose(integrate_polar(lambda r, t: np.exp(-r)*(np.cos(t)**2), np.inf), np.pi)

In [ ]:


In [ ]:


In [ ]:


In [ ]: