clear; clc; close all;
% Set initial conditions
v0 = 12; theta = 70; w = 10*pi;
m1 = 0.2; m2 = 0.3; L = 1;
% Necessary calculation
g = 10; th = theta*pi/180;
r1 = L*m2/(m1+m2); r2 = L*m1/(m1+m2);
t_end = 2*v0*sin(th)/g;
% Get result data
fn = 100;
t = linspace(0,t_end,fn);
xc = v0*cos(th)*t; % Centroid coordinate
yc = v0*sin(th)*t-g*t.*t/2;
x1 = xc+r1*cos(w*t); % Ball 1 coordinate
y1 = yc+r1*sin(w*t);
x2 = xc-r2*cos(w*t); % Ball 2 coordinate
y2 = yc-r2*sin(w*t);
% Plot the result
fig = figure;
plot([-2,-2,xc(end)+2,xc(end)+2,-2],[-2,max(yc)+2,max(yc)+2,-2,-2]);
set(fig,'DoubleBuffer','on');
set(gca,'NextPlot','replace','Visible','off');
grid off; axis equal; axis off; hold on;
h12 = plot([x1(1);x2(1)],[y1(1);y2(1)],'o-b','LineWidth',2,'MarkerSize',5);
title('直杆斜抛运动过程','FontSize',16);
pause;
for i=1:fn-1
F(i) = getframe(fig);
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);
if floor((i-1)/10)==(i-1)/10
plot([x1(i);x2(i)],[y1(i);y2(i)],'LineWidth',2);
plot(x1(i),y1(i),'ko','MarkerSize',5);
plot(x2(i),y2(i),'ro','MarkerSize',5);
end
drawnow;
end
% Save to avi file
mov = avifile('mov1.avi');
mov = addframe(mov,F);
mov = close(mov);