2017-02-24 5 views
2

나는 임의의 워커를 시뮬레이트했습니다. 각 단계에서 입자를 보여 - (O 'B'XB, YB)matlab에 랜덤 워커의 움직임을 표현

제가

그래프를 사용 하였다. 나는 꼬리가 흐릿하게 움직이는 아름다운 입자들과 아래 링크에있는 코드를 보았습니다. 매트 랩에서 워커와 동일한 방법으로 무작위로 보행자를 볼 수 있습니까? 아무도 내가 사용하는 플롯 기능 대신 어떤 것을 사용해야한다고 말할 수 있습니까?

beautiful particles

내가 노력 코드 :

clear all 
close all 
lbox=20; 

%random fluctuation 
eta = (2.*pi).*.1; 
vs=0.02; 
n=200; 
birdl=[1:n]; 


axis([0 lbox 0 lbox]) 
axis('square') 
hold on 
xb=rand(n,1).*lbox; %first possition 
yb=rand(n,1).*lbox; %first possition 
vxb = 1; 
vyb = 1; 

for steps=1:5000; 
xb = xb + vxb; 
yb = yb+ vyb; 

for bird1 = 1:n; 
%periodic boundary condition 
if(xb(bird1)<0);xb(bird1)=xb(bird1)+lbox; end 
if (yb(bird1)<0);yb(bird1)=yb(bird1)+lbox;end 
if (xb(bird1)>lbox);xb(bird1)=xb(bird1)-lbox;end 
if (yb(bird1)>lbox);yb(bird1)=yb(bird1)-lbox;end 

end 
ang=eta.*(rand(n,1)-0.5); 

vxb = vs.*cos(ang); 
vyb = vs.*sin(ang); 

cla 

set(gcf,'doublebuffer','on') 

plot(xb,yb,'.b') 
%quiver(xb,yb,vxb,vyb,'b') 
drawnow 
end 
+0

https://stackoverflow.com/questions/42435693/showing-simple-random-walk-resualt-when-the-code-is-running/42435925#42435925 –

답변

4

입자가 최근에 있었던 곳의 흔적, 당신은 prev 오래된 플롯이 점차 어두워지고 검은 색으로 희미 해 지도록 색상을 변경하십시오 (샘플의 알파 투명도는 MATLAB에서 선 객체로는 불가능합니다). 여기에 코드의 재 작업 (몇 가지 다른 개선 사항은 인덱싱 내부 경계 조건 루프 교체처럼)입니다 :

clear all 
close all 
lbox = 20; 

%random fluctuation 
eta = (2.*pi).*1; 
vs = 0.05; 
n = 200; 

set(gcf, 'doublebuffer', 'on', 'Color', 'k'); 
set(gca, 'Visible', 'off'); 
axis([0 lbox 0 lbox]) 
axis('square') 
hold on 
xb = rand(n, 1).*lbox; %first possition 
yb = rand(n, 1).*lbox; %first possition 
vxb = 1; 
vyb = 1; 

hList = []; 
nStore = 30; 
cMap = [zeros(nStore+1, 1) linspace(1, 0, nStore+1).' zeros(nStore+1, 1)]; 

for steps = 1:200 

    xb = xb + vxb; 
    yb = yb + vyb; 

    %periodic boundary condition 
    index = (xb < 0); 
    xb(index) = xb(index) + lbox; 
    index = (yb < 0); 
    yb(index) = yb(index) + lbox; 
    index = (xb > lbox); 
    xb(index) = xb(index) - lbox; 
    index = (yb > lbox); 
    yb(index) = yb(index) - lbox; 

    ang = eta.*(rand(n,1)-0.5); 

    vxb = vs.*cos(ang); 
    vyb = vs.*sin(ang); 

    h = plot(xb, yb, '.g', 'MarkerSize', 12); 
    if (numel(hList) == nStore) 
    delete(hList(nStore)); 
    hList = [h hList(1:end-1)]; 
    else 
    hList = [h hList]; 
    end 

    set(hList, {'Color'}, num2cell(cMap(1:numel(hList), :), 2)); 

    drawnow 
end 

을 그리고 여기에 애니메이션이다 : 나는하여 애니메이션을 만들어

enter image description here

다음 코드를 추가하십시오.

gif를 더 작게 만들려면 일부 프레임을 잘라내야했습니다.

2

"더 좋은"랜덤 워크 (random walk)에서 내 샷 : 당신은 일종의을 만들려면

clear all 
close all 
lbox=20; 

figure('Color',[0 0 0]) 

%random fluctuation 
eta = (2.*pi).*1; 
vs=0.02; 
n=300; 
birdl=[1:n]; 


axis([0 lbox 0 lbox]) 
axis('square') 
hold on 
xb=rand(n,1).*lbox; %first possition 
yb=rand(n,1).*lbox; %first possition 
vxb = 1; 
vyb = 1; 

for steps=1:5000; 
    xb = xb + vxb; 
    yb = yb+ vyb; 

    for bird1 = 1:n; 
     %periodic boundary condition 
     if (xb(bird1)<0);xb(bird1)=xb(bird1)+lbox; end 
     if (yb(bird1)<0);yb(bird1)=yb(bird1)+lbox;end 
     if (xb(bird1)>lbox);xb(bird1)=xb(bird1)-lbox;end 
     if (yb(bird1)>lbox);yb(bird1)=yb(bird1)-lbox;end 

    end 
    ang=eta.*(rand(n,1)-0.5); 

    vxb = vs.*cos(ang); 
    vyb = vs.*sin(ang); 

    cla 
    set(gca,'Color',[0 0 0]); 
    set(gcf,'doublebuffer','on') 
    set(gca,'YTick',[]); 
    set(gca,'XTick',[]); 

    plot(xb,yb,'.g','markersize',10) 
    % this should draw lines, but its slow and not as neat as a web app 
%  plot([xb xb-vxb*5]',[yb yb-vyb*5]','g') 

    drawnow 
end 

enter image description here

+0

@oliverrouph MATLAB은 그래픽 도구가 아닙니다. 과학적 컴퓨팅. 그래도 코드를 읽을 시간이 있다면 꼬리에 대한 대략적인 접근법을 추가 한 것을 볼 수 있습니다. 그 꼬리를 더 좋게 만들기 위해 기쁘게 수정하십시오. –

+0

@oliverrouph 실제로 코드의 주석 처리를 제거하면 [웹 페이지] (http://allanino.me/vicsek-model-simulation/?N=500&n=2.0&v=0.2)와 동일한 꼬리가 나타납니다. –