在 matlab 中呈现随机游走者的运动
presenting motion of random walkers in matlab
我模拟了一些随机游走者。我用了
plot(xb,yb,'b--o')
显示每个步骤中的粒子。我在下面 link 中看到一段代码,其中有美丽的粒子,尾巴以模糊的方式移动。有没有一种方法可以让我的随机行走者与 mat 实验室 link 中的行走者相同?谁能告诉我应该使用哪个而不是我使用的 plot 函数?
我试过的代码:
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
我在 "nicer" 随机游走中的投篮:
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
如果你想创建一种粒子最近去过的地方的轨迹,你可以存储以前的 nStore
图并改变它们的颜色,这样旧的图就会逐渐变暗并逐渐变黑(alpha 透明度) like in your sample is possible with line objects in 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);
我不得不 trim 出一些帧以使 gif 变小。
我模拟了一些随机游走者。我用了
plot(xb,yb,'b--o')
显示每个步骤中的粒子。我在下面 link 中看到一段代码,其中有美丽的粒子,尾巴以模糊的方式移动。有没有一种方法可以让我的随机行走者与 mat 实验室 link 中的行走者相同?谁能告诉我应该使用哪个而不是我使用的 plot 函数?
我试过的代码:
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
我在 "nicer" 随机游走中的投篮:
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
如果你想创建一种粒子最近去过的地方的轨迹,你可以存储以前的 nStore
图并改变它们的颜色,这样旧的图就会逐渐变暗并逐渐变黑(alpha 透明度) like in your sample is possible with line objects in 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);
我不得不 trim 出一些帧以使 gif 变小。