2017-02-24 1 views
2

私はいくつかのランダムウォーカーをシミュレートしました。各ステップでの粒子を示すmatlabのランダムウォーカーの動きを提示

- 私は

プロット(XB、YB、O 'B')を用います。私はぼかしの方法で動く尾を持つ美しい粒子との下のリンクのコードを見ました。私の無作為の歩行者がマットラボのリンクの歩行者と同じようにできる方法はありますか?誰かが私が使用したプロット関数の代わりにどのようなものを使うべきか教えてもらえますか?

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を保存することができます古いプロットが徐々に暗くなり、黒にフェードするように(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を小さくするためにいくつかのフレームをトリミングする必要がありました。

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)と同じテールが表示されます。 –

関連する問題