Plotting help, function only giving one point

1 次查看(过去 30 天)
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

采纳的回答

Star Strider
Star Strider 2015-4-27
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 个评论
Japoe25
Japoe25 2015-4-27
I sort of got something weird when I ran the code :S
Star Strider
Star Strider 2015-4-27
I would change the ‘tv’ assignment to:
tv = linspace(0, 2*pi*n, 50*n);
That will smooth the plot.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graphics Performance 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by