Azzera filtri
Azzera filtri

Creating an ellipse plot with a for loop

2 visualizzazioni (ultimi 30 giorni)
wswur
wswur il 22 Mag 2017
Risposto: KSSV il 22 Mag 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

Risposte (1)

KSSV
KSSV il 22 Mag 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

Categorie

Scopri di più su 2-D and 3-D Plots in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by