Ellipse Perimeter - Infinite Series Equation

Information from: Perimeter of an Ellipse

Infinite Series Equation to Calculate an Ellipse's Perimeter

This is an exact equation. It requires an infinite number of terms to get the exact perimeter. With fewer terms, however, we can calculate a very close approximation.

image missing

Approximation Steps, Notes, ...

Step 1, calculate the ellipse's "h" value

(a − b)2 h = -------- (a + b)2

Step 2, use the "infinite series" equation

This is the expand series with 4 terms. It shows terms n=0 to n=3 (1, 1/4, 1/64, 1/256).

image_missing

With more terms we get a more accurate answer.
the next term (n=4) is (25/16384)h4
the next term (n=5) is (49/65536)h5
the next term (n=6) is (441/1048576)h6
the next term (n=7) is (1089/4194304)h7

Binomial Coefficient

image_missing

image missing

Note: For eccentricities less than 0.5 (h < 0.005), the error is at the limits of double-precision floating-point after the h4 term. In other words the precision of the hardware is exceeded.

Code

#!/usr/bin/python3 # ================================================================= # ellipse perimeter binomial coefficients # ================================================================= import math import user_interface as ui # ----------------------------------------------------------------- # ---- double factorial # ----------------------------------------------------------------- def double_factorial(n:int) -> int: if n <= 0: return 1 else: return n * double_factorial(n-2) # ----------------------------------------------------------------- # ---- calculate the binomial coefficient for term N # ----------------------------------------------------------------- def calculate_binomial_coefficient(n:int, debug:bool=False)->(int|float): numerator = double_factorial(2*n - 3) denominator = double_factorial(2*n) binomial_coefficient = (numerator/denominator)**2 if debug: print(f'(n={n}) numerator = {numerator}') print(f'(n={n}) denominator = {denominator}') print(f'(n={n}) coefficient = {binomial_coefficient}') return binomial_coefficient # ----------------------------------------------------------------- # ---- test binomial coefficients (n = 0 to 7) # ----------------------------------------------------------------- def test(): n_list = [ (0,1),(1,1/4),(2,1/64),(3,1/256), (4,25/16384),(5,49/65536), (6,441/1048576),(7,1089/4194304) ] print('Test Binomial Coefficients') for n,x in n_list: print() bc = calculate_binomial_coefficient(n) print(f'[{n:02}] bc = {bc}') print(f' {x}') return # ----------------------------------------------------------------- # ---- ask the user for n (binomial coefficient term number) # ----------------------------------------------------------------- def ask_the_user(): while True: # ---- ask the user for n print() s = ui.get_user_input('Enter n: ') if not s: break tf,n = ui.is_integer(s) if not tf: print('bad input - try again') continue if n < 0: print('bad input - try again') continue bc = calculate_binomial_coefficient(n) print(f'(n={n}) coefficient = {binomial_coefficient}') return # ----------------------------------------------------------------- # ---- main # ----------------------------------------------------------------- test() ## ask_the_user()