Plotting help, function only giving one point

조회 수: 7 (최근 30일)
Japoe25
Japoe25 2015년 4월 27일
댓글: Star Strider 2015년 4월 27일
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에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by