%轻质杆两端小球和质心运动动画
clear,g=10;v0=12;
theta=input('请输入抛射角:');th=theta*pi/180;
w=10*pi;l=1;m1=0.2;m2=0.3;
r1=l*m2/(m1+m2);r2=l*m1/(m1+m2);
t0=2*v0*sin(th)/g;n=200;t=linspace(0,t0,n);
xc=v0*cos(th)*t;yc=v0*sin(th)*t-g*t.*t/2;
x1=xc+r1*cos(2*t);y1=yc+r1*sin(w*t);
x2=xc-r2*cos(2*t);y2=yc-r2*sin(w*t);
figure
h12=plot([x1(1);x2(1)],[y1(1);y2(1)],'-o','LineWidth',3);%画杆并取句柄
axis([-0.5,xc(end)+1,-1.5,max(yc)+1]),axis equal
fs=16;title('轻质杆两端小球和质心的斜抛运动','FontSize',fs)
if theta==90,title('轻质杆两端小球和质心的上抛运动','FontSize',fs),end
xlabel('\itx\rm/m','FontSize',fs)
xlabel('\ity\rm/m','FontSize',fs)
grid on,hold on,pause
for i=1:n-1
set(h12,'XData',[x1(i);x2(i)],'YData',[y1(i);y2(i)]);
plot([xc(i);xc(i+1)],[yc(i);yc(i+1)],'LineWidth',2)
plot([x1(i);x1(i+1)],[y1(i);y1(i+1)],'k')
plot([x2(i);x2(i+1)],[y2(i);y2(i+1)],'r')
if floor((i-1)/20)==(i-1)/20
plot([x1(i);x2(i)],[y1(i);y2(i)],'LineWidth',2) %重画杆
plot(x1(i),y1(i),'ko'),plot(x2(i),y2(i),'ro') %重画球
end,drawnow
end