在matlab中呈现随机助步器的运动 [英] presenting motion of random walkers in matlab

查看:102
本文介绍了在matlab中呈现随机助步器的运动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have simulated some random walkers. I used

图(xb,yb,'b--o')

plot(xb,yb,'b--o')

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

to show particles in each step. I saw a code in below link with beautiful particles with tail which moves in a blur way. Is there a way which I could my random walkers the same as the walkers in the link in mat lab? Could anyone tell me which should I use instead of the plot function I used?

美丽的粒子

我尝试的代码:

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

推荐答案

如果要创建粒子最近所在位置的轨迹,则可以存储以前的nStore图并更改其颜色,以便较旧曲线逐渐变暗并逐渐变黑(MATLAB中的线对象无法实现示例中的alpha透明度).这是对代码的重做(还有一些其他改进,例如用索引替换内部边界条件循环):

If you want to create a sort of trail of where the particles have recently been, you can store the previous nStore plots and change their color so that older plots gradually darken and fade to black (alpha transparency like in your sample isn't possible with line objects in MATLAB). Here's a reworking of your code (with a few other improvements, like replacing the inner boundary condition loop with indexing):

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

这是一个动画:

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

I created the animation by adding the following code:

% 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变小.

I had to trim out some frames to make the gif smaller.

这篇关于在matlab中呈现随机助步器的运动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