2014-11-24 3 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를 계속 진행하는 것입니다. 시뮬레이션에서는 연속적으로 튀어 오르게됩니다 .2 바운싱은 1:10 단계에서 표시되지만 10 단계 후에 두 번째 루프를 표시해야합니다.두 번째 루프에 대한 Matlab 계속 그래프

+1

두 번째 루프는 첫 번째 루프의 모든 변수를 덮어 씁니다. – Rashid

+0

우리는 이미 그것을 덮어 쓴다는 것을 알고 있습니다. 우리는 우리 프로그램이 제대로 작동하는지 확인하는 방법으로이 방법을 계속 사용합니다. thats 왜 우리가 그래프에서 첫 번째 루프의 continious 수 있도록 두 번째 루프를 만드는 방법을 묻습니다. 하나의 그래프에서 두 루프를 모두 보여주고 다시 첫 번째 루프로 계속 보여줍니다. – user2330096

답변

0

어떤 영향을 미치는지에 따라 몇 가지 방법이 있습니다. 가장 간단한 방법은 각 플롯 명령 다음에 별도의 명령으로 hold on을 추가하는 것입니다. 이렇게하면 이전 줄을 지우지 않고 새 줄/점을 계속 누적합니다. 이 효과를 중지하려면 hold off 명령을 추가하십시오. 다음 plot 명령은 모든 것을 지우고 깨끗한 그래프에 플롯합니다. 만합니다 (hold on 할 것 같은, 그리고 모두) 고정 이전 단계의 수를 표시하려면

또는 명시 적으로 이전 값에 유지해야합니다. 다른 for 루프에

% 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 

복사이 같은 생각 당신은 좋은 - 투 - 이동해야한다 : 이런 식으로 뭔가!

관련 문제