How to animate two pendulums in one figure?

2 次查看(过去 30 天)
Im working on a project for my matlab class. I would like to animate multiple pendulums each with a different length on the same figure. I have seen this done by using simple sin and cos values for the x and y positions. Does anyone have some helpful input or could show me how to do this? Even some similar examples would be great. Here is the code i have, it is currently just a single pendulum.
function project
close all
clear all
%user input
g = 9.8;
L = 1;
L1 = 1.5
m = 1;
alpha = 0.2;
y0Deg = [45 0];
tSpan = [0:.1:30];
%tSpan = [0 30];
y0Rad = y0Deg*pi/180;
[T,Y] = ode45(@pend3,tSpan,y0Rad,[],g,L,m,alpha);
%time series
%plot(T,Y(:,1),'k')
%xlabel('time (s)')
%ylabel('\theta (rad)')
%phase portrait
%figure
%plot(Y(:,1),Y(:,2),'k')
%xlabel('\theta_1')
%ylabel('\theta_2','rotation',0)
animatePendulum(T,Y,L);
%animatePendulum(T,Y,L1);
%animatePendulum(C,F,L1);
hold on
function dydt = pend3(t,y,g,L,mass,alpha)
dydt = zeros(2,1);
dydt(1) = y(2);
dydt(2) = -alpha*y(2)-mass*g/L*sin(y(1));
function animatePendulum(T,Y,L)
nT = length(T);
figure
for ind1 = 1:nT
xTip = L*sin(Y(ind1,1));
yTip = -L*cos(Y(ind1,1));
%xTip1 = (L+.5)*sin(Y(ind1,1));
%yTip1 = (-L-.5)*cos(Y(ind1,1));
plot(0,0,'k.','markersize',15)
hold on
plot([0 xTip],[0 yTip],'k')
plot(xTip,yTip,'ko','markersize',15)
%plot([0 xTip1],[0 yTip1],'k')
%plot(xTip1,yTip1,'ko','markersize',15)
hold off
axis equal
axis([-1 1 -1.8 .2])
drawnow
end

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by