Simulating a 2D Random Walk
10 views (last 30 days)
Show older comments
How can I turn my 1D Random Walk code into a 2D Random Walk.
Heres my code for the 1D Random walk:
N = 100; % Number of steps
single_trajectory = simulateRandomWalk(N);
figure
plot(0:N, single_trajectory, '-o')
title('1D Random Walk - Single Trajectory')
xlabel('Steps')
ylabel('Position')
P_values = [50, 500, 5000, 50000, 500000];
for p_index = 1:length(P_values)
P = P_values(p_index);
final_positions = zeros(1, P);
for i = 1:P
single_trajectory = simulateRandomWalk(N);
final_positions(i) = single_trajectory(end);
end
% Plot histogram of final positions
figure
histogram(final_positions, 'Normalization', 'probability')
title(['Histogram for P = ' num2str(P)])
xlabel('Final Position')
ylabel('Probability')
end
function trajectory = simulateRandomWalk(N)
% Initialize position
position = 0;
% Initialize trajectory array
trajectory = zeros(1, N+1);
trajectory(1) = position;
% Simulate random walk
for step = 1:N
% Generate a random number to decide left or right movement
move = randi([0, 1])*2 - 1; % -1 for left, 1 for right
% Update position
position = position + move;
% Store the current position in the trajectory array
trajectory(step+1) = position;
end
end

Answers (1)
Nipun
on 18 Dec 2023
Hi William,
I understand that you have one dimensional random walk code and intend to modify the given snippet to yield a two dimensional random walk simulation.
Based on the function "simulateRandomWalk", I assume that for two dimensional random walk, the walker is free to move left, right, up, down
A similar question was answered here: Simulating a 2D Random Walk: - MATLAB Answers - MATLAB Central (mathworks.com)
I will start by changing the "simulateRandomWalk" code to include position changes in Y-coordinate.
function trajectory = simulateRandomWalk(N, position)
%% the starting X and Y coordinates are in position structure
% position has properties: X, Y
% Initialize trajectory array
%% the trajectory is a structure with X and Y coordinates
trajectory.X = zeros(1, N+1);
trajectory.Y = zeros(1,N+1);
trajectory.X(1) = position.X;
trajectory.Y(1) = position.Y;
% Simulate random walk
for step = 1:N
%% Generate a random number to decide left, right, up, down
% generate a random number between 1 and 4
% 1 = left, 2 = right, 3 = up, 4 = down
pos = randi([1,4]);
switch pos
case 1
% left
position.X = position.X - 1;
case 2
% right
position.X = position.X + 1;
case 3
% up
position.Y = position.Y + 1;
case 4
% down
position.Y = position.Y - 1;
end
%% Store the current position in the trajectory array
% update both coordinates
trajectory.X(step+1) = position.X;
trajectory.Y(step+1) = position.Y;
end
Now, to plot the random walk, we can use "plot" function, similar to the one dimensional random walk.
% starting coordinates
start.X = 0;
start.Y = 0;
steps = 1000;
t = simulateRandomWalk(steps, start);
%% plot
% Indicate starting position with a circle
plot(0,0,'-ro');
hold on;
plot(t.X,t.Y,'-b');
title("Random walk with " + num2str(steps) + " steps");
Here's an output of the code:

Similarly, use the histogram function to map a histogram of "t.X" and "t.Y"
hold off;
histogram(t.X);
histogram(t.Y);
Refer to the following links for more information on MATLAB plot and histogram function. Hope this helps.
- MATLAB line plot: https://www.mathworks.com/help/matlab/ref/plot.html
- MATLAB histogram : https://www.mathworks.com/help/matlab/ref/matlab.graphics.chart.primitive.histogram.html
Regards,
Nipun
0 Comments
See Also
Categories
Find more on Labeling, Segmentation, and Detection in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!