The volume of a height-h cap of a ball of radius r in dimension n can be derived:

where I_x(a,b) is the regularized incomplete beta function. In high dimension each of these terms can be extremely small or large, so rounding errors accumulate quickly. In moderate dimension this can be overcome by working with the quantity's log. Here is a simple matlab function that does this:

As of 2018-7-13, this function involves taking the log of matlab's regularized incomplete beta function `betainc`

. If there is numerical imprecision in `log(betainc(.))`

, it is necessary to replace this with a formula involving `betaln`

, some constants and an approximation of the log of Gauss' hypergeometric function. The comment at the top of the .m file describes this formula should the need arise.