# Triple Integral over a shifted sphere

I am interested in the following: Let $f(x,y,z)$ be a given (known) function in Cartesian coordinates, and let $B_d(p_0) = \{ y: ||y-p_0|| < d \}$ (i.e., a sphere centered at p_0). I want to numerically compute in MATLAB

$$\int \int \int_{B_d(p_0)} f(x,y,z) dx dy dz$$

Now, I remember that if $p_0 = 0$, this isn't too bad: I just change to spherical coordinates and compute

$$\int_0^{2 \pi} \int_0^{\pi} \int_0^d f(r,\theta,\phi) r^2 \sin(\theta) dr d\theta d \phi$$

where $\theta$ is my polar angle (from the positive z-axis down) and $\phi$ is my azimuthal angle (from the positive x-axis in the x-y plane). In this form, this is just an integral over a cube , and I can just call integral3 on this, with my integrand being $f(r,\theta,\phi) r^2 \sin(\theta)$. But, if I have a circle centered elsewhere, it seems a bit trickier.

I thought I could shift variables as such: Let $p_0 = (x_0,y_0,z_0)$. Then, set $x' = x-x_0, y' = y-y_0, z' = z-z_0$. I believe that this linear shift won't affect the integration, so now I have

$$\int \int \int_{B_d(0)} f(x',y',z') dx' dy' dz'$$ This I can now switch to polar coordinates as

$$\int_0^{2\pi} \int_0^{\pi} \int_0^d f(r',\theta',\phi') r'^2 \sin(\theta') dr d\theta' d\phi'$$.

with $r' = x'^2 + y'^2 + z'^2$, $\theta' = arccos(\frac{z'}{r'})$, $\phi' = arctan\frac{y'}{x'})$. I guess I'm worried about how to actually input this; there has to be some additional shifting or changing going on, because if I just tell MATLAB to integrate $f(r',\theta',\phi')$, over a sphere of radius 0, it looks to me like it would be no different than integrating over $(r,\theta,\phi)$ without the shift. Is there some change to the Jacobian or something I need to do to reflect this shift of variables to this shifted polar coordinate system? Would I first define a function $f(x,y,z)$, then define a new shifted function $g(x',y',z') = f(x,y,z)$, then spherical coordinate $g$?

For example, let's say we want to integrate $f(x,y,z) = \sin(x) \sin(y) \sin(z)$ over a ball of radius 1 centered at (2,3,4). First, I can define $x' = x-2, y' = y-3, z' = z-4$, and I have $f(x',y',z') = \sin(x'+2) \sin(y'+3) \sin(z'+4)$. Then, I can define my spherical coordinate system, and I replace $x' = r' \sin(\theta') \cos(\phi'), y' = r' \sin(\theta') \sin(\phi')$ and $z' = r' \cos(\theta')$. This leads to the final integrand being (including the Jacboian from the spherical coordinate change)

$$F(r',\theta',\phi') = \sin(r'\sin(\theta')\cos(\phi')+2) \sin(r' \sin(\theta') \sin(\phi')+3) \sin(r' \cos(\theta')+4) r'^2 \sin(\theta')$$

This function i can then apply a Matlab routine like integral3 over $0\leq r' \leq 1$, $0 \leq \theta' \leq \pi$ and $0 \leq \phi' \leq 2\pi$.

Does this sound correct? Thanks!

-

Another way of thinking of this is just making normal variable substitution in integral and finding new $dx^\prime, dy^\prime, dz^\prime$. As you do this, you'll see that integral didn't change except having new variables. This problem now becomes completely self-contained, and you can safely switch to spherical coordinates from your new integral.