# Recursive Integral for Volume of $n$-Ball

The volume of an $n$-ball (the $(n+1)$-dimensional analogue of a disk) of radius $r$ can be found by the following integral recurrence: $$V_0(r)=2r$$ $$V_n(r)=\int_{-r}^rV_{n-1}\left(\sqrt{r^2-x^2}\right)\ \mathrm{d}x$$ I would like to use Mathematica to compute a few terms of this recurrence (as an exercise. I am aware that an explicit formula exists). The recursive code I came up with was this:

BallVolume[dimension_, radius_] := If[dimension == 0,
Integrate[
BallVolume[dimension - 1, Sqrt[radius^2 - x^2]],
]
]
]


Calling BallVolume[1, r] works as expected, giving $\pi r^2$, but Mathematica seems to get stuck when evaluating BallVolume[2, r]. This doesn't seem to be a problem with its ability to integrate; if I define explicitly CircleArea[r_] := Pi*r^2, then Integrate[CircleArea[Sqrt[r^2 - x^2]], {x, -r, r}] correctly gives $4 \pi r^3\over 3$. Why does the above code fail for dimensions $3$ and higher?

You are using the same dummy variable for all integrals.

Modify your code slightly and note that all integrals use the same dummy:

BallVolume[dimension_, radius_] :=
If[dimension == 0, 2*radius,
testIntegrate[
BallVolume[dimension - 1, Sqrt[radius^2 - x^2]], {x, -radius,
BallVolume[2, r]


Use Module to create temporary dummies.

BallVolume[dimension_, radius_] :=
If[dimension == 0, 2*radius,
Module[{x},
Integrate[
BallVolume[dimension - 1, Sqrt[radius^2 - x^2]], {x, -radius,
BallVolume[3, r]


I learned about this issue thanks to a comment by ssch in this answer.

Recursive functions are suitable for memoizing. That has the advantage of not having to perform those integrals over and over again.

hBallVolume[d_Integer /; d > 0] :=
hBallVolume[d] =
Function @@ {r,
Integrate[hBallVolume[d - 1][Sqrt[r^2 - x^2]], {x, -r, r},
Assumptions -> r > 0]};
hBallVolume[0][r_] := 2 r;


• Hello. I'm working in your last solution (recursive function). I'd like that $d$ was considered a variable rather than a parameter, that is, that hBallVolume was a function of two variables $d$ and $r$, so that I could see how hBallVolume depends on $d$ for a fixed value of $r$ besides how hBallVolume depends on $r$ for fixed values of $d$. How could I do it? It's driving me crazy. (I'm a beginner in Mathematica). Thank you. Commented Nov 22, 2013 at 2:04
• @drake: $d$ is already a variable: dimension. If you want a general formula, you can induce one yourself (that would be beyond this question). Try Table[BallVolume[d,r],{d,4}]. Better yer, try Table[hBallVolume[d][r], {d, 10}]. Commented Nov 22, 2013 at 13:15
• Thanks. I'm looking for a closed formula. I have asked it as a separate question mathematica.stackexchange.com/questions/37632/… Commented Nov 22, 2013 at 19:17
vol[n_]:=FullSimplify[
Nest[Integrate[# /. r -> Sqrt[r^2 - x^2], {x, -r, r}] &, 2 r, n],
Element[r, Reals] && r > 0]


The results can be tabulated for :

 Style[TableForm[Table[{Subscript[V, j + 1], j, vol[j]}, {j, 4}],
TableHeadings -> {None, {"Sphere volume", "n", "volume"}}], 20]


• If the purpose is to get a table, use NestList instead of Nest. Commented Oct 13, 2013 at 15:34
• @Hector excellent point... Commented Oct 14, 2013 at 3:45