2D Random Walk

7 views (last 30 days)
William
William on 12 Dec 2023
Answered: Walter Roberson on 12 Dec 2023
I am trying to simulate a 2D random walk.
But I keep getting errors
I dont know what I did wrong?
This is my code:
% Define the number of steps for two different runs
N1 = 1000;
N2 = 200;
% Initialize the position vectors for the first run
X1 = zeros(1, N1);
Y1 = zeros(1, N1);
% Perform the random walk for the first run
for i = 2:N1
% Generate a random direction
direction = rand(1, 2)*2*pi;
% Update the position
X1(i) = X1(i-1) + cos(direction);
Y1(i) = Y1(i-1) + sin(direction);
end
% Initialize the position vectors for the second run
X2 = zeros(1, N2);
Y2 = zeros(1, N2);
% Perform the random walk for the second run
for i = 2:N2
% Generate a random direction
direction = rand(1, 2)*2*pi;
% Update the position
X2(i) = X2(i-1) + cos(direction);
Y2(i) = Y2(i-1) + sin(direction);
end
% Plot the results for 1000 steps
figure;
subplot(2, 1, 1);
plot(X1, Y1, 'LineWidth', 2);
hold on;
plot(X2, Y2, 'LineWidth', 2);
title('2D Random Walk - Trajectories for 1000 Steps');
xlabel('X-axis');
ylabel('Y-axis');
legend('Run 1', 'Run 2');
grid on;
% Plot the histograms for 1000 steps
subplot(2, 1, 2);
histogram(X1, 'FaceColor', 'blue', 'EdgeColor', 'black', 'BinWidth', 0.5);
hold on;
histogram(Y1, 'FaceColor', 'red', 'EdgeColor', 'black', 'BinWidth', 0.5);
histogram(X2, 'FaceColor', 'cyan', 'EdgeColor', 'black', 'BinWidth', 0.5);
histogram(Y2, 'FaceColor', 'magenta', 'EdgeColor', 'black', 'BinWidth', 0.5);
title('Histograms for X and Y - 1000 Steps');
xlabel('Value');
ylabel('Frequency');
legend('X - Run 1', 'Y - Run 1', 'X - Run 2', 'Y - Run 2');
grid on;
% Repeat for 200 steps
% Initialize the position vectors for the third run
X3 = zeros(1, N2);
Y3 = zeros(1, N2);
% Perform the random walk for the third run
for i = 2:N2
% Generate a random direction
direction = rand(1, 2)*2*pi;
% Update the position
X3(i) = X3(i-1) + cos(direction);
Y3(i) = Y3(i-1) + sin(direction);
end
% Plot the results for 200 steps
figure;
subplot(2, 1, 1);
plot(X3, Y3, 'LineWidth', 2);
title('2D Random Walk - Trajectory for 200 Steps');
xlabel('X-axis');
ylabel('Y-axis');
grid on;
% Plot the histograms for 200 steps
subplot(2, 1, 2);
histogram(X3, 'FaceColor', 'blue', 'EdgeColor', 'black', 'BinWidth', 0.5);
hold on;
histogram(Y3, 'FaceColor', 'red', 'EdgeColor', 'black', 'BinWidth', 0.5);
title('Histograms for X and Y - 200 Steps');
xlabel('Value');
ylabel('Frequency');
legend('X', 'Y');
grid on;
  1 Comment
Torsten
Torsten on 12 Dec 2023
Edited: Torsten on 12 Dec 2023
What are the possible positions after one random step starting from (x,y) ?
Is it (x+1,y+1), (x+1,y-1), (x-1,y+1) and (x-1,y-1) ?
If this is the case, why do you use cos and sin functions to compute the next position ?
Or are you allowed to reach all points on the circle with radius 1 around (x,y) with equal probability ?

Sign in to comment.

Answers (2)

