第二个循环的Matlab继续图

时间:2014-11-24 18:26:02

标签: matlab graph simulation

    %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:\Users\Mehmet\Desktop\avi\mm','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步骤显示了bouncings但我们希望在10步之后显示第二个循环。

1 个答案:

答案 0 :(得分:0)

有几种方法,取决于你的目的是什么。最简单的方法是在每个绘图命令之后简单地添加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循环中,您应该很好![/ p>