【问题标题】:presenting motion of random walkers in matlab在matlab中呈现随机游走者的运动
【发布时间】:2017-07-15 07:01:49
【问题描述】:

我模拟了一些随机步行者。我用过

绘图(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

【问题讨论】:

标签: matlab matlab-figure matlab-guide


【解决方案1】:

我对“更好”的随机游走的看法:

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

【讨论】:

  • @oliverrouph MATLAB 不是图形工具,而是科学计算工具。不过,如果您花时间阅读代码,您会发现我为尾部添加了一个粗略的方法。随意修改以使尾巴更好。
  • @oliverroph 实际上,如果您取消注释代码,您将看到与 in the webpage 相同的尾部。
【解决方案2】:

如果您想创建一种粒子最近所在位置的轨迹,您可以存储以前的 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

这是一个动画:

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

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

【讨论】:

    猜你喜欢
    • 2021-10-21
    • 2014-02-19
    • 2015-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多