2017-02-24 29 views
2

我模拟了一些随机步行者。我用在matlab中呈现随机步行者的运动

情节(XB,YB, 'B - O')

显示在每个步骤中的颗粒。我看到下面的代码与美丽的尾巴链接,模糊地移动。有没有一种方法可以让我的随机步行者像坐在实验室中的步行者一样?任何人都可以告诉我应该使用哪一个来代替我使用的绘图函数?

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 ious nStore绘制并改变它们的颜色,以便老的绘图逐渐变暗并褪色成黑色(在MATLAB中,线样本中不可能使用像样本中的alpha透明度)。这是你的代码的重构(与其他一些改进,如更换与索引内边界条件环路):

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

我所创造的动画添加下面的代码:

% After the drawnow... 
frame = getframe(gca); 
im = frame2im(frame); 
imind(:, :, 1, steps) = uint8(rgb2ind(im, cMap, 'nodither')); 

% After the loop... 
imwrite(imind(:, :, 1, 1:2:end), cMap, 'randwalk.gif', ... 
     'Loopcount', Inf, 'DelayTime', 0); 

我不得不修剪一些帧,使gif变小。

2

我射击在 “更好的” 随机漫步:

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)。 –