Simulating a 2D Random Walk:
Show older comments
Here is my 1D walk code:
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

I need help converting the 1D code into a 2D random walk code as instructed your help is very much apprectiated.
1 Comment
Walter Roberson
on 12 Dec 2023
Edited: Walter Roberson
on 13 Dec 2023
(which is a different poster asking the same thing)
Answers (1)
Nipun
on 18 Dec 2023
Hi Ryan,
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
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" . Hope this helps.
Regards,
Nipun
Categories
Find more on Triangular Distribution in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!