- 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.
1D Random Walk for Multiple Walks
9 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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:

0 Commenti
Risposte (1)
BhaTTa
il 17 Lug 2025
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;
0 Commenti
Vedere anche
Categorie
Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
