Perimeter of Ellipse
03-05-2016, 04:19 PM
Post: #1
 Joe Horn Senior Member Posts: 1,454 Joined: Dec 2013
Perimeter of Ellipse
Although simple formulae for the perimeter of an ellipse exist, they are only approximations. Exact formulae are complicated. The following program uses a converging iteration technique instead. Ported from a QBASIC program by Gérard P. Michon.

Syntax: EllipsePerimeter(a,b), where a & b are the lengths of the axes (order doesn't matter)
Output: perimeter of ellipse

Examples:
EllipsePerimeter(0.5, 0.5) --> 3.14159265359
EllipsePerimeter(3, 4) --> 22.1034921607

Code:
gk(h); cayley(x); EXPORT EllipsePerimeter(a,b) BEGIN LOCAL x,h,P; a:=ABS(a); b:=ABS(b); IF a < b THEN x:=a; a:=b; b:=x; END; IF b < 0.28*a THEN  P := 4*a*cayley((b/a)^2); ELSE  h := ((a-b)/(a+b))^2;  P := pI*(a+b)*gk(h); END; RETURN P; END ; gk(h) BEGIN LOCAL z,x,n; z := 0; x := 1; n := 0; WHILE z + x <> z  DO  n := n + 1;  x := h * x * ((n-1.5)/n)^2;  z := z + x; END; RETURN 1 + z; END; cayley(x) BEGIN LOCAL y,t,n,z,u,v,w; y := LOG(16 / x) - 1; t := x / 4; n := 1; z := 0; u := t * y; v := (n - .5) / n; w := .5 / ((n - .5) * n); WHILE z <> z + u  DO  z := z + u;  n := n + 1;  t := x * t * v;  v := (n - .5) / n;  t := t * v;  y := y - w;  w := .5 / ((n - .5) * n);  y := y - w;  u := t * y; END; RETURN 1 + z; END;

<0|ɸ|0>
-Joe-
03-06-2016, 11:55 AM
Post: #2
 Wes Loewer Member Posts: 141 Joined: Jan 2014
RE: Perimeter of Ellipse
I was going to suggest the following:

Code:
EXPORT EllipsePerimeter(a,b) BEGIN  RETURN ∫(√(((a*COS(T))^2+(b*SIN(T))^2)),T,0,2*π); END;

but it seems that PPL doesn't like local variables inside of an integral.

Here's a work-around:

Code:
EXPORT EllipsePerimeter(a,b) BEGIN  A:=a;  B:=b;  RETURN ∫(√(((A*COS(T))^2+(B*SIN(T))^2)),T,0,2*π); END;
03-06-2016, 02:16 PM
Post: #3
 Wes Loewer Member Posts: 141 Joined: Jan 2014
RE: Perimeter of Ellipse
or this work-around:

Code:
EXPORT EllipsePerimeter(a,b) BEGIN  RETURN approx(int(√(((a*COS(T))^2+(b*SIN(T))^2)),T,0,2*π)); END;
03-06-2016, 02:40 PM
Post: #4
 TASP Senior Member Posts: 401 Joined: Mar 2015
RE: Perimeter of Ellipse
Did not know that about perimeters of ellipses.

Learn something here everyday.

2speed HP41CX,int2XMEM+ZEN, HPIL+DEVEL, HPIL+X/IO, I/R, 82143, 82163, 82162 -25,35,45,55,65,67,70,80
03-06-2016, 06:42 PM
Post: #5
 parisse Senior Member Posts: 997 Joined: Dec 2013
RE: Perimeter of Ellipse
I guess the perimeter is computed using the arithmetic geometric mean, correct?
03-07-2016, 01:16 PM (This post was last modified: 03-07-2016 01:16 PM by SlideRule.)
Post: #6
 SlideRule Senior Member Posts: 765 Joined: Dec 2013
RE: Perimeter of Ellipse
(03-05-2016 04:19 PM)Joe Horn Wrote:  Although simple formulae for the perimeter of an ellipse exist, they are only approximations. Exact formulae are complicated. The following program uses a converging iteration technique instead.

When I was employed by an Engineering firm specializing in Bridge Design, we used elliptical substructure rather than rectangular. The design was easy enough BUT the insitu replication of that design was elusive, so we substituted cirular arcs (tangent) for the desired elipse. The desired substitute was reflective about both inplane axis, so only 1/4 of the form need be designed. Seldom were more the 5 contiguous arcs needed while 3 arcs satisfied most requirements. The circle(s) approximations functioned well within the desired strutural requirements while easing the construction layout.

BEST!
SlideRule
03-07-2016, 03:34 PM
Post: #7
 Joe Horn Senior Member Posts: 1,454 Joined: Dec 2013
RE: Perimeter of Ellipse
(03-06-2016 02:16 PM)Wes Loewer Wrote:  or this work-around:

Code:
EXPORT EllipsePerimeter(a,b) BEGIN  RETURN approx(int(√(((a*COS(T))^2+(b*SIN(T))^2)),T,0,2*π)); END;

Your program above (unlike mine) always agrees with Wolfram Alpha's perimeter of an ellipse, even for very eccentric ellipses. Congratulations!

<0|ɸ|0>
-Joe-
03-09-2016, 08:39 AM (This post was last modified: 03-09-2016 08:42 AM by parisse.)
Post: #8
 parisse Senior Member Posts: 997 Joined: Dec 2013
RE: Perimeter of Ellipse
I can add this functionnality to perimeter, using the approximate value of the integral
http://www-fourier.ujf-grenoble.fr/%7epa...(-1,1,2))&
(make sure to clear your browser cache if you have already tried Xcas offline).
03-24-2019, 12:42 PM
Post: #9
 Albert Chan Senior Member Posts: 627 Joined: Jul 2018
RE: Perimeter of Ellipse
From HP Forum Archive 21, message 5, and some optimizations, I get:

Quote:local sqrt, pi = math.sqrt, math.pi

function ellipsePerimeter(a, b) -- agm2 method
﻿ ﻿ ﻿ ﻿ local s = 0.5 * (a*a+b*b)
﻿ ﻿ ﻿ ﻿ local t = 0.25
﻿ ﻿ ﻿ ﻿ while true do
﻿ ﻿ ﻿ ﻿ ﻿ ﻿ ﻿ ﻿ local k = b - a
﻿ ﻿ ﻿ ﻿ ﻿ ﻿ ﻿ ﻿ local s1 = s - t*k*k
﻿ ﻿ ﻿ ﻿ ﻿ ﻿ ﻿ ﻿ if s == s1 then return 2*pi*s/(a+0.5*k) end
﻿ ﻿ ﻿ ﻿ ﻿ ﻿ ﻿ ﻿ b = sqrt(a*b)
﻿ ﻿ ﻿ ﻿ ﻿ ﻿ ﻿ ﻿ a = a + 0.5*k
﻿ ﻿ ﻿ ﻿ ﻿ ﻿ ﻿ ﻿ s = s1
﻿ ﻿ ﻿ ﻿ ﻿ ﻿ ﻿ ﻿ t = t + t
﻿ ﻿ ﻿ ﻿ end
end

Example:

lua> ellipsePerimeter(1, 2) --> 9.688448220547675
lua> ellipsePerimeter(2, 3) --> 15.86543958929059
lua> ellipsePerimeter(3, 4) --> 22.103492160709504
 « Next Oldest | Next Newest »

User(s) browsing this thread: 1 Guest(s)