In [1]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
from scipy import integrate
from scipy.integrate import dblquad
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 [74]:
def integrate_polar(f, rmax):
"""Integrate the function f(r, theta) over r=[0,rmax], theta=[0,2*np.pi]"""
thmin=0
thmax=2*np.pi
rmin=lambda t: 0
rm=lambda t: rmax
f1=lambda r,t :f(r,t)*r #started on this path from suggestion in Gitter chat
I,e=dblquad(f1,thmin,thmax,rmin,rm)
return (I)
In [76]:
integrate_polar(lambda r,t: 1, 1.0)
Out[76]:
In [77]:
integrate_polar(lambda r, t: np.exp(-r)*(np.cos(t)**2),np.inf)
Out[77]:
In [80]:
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 [ ]: