clc
clear
disp('Specify the initial angle of the pendulum in degrees, e.g., 50')
disp('or press ENTER for the default value.')
theta=input('Initial angular displacement of the pendulum= ');
if isempty(theta)
theta=50
else
theta;
end
theta=theta*pi/180;
disp('Specify the final time, e.g. tfinal = 25')
disp('or press ENTER for default value.')
tfinal=input('Final time tfinal = ');
if isempty(tfinal)
tfinal=10
else
tfinal;
end
data_init=[0 0; -4 0];
R=[cos(theta) -sin(theta);sin(theta) cos(theta)];
data=R*data_init;
bar=line('xdata',[0 data(1)],'ydata',...
[0 data(2)]','linewidth',3,'erase','xor');
mass=line('xdata',data(1),'ydata',data(2),'marker','o',...
'markersize',15,'MarkerFaceColor','b','erase','xor');
hinge=line('xdata',0,'ydata',0,'marker','o',...
'markersize',7,'erase','xor');
axis([-5 5 -5 5])
grid
set(gca,'Fontsize',14)
set(gca,'dataaspectratio',[1 1 1])
box on
dt=0.03;
t=0;
thetadot=0;
disp('Can maximize the display so you can see the action better,')
disp('then press ENTER.')
disp('If you are happy with the display size, press ENTER.')
pause
while(t<tfinal)
t=t+dt;
theta = theta + thetadot*dt;
thetadot=thetadot - 5*sin(theta)*dt;
R=[cos(theta) -sin(theta);sin(theta) cos(theta)];
datanew=R*data_init;
set(bar,'xdata',[0 datanew(1)],'ydata',[0 datanew(2)]);
set(mass,'xdata',datanew(1),'ydata',datanew(2));
set(hinge,'xdata',0,'ydata',0)
set(gca,'dataaspectratio',[1 1 1])
drawnow;
end
5 Comments
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/648548-animating-a-pendulum-error-in-code#comment_1136083
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/648548-animating-a-pendulum-error-in-code#comment_1136083
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/648548-animating-a-pendulum-error-in-code#comment_1136328
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/648548-animating-a-pendulum-error-in-code#comment_1136328
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/648548-animating-a-pendulum-error-in-code#comment_1136378
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/648548-animating-a-pendulum-error-in-code#comment_1136378
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/648548-animating-a-pendulum-error-in-code#comment_1136548
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/648548-animating-a-pendulum-error-in-code#comment_1136548
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/648548-animating-a-pendulum-error-in-code#comment_1136563
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/648548-animating-a-pendulum-error-in-code#comment_1136563
Sign in to comment.