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

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.
The quesiton asks:
Write a program that performs M random walks of N steps in two dimensions on a square lattice, assuming that each step is chosen at random, and each of the four possible directions are equally probable.
Run the program for M~1000 walkers who are all originally at the origin. Allow the walkers to take at least 1000 steps.
Use a plotting program to draw maps of several independent random walks and discuss.
Compute the mean displacement vector by ensemble averaging xN and yN over the M random walks, for different values of N. Plot as a function of N.
xN is the mean dispalcement of the x fromt he orgin by the walker
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에 대해 자세히 알아보기

질문:

2019년 2월 2일

댓글:

2019년 2월 2일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by