A PHP Function to Compute the Circumference or Perimeter of an Ellipse or Circle
by Evaluating the Complete Elliptic Integral of the Second Kind Using an Infinite
Power Series Summation Based on Legendre Polynomials.
MathJax equations display requires Java Script to be enabled. PHP Function by Jay Tanner - 2018
The Complete Elliptic Integral Of The Second Kind
The complete elliptic integral of the second kind may be symbolized as:
$\begin{align*}
(Equation~1):~~~~~~~~
\frac{C}{4} = \int_0^{\large\frac{\pi}{2}}{\sqrt{1-\epsilon^2\cdot\sin^2(\phi)}\;\;d\phi}
\end{align*}$ =
The integral defines the length of the elliptic arc from $0$ to $2\pi$ (or $0$ to $90$°) or only $\frac{1}{4}$ of the full $360$° span of the ellipse. It is a line integral in the sense that it defines the length of a line, in this case, one quarter of the ellipse circumference.
Numerically solving the integral is difficult enough as it is, so solving only for the first quarter and then multiplying the result by $4$ seems like the simplest way to go about it.
Notice that the angular arc-span is measured positively counter-clockwise.
The Basic Ellipse
To make use of the integral, we need to adapt it to our purpose, which in this case, is to 'simply' compute the circumference of an ellipse.
Generally, the idealized equations equate the major radius $(R)$ to unity or ($R = 1$) and this is often left invisible and not clearly indicated. In the real world, we need to be able to use any units of measure we wish, so we will use $R$ rather than an implied invisible $1$. Thus, $R$ can be interpreted as any implied units, meters, kilometers, miles, etc.
Given an ellipse with parameters $(R \ge r)$ =
The elliptic eccentricity $(\epsilon)$, may be found from:
$
(Eq.~2):~~~~~~~~
\begin{align*}
\epsilon ~=~ \sqrt{1 - \frac{\Large r^2}{R^2}}
\end{align*}
$
The eccentricity tells us how much the form of the ellipse deviates from a perfect circle. Zero means the form is a perfect circle and the greater the value of $\large\epsilon$, within the contstraints $(0 ~\le~ \large\epsilon ~\lt~ 1)$, the 'flatter' the ellipse.
The symbol $\large(\epsilon)$ (Greek lower-case letter epsilon) is used here for the eccentricity instead of $\large(e)$, so as not to confuse it with Euler's number $\large(e = 2.71828...)$, used as the base of natural logarithms.
To be able to use any desired units of length or distance measure, simply specify the $(R)$ value in any units $($m, km, mi, etc.$)$, so that the resulting circumference $(C)$ will be in the same units as $(R)$.
$\begin{align*}
(Eq.~3):~~~~~
\frac{C}{4} ~=~ R \times \int_0^{\large\frac{\pi}{2}}{\sqrt{1-\epsilon^2\cdot\sin^2(\phi)}\;\;d\phi}
\end{align*}$ = =
Since the integral equates to $\frac{1}{4}$ the circumference, we must multiply it by $4$ to obtain the full circumference.
$\begin{align*}
(Eq.~4):~~~~~
C ~=~ 4R~\times \int_0^{\large\frac{\pi}{2}}{\sqrt{1-\epsilon^2\cdot sin^2(\phi)}\;\;d\phi}
\end{align*}$ = $4$ $\times$ =
The integral above formally defines the problem and the summation equation below provides one of several possible numerical solutions adopted to solve the problem, defined purely in terms of the basic ellipse parameters $(R,~r)$.
Numerical Solution
The numerical computation of the above elliptic integral will involve evaluating the infinite power series summation below until a given precision limit is reached. This solution is based on Legendre polynomials.
The numerical solution for the quarter-ellipse is expressed by this power series:
$\begin{align*}
(Eq.~5):~~~~~
\frac{C}{4} ~=~ \frac{\pi R}{2}\left[ 1 - \sum_{n=1}^\infty \left(\frac{(2n-1)!!~^2}{(2n)!!~^2 \cdot (2n-1)}\right) \cdot {\epsilon ^ {2n}} \right]
\end{align*}$
As stated above, since $(Eq.~4)$ only solves for the length of $\frac{1}{4}$ of the ellipse, we have to multply it by $4$ to obtain the full circumference.
Multiplying by $4$ gives the equation:
$
\begin{align*}
(Eq.~6):~~~~~~~~
C ~=~ 2\pi{R}\left[ 1 - \sum_{n=1}^\infty \left(\frac{(2n-1)!!~^2}{(2n)!!~^2\cdot(2n-1)}\right) \cdot \epsilon^{2n}\right]
~=~ 2\pi{R}~\left[ 1 - Summation\right]
\end{align*}
$
Other numerical solutions are possible, but this solution was adopted due to the simplicity of programming it as well as its fast convergence for many practical problems. $(Eq. 6)$ is numerically evaluated by the PHP function defined below.
If the ellipse is the cross-section of an ellipsoid, then the $(R, ~r)$ parameters respectively represent the equatorial and polar radii, sometimes symbolized as $(a, ~b)$, where $(a \ge b)$.
In the special case of a circle, where ($R = r$), $(\epsilon = 0)$, then $[Summation ~=~ 0]$, and the equation for the circumference reduces to the classical circular circumference equation:
$\begin{align*}
(Eq.~7):~~~~~~~~
C ~=~ 2\pi{R}~\left[ 1 - Summation\right] ~=~ 2\pi{R}~[ 1 - 0] ~=~ 2\pi R
\end{align*}$
When $(R \ne r)$, then $(0 ~<~ \epsilon ~<~ 1)$ and the form is an ellipse, and computing the circumference will require solving the elliptic integral via a power series summation like the one used here.
Below is a listing of a PHP function designed to compute the high-precision general circumference of an ellipse. It is based on $(Eq.~6)$ above and makes use of arbitrary-precision arithmetic to compute the EXACT values of the extremely large integer fractions involved, so it could take several seconds in some extreme cases.
For most practical applications, such as geodesic and meridian computations, the function should converge sufficiently fast and accurately to $16$ decimals Also, the function could be easily tweaked for higher accuracy, if actually needed.
The PHP Code
The demonstration program below demonstrates using the function to compute the equatorial and polar meridian circumferences of the Earth according to the WGS-84 Earth ellipsoid model used by the GPS system. The parameters for any other ellipsoid model could also be used instead.
The function is an implementation of $(Eq. 6)$, above.
Double-Click to select ALL code
Double-Click to select ALL code
Below is the PHP code for only the ellipse circumference function, without all the comments.
Double-Click to select ALL code
Double-Click to select ALL code
|