在matlab中呈现随机游走者的动作

时间:2017-02-24 12:33:04

标签: matlab matlab-figure matlab-guide

我模拟了一些随机游走者。我用了

  

积(XB,YB,' b - O&#39)

显示每个步骤中的粒子。我在下面的链接中看到了一个带有尾巴的美丽粒子的代码,它以模糊的方式移动。有没有办法可以让我的随机助行器与垫子实验室链接中的助行器相同?谁能告诉我应该使用哪个而不是我使用的绘图功能?

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

2 个答案:

答案 0 :(得分:4)

如果你想创建一种最近粒子所在位置的踪迹,你可以存储之前的nStore图并改变它们的颜色,以便旧图逐渐变暗并渐变为黑色(alpha透明度就像在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

我通过添加以下代码创建了动画:

% 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更小。

答案 1 :(得分: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