%free fall of a ball
clc
clear all
close all
v0=5; % initial velocity up
g=9.8; %free fall acceleration
v1=(0.7/0.9)*v0
% time of fly
tup=v0/9;
nsteps=10; %number of frames
dt=tup/nsteps; %time step
Hmax=v0*tup+(-g)*tup*tup/2; % maximum altitude
altitude(1:nsteps+1)=0; %define array for position Y
time=0:dt:tup;% define time array
%initilaise plot
figure(1)
axis([0,2*tup,0,2*Hmax]);
hold on
% loop
for i=1:nsteps
altitude(i)=v0*time(i)+(-g)*time(i)*time(i);
plot(time(i),altitude(i), 'ro')
grid on;
M(i)=getframe;
end
%loop bouncing
for i=1:nsteps
altitude(i)=v1*time(i)+(-g)*time(i)*time(i);
plot(time(i),altitude(i), 'ro')
grid on;
M(i)=getframe;
end
%make movie
movie(M);
movie2avi(M, 'C:UsersMehmetDesktopavimm','compression','none');
%extra plots
figure(2)
plot(time(1:nsteps),altitude(1:nsteps))
figure(3)
plot(time(1:nsteps),altitude(1:nsteps),'ro')
我们有这个球弹跳模拟。我们想要做的是,在图中的循环 1 之后继续循环 2。因此,这将是连续弹跳模拟。2 从 1:10 步开始显示弹跳,但我们希望在 10 步后显示第二个循环。
有几种方法,具体取决于您要达到的效果。 最简单的方法是在每个绘图命令之后简单地将hold on
添加为单独的命令。 这将不断积累新线/点而不会擦除旧线/点。 若要停止此效果,请添加 hold off
命令。 然后,下一个plot
命令将擦除所有内容并绘制在干净的图形上。
或者,如果您只想显示固定数量的前面步骤(而不是所有步骤,因为hold on
将这样做),则必须显式保留前面的值。 像这样:
% loop
max_history = 10; %plot this many symbols
for i=1:nsteps
altitude(i)=v0*time(i)+(-g)*time(i)*time(i);
%get the historical and current data points to plot
start_ind = i-max_history+1; %here's the oldest point that you'd like
start_ind = max([start_ind, 1]) %but you can't plot negative indices
inds = [start_ind:i]; %here are the ones to plot
%update the plot
plot(time(inds),altitude(inds), 'ro');
grid on;
%you might need a "drawnow" here
M(i)=getframe;
end
将同样的想法复制到您的其他for
循环中,您应该很高兴!