2D random Walker Mean displacement vectors plotting
Afficher commentaires plus anciens
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 commentaires
Guillaume
le 2 Fév 2019
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
le 2 Fév 2019
Walter Roberson
le 2 Fév 2019
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.
Réponses (0)
Catégories
En savoir plus sur 2-D and 3-D Plots dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!