Plotting help, function only giving one point

6 visualizaciones (últimos 30 días)
Japoe25
Japoe25 el 27 de Abr. de 2015
Comentada: Star Strider el 27 de Abr. de 2015
Hello friends,
I'm trying to recreate the image of a spiral staircase attached but I cant seem to get my code working. I just get a blank graph. I don't know how my loop is just giving me one point. Please help!
function z=my_staircase(a,b,h,n)
for t=0:2*pi*n
r=((a*b)*exp(-.04*t))/sqrt((b*cos(t)^2)+ (a*sin(t))^2);
x=r*cos(t);
y=r*sin(t);
z=(h*t)/(2*pi*n);
end
hold on
plot3(x,y,z)
xlabel('x(m)'); ylabel('y(m)');zlabel('z(m)');
grid on

Respuesta aceptada

Star Strider
Star Strider el 27 de Abr. de 2015
It’s giving you one point because that’s all you’re asking it to do.
A few tweaks, including subscripting ‘x’, ‘y’ and ‘z’ and all will be well:
function z = my_staircase(a,b,h,n)
tv = 0:2*pi*n;
for k1 = 1:length(tv)
t = tv(k1);
r=((a*b)*exp(-.04*t))/sqrt((b*cos(t)^2)+ (a*sin(t))^2);
x(k1)=r*cos(t);
y(k1)=r*sin(t);
z(k1)=(h*t)/(2*pi*n);
end
hold on
plot3(x,y,z)
xlabel('x(m)'); ylabel('y(m)');zlabel('z(m)');
grid on
view([-30 30])
end
Changing the view parameters (I added that) help too.
  2 comentarios
Japoe25
Japoe25 el 27 de Abr. de 2015
I sort of got something weird when I ran the code :S
Star Strider
Star Strider el 27 de Abr. de 2015
I would change the ‘tv’ assignment to:
tv = linspace(0, 2*pi*n, 50*n);
That will smooth the plot.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by