Voss
Voss on 12 Dec 2023
rand(1,2) produces a 1x2 vector:
rand(1, 2)
ans = 1×2
0.6280 0.2123
Seems like you want a scalar instead:
rand(1, 1) % or just rand()
ans = 0.5676
Making that change allows the code to run:
% Define the number of steps for two different runs
N1 = 1000;
N2 = 200;
% Initialize the position vectors for the first run
X1 = zeros(1, N1);
Y1 = zeros(1, N1);
% Perform the random walk for the first run
for i = 2:N1
% Generate a random direction
direction = rand()*2*pi;
% Update the position
X1(i) = X1(i-1) + cos(direction);
Y1(i) = Y1(i-1) + sin(direction);
end
% Initialize the position vectors for the second run
X2 = zeros(1, N2);
Y2 = zeros(1, N2);
% Perform the random walk for the second run
for i = 2:N2
% Generate a random direction
direction = rand()*2*pi;
% Update the position
X2(i) = X2(i-1) + cos(direction);
Y2(i) = Y2(i-1) + sin(direction);
end
% Plot the results for 1000 steps
figure;
subplot(2, 1, 1);
plot(X1, Y1, 'LineWidth', 2);
hold on;
plot(X2, Y2, 'LineWidth', 2);
title('2D Random Walk - Trajectories for 1000 Steps');
xlabel('X-axis');
ylabel('Y-axis');
legend('Run 1', 'Run 2');
grid on;
% Plot the histograms for 1000 steps
subplot(2, 1, 2);
histogram(X1, 'FaceColor', 'blue', 'EdgeColor', 'black', 'BinWidth', 0.5);
hold on;
histogram(Y1, 'FaceColor', 'red', 'EdgeColor', 'black', 'BinWidth', 0.5);
histogram(X2, 'FaceColor', 'cyan', 'EdgeColor', 'black', 'BinWidth', 0.5);
histogram(Y2, 'FaceColor', 'magenta', 'EdgeColor', 'black', 'BinWidth', 0.5);
title('Histograms for X and Y - 1000 Steps');
xlabel('Value');
ylabel('Frequency');
legend('X - Run 1', 'Y - Run 1', 'X - Run 2', 'Y - Run 2');
grid on;
% Repeat for 200 steps
% Initialize the position vectors for the third run
X3 = zeros(1, N2);
Y3 = zeros(1, N2);
% Perform the random walk for the third run
for i = 2:N2
% Generate a random direction
direction = rand()*2*pi;
% Update the position
X3(i) = X3(i-1) + cos(direction);
Y3(i) = Y3(i-1) + sin(direction);
end
% Plot the results for 200 steps
figure;
subplot(2, 1, 1);
plot(X3, Y3, 'LineWidth', 2);
title('2D Random Walk - Trajectory for 200 Steps');
xlabel('X-axis');
ylabel('Y-axis');
grid on;
% Plot the histograms for 200 steps
subplot(2, 1, 2);
histogram(X3, 'FaceColor', 'blue', 'EdgeColor', 'black', 'BinWidth', 0.5);
hold on;
histogram(Y3, 'FaceColor', 'red', 'EdgeColor', 'black', 'BinWidth', 0.5);
title('Histograms for X and Y - 200 Steps');
xlabel('Value');
ylabel('Frequency');
legend('X', 'Y');
grid on;

Walter Roberson
Walter Roberson on 12 Dec 2023
for i = 2:N1
% Generate a random direction
direction = rand(1, 2)*2*pi;
% Update the position
X1(i) = X1(i-1) + cos(direction);
Y1(i) = Y1(i-1) + sin(direction);
end
This will fail for the reasons already identified by @Voss
I would like to suggest
for i = 2:N1
% Generate a random direction
direction = randi(4,1,1)/2
% Update the position
X1(i) = X1(i-1) + cospi(direction);
Y1(i) = Y1(i-1) + sinpi(direction);
end
This will have only select from 4 positions, rather than from infinite positions.

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Tags

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!