How to implemente squircle formula in Matlab?

144 Views Asked by At

So I am trying to plot a Squircle in Matlab. I would like to use the formula below

$$\left|\frac{x - a}{r_a}\right|^p + \left|\frac{y - b}{r_b}\right|^p = 1$$

I am not sure how to deal with the absolute value and to solve it for y?

ps. Yes I saw this post.

2

There are 2 best solutions below

0
On BEST ANSWER

I don't have MATLAB in my pc but I think the following will work:

a=1; b=3; c= 5; d= 2; p=2;
syms f(x,y)
f(x,y) = (abs(x-a)/c)^p +(abs(y-b)/d)^p - 1;
fimplicit(f)
0
On

If you just want to have a picture of a superellipse or "squircle", there are plenty of online graphers like Desmos that can do it for you. Desmos even lets you put sliders in to control the length, width, and "squircularity" (i.e. the power $p$). See pic:

enter image description here

Note: I'm using $h, k, a, b$ here instead of the OP's notation because those are the variables typically used in writing an ellipse: $$\frac{(x-h)^2}{a^2} + \frac{(y-k)^2}{b^2} = 1,$$ where $(h, k)$ is the center, $a$ is the semi-horizontal diameter, $b$ is the semi-vertical diameter.

In terms of graphing as a function, you will need 2 functions, one for the top half and one for the bottom half:

\begin{align*} \left| \frac{x - h}{a} \right|^p &+ \left| \frac{y - k}{b} \right|^p = 1 \\ \left| \frac{y - k}{b} \right|^p &= 1 - \left| \frac{x - h}{a} \right|^p \\ \frac{y - k}{b} & = \pm \sqrt[p]{1 - \left| \frac{x - h}{a} \right|^p} \\ y &= k \pm b\sqrt[p]{1 - \left| \frac{x - h}{a} \right|^p} \\ \end{align*}

So the top half equation is $$y = k + b\sqrt[p]{1 - \left| \frac{x - h}{a} \right|^p}$$ and the bottom half equation is $$y = k - b\sqrt[p]{1 - \left| \frac{x - h}{a} \right|^p}.$$

You could also use a fractional exponent $1/p$ instead of the $\sqrt[p]{}$ to write these equations.