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
0 件のコメント
採用された回答
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 件のコメント
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 Center および File Exchange で Graphics Performance についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!