创建图表以显示在三个不同时间点的大量2D随机游走的空间分布 [英] Creating graphs that show the distribution in space of a large number of 2D Random Walks at three different time points

查看:112
本文介绍了创建图表以显示在三个不同时间点的大量2D随机游走的空间分布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,基本上,我在这里有这段代码,可以用来沿着N个步长和M个助步器离散地生成2D随机游走.我可以将它们全部绘制在同一张图上.

So essentially I have this code here that I can use to generate a 2D Random Walk discretely along N number of steps with M number of walkers. I can plot them all on the same graph here.

clc;
clearvars;
N = 500; % Length of the x-axis, also known as the length of the random walks.
M = 3; % The amount of random walks.
x_t(1) = 0;
y_t(1) = 0;
for m=1:M
    for n = 1:N % Looping all values of N into x_t(n).
        A = sign(randn); % Generates either +1/-1 depending on the SIGN of RAND.
        x_t(n+1) = x_t(n) + A;
        A = sign(randn); % Generates either +1/-1 depending on the SIGN of RAND.
        y_t(n+1) = y_t(n) + A;
    end
    plot(x_t, y_t);
    hold on
end
grid on;
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0.05, 1, 0.95]);
axis square;

现在,我希望能够创建图形来显示大量位置在空间中的分布 (例如n = 1000)个随机步行者,分别在三个不同的时间点(例如t = 100、200和300或实际上是任何三个时间点).

Now, I want to be able to Create graphs that show the distribution in space of the positions of a large number (e.g. n = 1000) random walkers, at three different time points (e.g. t = 100, 200 and 300 or any three time points really).

我不确定如何执行此操作,我需要将其转换为函数,并在三个不同的时间对其进行迭代并存储坐标?我有一个大概的主意,但对实际实施持怀疑态度.我认为最安全,最不混乱的方法是使用subplot()在同一图中一起创建所有三个图.

I'm not sure how to go about this, I need to turn this into a function and iterate it through itself three different times and store the coordinates? I have a rough idea but iffy on actually implementing. I'd assume the safest and least messy way would be to use subplot() to create all three plots together in the same figure.

感谢任何帮助!

推荐答案

您可以使用cumsum线性化该过程.基本上,您只想累加由[-1和1]组成的随机矩阵.

You can use cumsum to linearize the process. Basically you only want to cumsum a random matrix composed of [-1 and 1].

clc;
close all;
M = 50; % The amount of random walks.
steps = [10,200,1000]; % here we analyse the step 10,200 and 1000
cc = hsv(length(steps)); % manage the color of the plot
%generation of each random walk
x = sign(randn(max(steps),M));
y = sign(randn(max(steps),M));
xs = cumsum(x);
xval = xs(steps,:);
ys = cumsum(y);
yval = ys(steps,:);

hold on
for n=1:length(steps)
    plot(xval(n,:),yval(n,:),'o','markersize',1,'color',cc(n,:),'MarkerFaceColor',cc(n,:));
end

legend('10','200','1000')
axis square
grid on;

结果:

感谢@LuisMendo回答了我的问题

Thanks to @LuisMendo that answered my question here, you can use a binomial distribution to get the same result:

steps = [10,200,10000];
cc = hsv(length(steps)); % manage the color of the plot
M = 50;
DV = [-1 1];
p = .5; % probability of DV(2)
% Using the @LuisMendo binomial solution:
for ii = 1:length(steps)
    SDUDx(ii,:) = (DV(2)-DV(1))*binornd(steps(ii), p, M, 1)+DV(1)*steps(ii);
    SDUDy(ii,:) = (DV(2)-DV(1))*binornd(steps(ii), p, M, 1)+DV(1)*steps(ii);
end
hold on
for n=1:length(steps)
    plot(SDUDx(n,:),SDUDy(n,:),'o','markersize',1,'color',cc(n,:),'MarkerFaceColor',cc(n,:));
end
legend('10','200','1000')
axis square
grid on;

有什么好处?即使您有很多步骤,比如说1000000,matlab也可以处理它.因为在第一个解决方案中,您有一个蛮力解决方案,在第二个情况中,您是一个统计解决方案.

What is the advantage ? Even if you have a big number of steps, let's say 1000000, matlab can handle it. Because in the first solution you have a bruteforce solution, and in the second case a statistical solution.

这篇关于创建图表以显示在三个不同时间点的大量2D随机游走的空间分布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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