CODE:
% % % % % 内容是:做一个长方体先以5m/s做匀速运动,
% % % % % 后以-2.4m/s2的加速度做减速运动的动画。
clear;clc;close all
dt=0.01;
t0=0;
t1=2;
t2=3.5;
t=t0:dt:t2;
v=5*(t<=t1&t>=t0)+(5-2.4*(t-t1)).*(t>t1&t<=t2);
y=5*t.*(t<=t1&t>=t0)+(5*t1+5*(t-t1)-2.4/2*(t-t1).^2).*(t>t1&t<=t2);
figure1=figure('color','w');
axes1=axes('parent',figure1);
% set(gca,'visible','off')
set(gca,'ytick',[]);
xlim([0 20]);
ylim( [-4 8]);hold on;axis equal
locx=[0 0 2 2];
locy=[0 1 1 0];
h=fill(locx,locy,'r');
ht1=text(3,8,sprintf('Time = %3.2f',t0),'fontname','times new roman');
ht2=text(3,7,sprintf('Velocity = %3.2f',v(1)),'fontname','times new roman');
for jj=1:length(y)
set(h,'Xdata',locx+y(jj));
set(ht1,'string',sprintf('Time = %3.2f',t(jj)),'fontname','times new roman');
set(ht2,'string',sprintf('Velocity = %3.2f',v(jj)),'fontname','times new roman');
drawnow;
pause(0.03)
end