Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Squircle Perimeter and the Isoparametric Problem With Python

DZone 's Guide to

Squircle Perimeter and the Isoparametric Problem With Python

In this post, we investigate the scientific/mathematical power of the Python language. Read on to learn more!

· Big Data Zone ·
Free Resource

If you have a fixed length of rope and you want to enclose the most area inside the rope, make it into a circle. This is the solution to the so-called isoparametric problem.

Dido's problem is similar. If one side of your bounded area is given by a straight line, make your rope into a semi-circle.

This post looks at a similar problem for a squircle. Peter Panholzer mentioned the problem of finding the squircle exponent that led to the largest area in proportion to its arclength. This sounds like the isoparametric problem, but it's not.

The isoparametric problem holds perimeter constant and lets the shape enclosed vary, maximizing the area. The question here is to hold the axes constant and maximize the ratio of the area to the perimeter. Panholzer reports the maximum occurs at p = 4.39365.

Computing the Perimeter

The volume of a squircle can be found in closed form, and I've mentioned the equation a few times, for example here. The perimeter, however, cannot be found in closed form, as far as I know, except for special exponents.

We can solve for y as a function of x and find the arclength using the formula taught in calculus courses. However, the derivative of y has a singularity at x = 1. By switching to polar coordinates, we can find arclength in terms of an integrand with no singularities. We can also simplify things a little by computing the total arclength as 4 times the arclength in the first quadrant; this avoids having to take absolute values.

The following Python code computes the perimeter and the ratio of the area to the perimeter.

    from scipy import sin, cos, pi
    from scipy.integrate import quad
    from scipy.special import gamma

    def r(t, p):
        return (cos(t)**p + sin(t)**p)**-(1/p)

    def drdt(t, p):
        deriv = (cos(t)**p + sin(t)**p)**(-1-1/p)
        deriv *= cos(t)**(p-1)*sin(t) - sin(t)**(p-1)*cos(t)
        return deriv

    def integrand(t, p):
        return (drdt(t,p)**2 + r(t,p)**2)**0.5

    def arclength(p):
        integral = quad(lambda t: integrand(t,p), 0, pi/2)[0]
        return 4*integral

    def area(p):
        return 4*gamma(1 + 1/p)**2 / gamma(1 + 2/p)

    def ratio(p):
        return area(p) / arclength(p)

Basic geometry tells us the ratio is 1/2 when p = 2 and we have a circle. The ratio is also 1/2 when p = ∞, i.e. when we have a square. We can use the code above to find that the ratio when p = 0.528627, so there is at least one local maximum for values of p between 2 and ∞.

Here's a plot of the ratio of area to perimeter as a function of p.

The plot is quite flat for large values of p, but if we zoom in we can see that there is a local maximum near 4.4.

When I find the maximum of the ratio function using Brent's method ( scipy.optimize.brent) I find p = 4.39365679, which agrees with the value Panholzer stated.

Here's a plot of the squircle corresponding to this value of p.

Back to the Isoparametric Problem

Now why doesn't this contradict the isoparametric problem? Area scales quadratically but perimeter scales linearly. If you don't hold perimeter constant, you can find a larger ratio of area to volume by making the perimeter larger. And that's what we've done. When p = 2, we have a unit circle, with area π and perimeter 2π. When p = 4.39365679 we have area 3.750961567 and permimeter 7.09566295. If we were to take a circle with the same perimeter, it would have area 4.00660097, larger than the squircle we started with.

Topics:
python ,big data ,python tutorial ,data visualization

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}