What is the numerical approximation for a Lamé Curve?
This question: Arc length of the squircle seems to be related, but doesn't provide an answer to the case where $a \neq b$, nor the limit that $n > 2$.
Mainly as I'm trying to figure out how the calculator here might work (i.e. it doesn't need to be exactly how they work, but should be a close enough approximation).
I've looked at What is the circumference (arc length) of $x^4 + x^2 + y^4 + y^2 = 2$? and tried to plot a superellipse then once I am sure the formula is correct I could get Wolfram Alpha to do the heavy lifting of integrating it, but the website seems to hang.
How can I approximate this?
Here is precisely how the website's calculator works: $\newcommand{\peri}{\bbox[#E4E6E8,1px]{\textsf{Perimeter}}} \newcommand{\ans}{\bbox[#E4E6E8]{\textsf{anss}}}$
Discovery
If you View Source and search for the button label
Calculateyou will find<td align="center" rowspan="3"><input type="button" onclick="lame(this.form)" value="Calculate" style="margin-top:2px"><br>. Where is thislame(...)defined? After poking around I discovered that this is hidden indrop-ads.js. (Aside: is this file for advertisements? why is user-code here...?)The full code for
lame(...)isTranslating from
javascriptto $math$The relevant part of the output is given by
Math.ceil(1000*anss)/1000;}, i.e. $\frac1{1000}{\lceil 1000\, \ans \rceil}$. This is a rounding up to 4 decimal points ofanss=adj(n)*sum*Math.PI*4/(250000*n). What exactly though isanss?adj(n)is a small fudge factor that was probably empirically determined. But the remaining bit, expressed in mathematical notation is as follows: set $N=250000$. Then \begin{align} &\ans =\\& \frac{4\pi}{Nn}\sum_{i=0}^{N-1}\sin\left(\tfrac{\pi}2\tfrac iN+\tfrac\pi{4N}\right)\cos\left(\tfrac{\pi}2\tfrac iN+\tfrac\pi{4N}\right) \sqrt{a^2\cos\left(\tfrac{\pi}2\tfrac iN+\tfrac\pi{4N}\right)^{\frac4n-4} + b^2\sin\left(\tfrac{\pi}2\tfrac iN+\tfrac\pi{4N}\right)^{\frac4n-4} } \end{align}This is a Riemann sum (with tags $\pi i/2N + \pi/4N$ and uniform mesh size $\pi/2N$) for the integral \begin{align} &\frac{4\pi}n \int_0^1 \sin(\pi x/2) \cos(\pi x/2) \sqrt{a^2\sin^{4/n-4}(\pi x/2) + b^2\cos^{4/n-4}(\pi x/2)} dx \\&= 4\int_0^{\pi/2} \frac2n\sin t \cos t \sqrt{a^2 \sin^{4/n-4}t+b^2\cos^{4/n-4}t } \ dt \end{align} which is precisely the arc-length integral (using symmetry across both axes to absorb the $4$) $$ \int_0^{2\pi} |\gamma'(t)|\ dt, \quad \gamma(t) = \binom{a \lvert\sin t|^{2/n-1}\sin t}{b\lvert \cos t|^{2/n-1}\cos t},$$ where $\big|\binom{v_1}{v_2}\big|=\sqrt{v_1^2+v_2^2}$ denotes the Euclidean norm, and for $t\in[0,\pi/2]$, $$\gamma'(t) = \frac2n\sin t\cos t\binom{a \sin^{2/n-2}t}{-b\cos^{2/n-2}t} . $$
Visualisation of arclength integrand and Riemann sums
For $n>2$, the integrand near $t=0$ behaves like $t^{-1+2/n}$, from $\sin t\sim t$. Near $t=\pi/2$ the same happens using $\cos t \sim \pi/2-t$. This is not a bounded function (which explains why the tags cannot be the left or right endpoints of the mesh), so the Riemann sum (which approximates using a bounded step function) is likely not so accurate. See the behavior near $x=0,\pi/2$:
Intuitively the error from the tails should be like the bound from Speed of convergence of a Riemann sum, i.e.
$$ \int_0^{2\pi} |\gamma'(t)|\ dt = \ans + O(N^{-2/n})$$
Which might explain why some sort of fudge-factor was used; even with the huge $N=250000$, $N^{-2/10} \approx 0.083,$ so this probably isn't enough for 4 decimal points.