Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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,
  2*radius,
  Assuming[radius > 0,
   Integrate[
    BallVolume[dimension - 1, Sqrt[radius^2 - x^2]],
    {x, -radius, radius}
   ]
  ]
 ]

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?

share|improve this question

2 Answers 2

up vote 11 down vote accepted

You are using the same dummy variable for all integrals.


Extended answer

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

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

enter image description here

Use Module to create temporary dummies.

BallVolume[dimension_, radius_] := 
 If[dimension == 0, 2*radius, 
  Assuming[radius > 0, 
   Module[{x}, 
    Integrate[
     BallVolume[dimension - 1, Sqrt[radius^2 - x^2]], {x, -radius, 
      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;

enter image description here

share|improve this answer
    
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. –  drake Nov 22 '13 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}]. –  Hector Nov 22 '13 at 13:15
    
Thanks. I'm looking for a closed formula. I have asked it as a separate question mathematica.stackexchange.com/questions/37632/… –  drake Nov 22 '13 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]

enter image description here

share|improve this answer
    
If the purpose is to get a table, use NestList instead of Nest. –  Hector Oct 13 '13 at 15:34
    
@Hector excellent point... –  ubpdqn Oct 14 '13 at 3:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.