I'm trying to simulate the movement of a pendulum by plotting a line and a marker, and then using a for loop:
L = 2 ;
g = 9.81 ;
Theta_0 = pi/3
t = 2*pi*sqrt(L/g)
cos(sqrt(g/L) * t);
Theta = Theta_0 * cos(sqrt(g/L) * t)
N_Theta = -1 * Theta
x = L * sin(Theta) ;
y = L * (1 - cos(Theta)) ;
figure(1)
hold on
axis equal
axis([-2,2,-0.5,3.5]);
for i = 1:length(t)
plot([0 x(i)], [2 y(i)], '-', 'LineWidth', 6, 'Color', 'k');
plot(x(i), y(i), 'b.', 'Markersize', 70)
drawnow
pause(0.1)
end
hold off
Ok so I've got the i thing figured out (thanks to those in the comments for your help), but now I'm trying to figure out how to make it so that I can actually see the different plotted pendulums without them stacking on top of each other. I'm pretty certain it's got to do with my placement of hold on and hold off, but I can't figure out what the right position is.
Sorry if this is really basic stuff or I've made an obvious mistake, I'm a beginner and just trying my best.
Thanks!

댓글 수: 4

Atsushi Ueno
Atsushi Ueno 2022년 4월 10일
이동: Atsushi Ueno 2022년 8월 17일
>so I'm thinking maybe I used the wrong variable for i?
Kindly. The direction of the iterator is from negative (smaller one) to positive (larger one).
L = 2 ;
g = 9.81 ;
Theta_0 = pi/3;
t = 2*pi*sqrt(L/g);
Theta = Theta_0 * cos(sqrt(g/L) * t)
Theta = 1.0472
N_Theta = -1 * Theta
N_Theta = -1.0472
for i = Theta:N_Theta
i
end
% Nothing hapen here
for i = N_Theta:Theta
i
end
i = -1.0472
i = -0.0472
i = 0.9528
Atsushi Ueno
Atsushi Ueno 2022년 4월 10일
이동: Atsushi Ueno 2022년 8월 17일
Because your iterator omits writing increments. The omitted increment becames "+1".
Theta = 1.0472;
N_Theta = -Theta;
for i = Theta:1:N_Theta
i % Nothing hapen
end
for i = Theta:-1:N_Theta
i
end
i = 1.0472
i = 0.0472
i = -0.9528
for i = N_Theta:-1:Theta
i % Nothing hapen
end
for i = N_Theta:1:Theta
i
end
i = -1.0472
i = -0.0472
i = 0.9528
VBBV
VBBV 2022년 4월 10일
The figure you have shown might have obtained with another tool, but not using the code you've posted
Isabelle Davies
Isabelle Davies 2022년 4월 10일
@VBBV yes you're right sorry, I can't even find what I did to show that. Now what I have is just the first frame showing as a figure (essentially the first plot in my question). I'll fix this now

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

도움말 센터File Exchange에서 General Applications에 대해 자세히 알아보기

제품

질문:

2022년 4월 10일

이동:

2022년 8월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by