2D random Walker Mean displacement vectors plotting
显示 更早的评论
I am trying to plot the mean squre vectors x_n that is the number of steps in the x-direction and y_n that is the number of steps in the y-direction as a function of M the number of steps by a walker. N is the number of walkers. This is a 2D random walker on a square lattice problem. What code should I use? Thank you
clc;
clearvars;
N = 10;
M = 10; % 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
pot([0:M],x_t)
hold on
end
grid on;
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0.05, 1, 0.95]);
axis square;
3 个评论
Guillaume
2019-2-2
I don't understand your question. M is the number of walks, not the number of steps (this is N). Perhaps, if you used meaningful names for your variables (such as numsteps and numwalks) you wouldn't get confused.
What is the mathematical formula of what you want to calculate?
Also,
A = sign(randn); % Generates either +1/-1
That comment is wrong. randn can generate a 0 (that's the most likely number but, granted, the probability is still very very low). The sign of 0 is 0. Therefore A can be -1, 0 or 1.
Benjamin Puzantian
2019-2-2
Walter Roberson
2019-2-2
If randn were capable of infinite precision ,then the probability of generating an exact 0 would be mathematically 0 (as in 1 out of infinity, not as in 0 out of infinity.) However, randn is not capable of infinite precision, and so 0 does end up with a slightly higher probability than any other representable number. Still very small.
回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 2-D and 3-D Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!