Clear Filters
Clear Filters

Trouble making my for loop work

1 view (last 30 days)
Isabelle Davies
Isabelle Davies on 10 Apr 2022
Moved: Atsushi Ueno on 17 Aug 2022
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)) ;
hold on
axis equal
for i = 1:length(t)
plot([0 x(i)], [2 y(i)], '-', 'LineWidth', 6, 'Color', 'k');
plot(x(i), y(i), 'b.', 'Markersize', 70)
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.
VBBV on 10 Apr 2022
The figure you have shown might have obtained with another tool, but not using the code you've posted
Isabelle Davies
Isabelle Davies on 10 Apr 2022
@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

Sign in to comment.

Answers (0)


Find more on Graphics Performance in Help Center and File Exchange


Community Treasure Hunt

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

Start Hunting!