I cannot understand how to approach defining the limits of integration for a triple integral in Scipy with the tplquad
function. I am using the book Learning Scientific Programming With Python By Christian Hill and trying to solve problem E8.14:
The volume of the unit sphere, 4π/3, can be expressed as a triple integral in spherical polar co-ordinates with constant limits:
My attempt to evaluate this integral:
import scipy.integrate as integrate
#define the integrand
f = lambda r, theta, phi: r**2 * np.sin(theta)
#integrate with tplquad
V_unit_sphere, _ = integrate.tplquad(f, 0,1,0,np.pi,0,2*np.pi)
V_unit_sphere
My output was 165... clearly not equal to 4pi/3.
Hill's Solution:
from scipy.integrate import tplquad
In [x]: tplquad(lambda phi, theta, r: r**2 * np.sin(theta),
0, 1,
lambda theta: 0, lambda theta: np.pi,
lambda theta, phi: 0, lambda theta, phi: 2*np.pi)
Out[x]: (4.18879020478639, 4.650491330678174e-14)
I do not understand why Hill defined the integrand in the order of phi, theta, r
when the integral is taken the other way around, and I do not understand why even though all the limits of integration are constants, they must be defined as depending on some variable. I also do not understand why the limits of integration for theta are dependent on theta, and why the limits for phi are dependent upon both phi and theta.