# Compute Triple Integral on spherical coords

I need to compute: $\int \int \int z dxdydz$

over the domain: $\{x^2+y^2+z^2\leqslant 16,z\geqslant 0\}$

Im trying to use spherical coords as:

$$\int_{0}^{2\pi} \int_{0}^{\frac{\pi}{2}} \int_{0}^{4} r \cos(\theta )r^2\sin(\varphi ) \;dr\,d\theta \,d\varphi$$

I try this on Mathematica:

Integrate[Integrate[Integrate[r *Cos[theta]*r^2*Sin[phi], {r, 0, 4}], {theta, 0, Pi/2}], {phi, 0, 2 Pi}]


But re result is 0. What is wrong here?

• What is $\int_0^{2\pi} d\phi\,\sin(\phi)$? – acl Jun 28 '14 at 2:43

In Version 9 currently, we can do (using the Undocumented form of Integrate):

Integrate[Boole[z >= 0] z, {x, y, z} ∈ Sphere[{0, 0, 0}, 4]]


64 Pi

Note: This is undocumented behaviour and functionality may change or behave differently in newer versions of Mathematica so use with caution e.g. as noted by Szabolcs in the comment, Sphere in V10 denotes a surface, whereas in V9 it represents a volume. So we should keep this in mind or we can get as fancy as V10 and use BallRegion (equivalent to Ball as Szabolcs did). To do this, first load the Region context

GraphicsRegionRegionInit[]


Then:

Integrate[ Boole[z >= 0] z, {x, y, z} ∈ BallRegion[{0, 0, 0}, 4]]


64 Pi

• Ok, thanks, the funny thing is that if i do it by hand it gives me 0 too... – Wyvern666 Jun 28 '14 at 4:26
• @Wyvern666 Just keep in mind the caveats about undocumented functionality. In v9 it's a work-in-progress, so it may break, give wrong results, etc. – Szabolcs Jun 28 '14 at 4:31
• An important note: in version 10 Sphere denotes a surface, not a volume region. So in v10 Integrate[Boole[z >= 0] z, {x, y, z} ∈ Sphere[{0, 0, 0}, 4]] is going to do a surface integral, which accidentally happens to have the same result the volume integral would for a radius of 4 (but not for other values). There might be a lot of confusion because of this accident ... you might want to mention this difference. – Szabolcs Jun 28 '14 at 4:40
• @Szabolcs. Thanks, note added. – RunnyKine Jun 28 '14 at 14:14
• @Wyvern666 that's because you mixed up the polar and azimuthal angles, so the integral you gave really does vanish (as I attempted to hint in my comment to your question). See Jens's answer for the correct form. – acl Jun 28 '14 at 23:56

Since you explicitly asked for a way to do this integral in spherical coordinates, here is a formulation that works in all versions of Mathematica. First I define the spherical coordinates, and then I do the triple integral using the Jacobi determinant:

{x, y, z} =
r {Cos[ϕ] Sin[θ], Sin[ϕ] Sin[θ], Cos[θ]};

Integrate[
z Abs[Det[D[{x, y, z}, {{r, θ, ϕ}}]]],
{r, 0, 4}, {θ, 0, π/2}, {ϕ, 0, 2 Pi}]

(* ==> 64 Pi *)


The more natural way to express this problem is

Integrate[Boole[x^2 + y^2 + z^2 <= 16] z, {x, -4, 4}, {y, -4, 4}, {z, 0, 4}]

(* ==> 64 Pi *)


This works in version 9 and earlier. In version 10 we can get fancy and do

Integrate[Boole[z >= 0] z, {x, y, z} ∈ Ball[{0, 0, 0}, 4]]

(* ==> 64 Pi *)

• Is version 10 of Mathematica available already? – Vaggelis_Z Jun 28 '14 at 6:48
• @Vaggelis_Z The programming cloud is available. It's possible to try these with a free account. – Szabolcs Jun 28 '14 at 13:07

You set up the integral incorrectly. You are mixing up $\varphi$ and $\theta$.

There is nothing wrong with your mathematica code. The problem is with the volume element. The volume element in spherical coordinates is $$r^2\sin(\theta ) \;dr\,d\theta \,d\varphi$$ not $$r^2\sin(\varphi ) \;dr\,d\theta \,d\varphi$$ Simply replace $$Sin[phi]$$ with $$Sin[theta]$$ in your code, and you get the answer.

Integrate[Integrate[Integrate[r *Cos[theta]*r^2*Sin[theta], {r, 0, 4}], {theta, 0, Pi/2}], {phi, 0, 2 Pi}]


Another method is using Cartesian coordinate:

4 Integrate[
Integrate[
Integrate[z, {x, 0, Sqrt[16 - y^2 - z^2]}], {y, 0,
Sqrt[16 - z^2]}], {z, 0, 4}]

(* 64 Pi *)