2014-11-24 44 views
-2
%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中的循環1之後繼續循環2.因此,它將連續彈跳仿真。從1:10步驟顯示2個彈跳,但是我們希望在10步後顯示第二個循環。第二回路的Matlab繼續圖

+1

你的第二個循環會覆蓋第一個循環的所有變量。 – Rashid 2014-11-24 19:01:13

+0

我們已經知道它會覆蓋。我們只是保持這種方式來檢查我們的程序是否正常工作。那就是爲什麼我們要問如何讓第二個循環在圖中連續的第一個循環。並且將一個圖中的兩個循環同時顯示爲第一個循環的連續循環。 – user2330096 2014-11-24 21:37:53

回答

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循環,你應該是好到去!