4
$\begingroup$

I am trying to plot a super ellipse. It's like a regular ellipse but there are added terms so you can define how box like the ellipse will be and on the other extreme how concave the shape will be. I have gotten it to plot in one quadrant but not all four. Specifically I am trying to plot the definition using a sign function: x(t)=(|cos(t)|^(2/n))*a*sgn(cos(t)), y(t)=(|sin(t)|^(2/n))*a*sgn(sin(t)), and where sgn is a piecewise sign function sgn(w)=[{{-1,w<0},{0,w=0},1,w>0}}

I am pretty new to Mathematica so I do not have a super complex script yet. I have considered using Module but I am not super sure how that would work. My best attempt is as follows:

Clear[w, t, n, a, b]
sgn[w_] := Piecewise[{{-1, w < 0}, {0, w = 0}, {1, w > 0}}];
x[t_, n_, a_] := Abs[Cos[t]]^(2/n)*a*sgn[Cos[t]];
y[t_, n_, b_] := Abs[Sin[t]]^(2/n)*b*sgn[Sin[t]];

ParametricPlot[{x[t, 4, 4], y[t, 4, 4]}, {t, 0, 2 Pi}]

I get a couple "cannot assign to raw object" errors which I am not sure how to fix. That is why I was thinking of using Module. Any help would be greatly appreciated and sorry if I wasted your time with some blatantly obvious errors.

$\endgroup$
  • $\begingroup$ You need to use w == 0 not w = 0. $\endgroup$ – Carl Woll Nov 12 at 21:12
  • $\begingroup$ Why not use the built-in function Sign? $\endgroup$ – m_goldberg Nov 13 at 7:50
6
$\begingroup$

The superellipse fullfills the equation (x/a)^n + (y/b)^n == 1 which might be plotted simple directly using ContourPlot

With[{n = 4, a = 4, b = 4}, 
ContourPlot[(x/a)^n + (y/b)^n == 1, {x, -a, a}, {y, -b, b}]]

enter image description here

$\endgroup$
  • $\begingroup$ I like how simple this way is too. Both of these are really helpful, thanks! $\endgroup$ – Sykes Nov 12 at 22:59
5
$\begingroup$

You can also use Norm with ContourPlot or with RegionPlot:

cp = With[{n = 4, a = 4, b = 4}, 
  ContourPlot[Norm[{x/a, y/b}, n] == 1, {x, -a, a}, {y, -b, b},
    ContourStyle -> Directive[Red, Thick]]]

enter image description here

rp = With[{n = 4, a = 4, b = 4}, 
  RegionPlot[Norm[{x/a, y/b}, n] <= 1, {x, -a, a}, {y, -b, b}]]

enter image description here

Show[rp, cp]

enter image description here

After fixing the typo mentioned by @Carl and adding the option Exclusions->None in OP's code:

Show[rp, 
 ParametricPlot[{x[t, 4, 4], y[t, 4, 4]}, {t, 0, 2 Pi}, 
   PlotStyle -> Directive[Red, Thick], Exclusions -> None]]

enter image description here

$\endgroup$
  • $\begingroup$ Thanks, this was super helpful! $\endgroup$ – Sykes Nov 12 at 22:58
3
$\begingroup$

Sign[] is a built-in function, so:

With[{a = 4, b = 4, n = 4}, 
     ParametricPlot[{a Sign[Cos[t]] Abs[Cos[t]]^(2/n), b Sign[Sin[t]] Abs[Sin[t]]^(2/n)},
                    {t, 0, 2 π}, Exclusions -> None]]

superellipse

The Exclusions -> None setting is necessary here because Mathematica tries to be too smart in this case, and adds a needless discontinuity.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.