1D Random Walk for Multiple Walks

조회 수: 9 (최근 30일)
bk9889
bk9889 2019년 4월 6일
답변: BhaTTa 2025년 7월 17일
I am trying to create a plot of < x^2 > versus n for a 1D random walk beginning at the origin with n = 100 steps and averaged over nwalk = 10^4 walks, which should, according to my textbook (Ch.7 of Computational Physics, 2nd Edition by Giordano and Nakanishi), more or less fit a linear slope through the origin. When I create my plot, my resulting graph more closely resembles a square-root of x graph, as can be seen below:
Here is the code:
close all;
clc;
n = 100; % number of steps
nwalk = 10000; % number of walks
X = zeros(nwalk,n);
X2avg = zeros(n);
% r = rand();
for j = 1:nwalk
X(j,1) = 0; % initial position
r = rand(); % getting random number to decide step left or step right
for i = 2:n
if r < 0.5
X(j,i) = X(j,i-1)+1; % step to the right
elseif r > 0.5
X(j,i) = X(j,i-1)-1; % step to the left
end
X2avg(i) = X2avg(i) + X(j,i)^.2; % accumulating squared displacement
end
end
for i = 2:n % normalizing the squared displacement
X2avg(i) = X2avg(i)/nwalk;
end
plot(1:n,X2avg);
xlabel('Step Number (Time)');
ylabel('<x^2>');
title('1-Dimensional Random Walk');
Also, when I run this I get the following warning, and I am unsure if this is why my graph is different from what I expected:

답변 (1개)

BhaTTa
BhaTTa 2025년 7월 17일
Hey @bk9889, i can see some issues in your code:
  • You have X2avg(i) = X2avg(i) + X(j,i)^.2;
  • The notation <x^2> means the average of the square of the displacement. You are currently raising X(j,i) to the power of 0.2 (which is the fifth root).
  • Correction: This should be X(j,i)^2.
Below i have attached correct implementations, please take it as reference and modify accordingly:
close all;
clc;
n = 100; % number of steps
nwalk = 10000; % number of walks
% Initialize X to store positions for each walk and step
% X(j,i) will be the position of walk 'j' at step 'i'
X = zeros(nwalk, n);
% Initialize X2avg to store the sum of squared displacements for each step
% This should be a 1D array, one element for each step number.
X2avg = zeros(1, n);
for j = 1:nwalk
X(j,1) = 0; % initial position for the current walk at step 1
% Accumulate squared displacement for the initial position (step 1)
% This will always be 0^2 = 0, but it's good practice for consistency.
X2avg(1) = X2avg(1) + X(j,1)^2;
for i = 2:n
% Generate a new random number for EACH step
r = rand();
if r < 0.5
X(j,i) = X(j,i-1) + 1; % step to the right
else % r >= 0.5 (covers the case for stepping left)
X(j,i) = X(j,i-1) - 1; % step to the left
end
% Accumulating the SQUARED displacement (X(j,i)^2)
X2avg(i) = X2avg(i) + X(j,i)^2;
end
end
% Normalize the accumulated squared displacement by the number of walks
for i = 1:n % Loop through all step numbers, including the first
X2avg(i) = X2avg(i) / nwalk;
end
% Plotting
figure;
plot(0:n-1, X2avg, 'b-o', 'LineWidth', 1.5, 'MarkerSize', 4); % Plot from step 0 to n-1
xlabel('Step Number (n)');
ylabel('Average Squared Displacement <x^2>');
title('1-Dimensional Random Walk');
grid on;
% Theoretical expectation: <x^2> = n
hold on;
plot(0:n-1, 0:n-1, 'r--', 'LineWidth', 1.5, 'DisplayName', 'Theoretical <x^2> = n');
legend('Simulation Result', 'Theoretical Expectation', 'Location', 'northwest');
hold off;

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by