Evaluating Triple Integrals with SymPy_jl

Suppose we want to determine the volume of a sphere with radius \(R\). Spherical coordinates are clearly the best option so the integral we are interested in is,

\[ \iiint_D \rho^2 \sin(\phi)\ d\rho\ d\phi\ d\theta \,.\]

By Fubini's theorem, we can change around the order of variables as we like, as long as we are consistent with the limits of integration which are given as,

\[\begin{aligned} 0 \leq\ &\rho\ \leq R \\ 0 \leq\ &\theta\ \leq 2\pi \\ 0 \leq\ &\phi\ \leq \pi/2 \\ \end{aligned}\]

and then we'll need to double it since we're only integrating over the top hemisphere.

using SymPy

@vars ρ θ ϕ R

intlims = Dict{Sym, Tuple{Int64, Number}}(
    ρ => (0, R),
    θ => (0, 2π),
    ϕ => (0, π/2)
)

eqn = ρ^2 * sin(ϕ)

2 * integrate(eqn, 
    (ρ, intlims[ρ][1], intlims[ρ][2]),
    (ϕ, intlims[ϕ][1], intlims[ϕ][2]),
    (θ, intlims[θ][1], intlims[θ][2])  
)
> 4.18879020478639𝑅3

Which is approximately equal to the known volume of a sphere,

\[ \frac{4}{3}\pi R^3 \,.\]