Clear Filters
Clear Filters

Creating an ellipse plot with a for loop

2 views (last 30 days)
wswur
wswur on 22 May 2017
Answered: KSSV on 22 May 2017
For some reason my superellipse isn't shown in the figure when I run my code below in MatLab. I don't know where I went wrong, do I need to store the values in some way and then plot these values in the figure? Hope someone can help me out, thanks.
-Somehow the code underneath isn't shown correctly in the square on this website, sorry -
n=2;
a=1;
b=1;
x0=0; % x0,y0 ellipse centre coordinates
y0=0;
for t=0:0.01:2*pi;
if t < 0
x=x0+(abs(cos(t)).^(2/a)) * a*-1*(abs(cos(t)));
y=y0+(abs(sin(t)).^(2/b)) * b*-1*(abs(sin(t)));
end
if t == 0
x=x0+(abs(cos(t)).^(2/a)) * a*0*(abs(cos(t)));
y=y0+(abs(sin(t)).^(2/b)) * b*0*(abs(sin(t)));
end
if t > 0
x=x0+(abs(cos(t)).^(2/a)) * a*1*(abs(cos(t)));
y=y0+(abs(sin(t)).^(2/b)) * b*1*(abs(sin(t)));
end
figure(1)
plot(x,y)
grid on;
grid minor;
end

Answers (1)

KSSV
KSSV on 22 May 2017
clc ; clear all ;
n=2;
a=1;
b=1;
x0=0; % x0,y0 ellipse centre coordinates
y0=0;
figure(1)
hold on
for t=0:0.01:2*pi;
if t < 0
x=x0+(abs(cos(t)).^(2/a)) * a*-1*(abs(cos(t)));
y=y0+(abs(sin(t)).^(2/b)) * b*-1*(abs(sin(t)));
end
if t == 0
x=x0+(abs(cos(t)).^(2/a)) * a*0*(abs(cos(t)));
y=y0+(abs(sin(t)).^(2/b)) * b*0*(abs(sin(t)));
end
if t > 0
x=x0+(abs(cos(t)).^(2/a)) * a*1*(abs(cos(t)));
y=y0+(abs(sin(t)).^(2/b)) * b*1*(abs(sin(t)));
end
plot(x,y,'.r')
grid on;
grid minor;
end

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!