Creating a 2D random walk

18 views (last 30 days)
Ella Yeo
Ella Yeo on 28 May 2019
Commented: Image Analyst on 28 Nov 2021
I'm trying to create 2D random walk, (up,down,left and right)
I want to store every results into a empty vector x and y and mark the final position in a graph.
i've done this so far but it's not storing every single steps into a vector.
what did I do wrong?
steps_r=0; %number of steps to the right
steps_l=0; %numbers of steps to the left
steps_u=0;
steps_d=0;
n=5; %number of turns
x=[];
y=[];
start_x=0; %total number of steps in x-axis to finalise the coordinate
start_y=0;
for i =1:n
p_x=rand(1) %probability of x
p_y=rand(1) %probability of y
if p_x<0.6
start_x = start_x-1
x=[start_x]
steps_l=steps_l+1
elseif p_x>=0.6
start_x=start_x+1
x=[start_x]
steps_r=steps_r+1
elseif p_y<0.7
start_y= start_y+1
y=[start_y]
steps_u=steps_u+1
else
start_y=start_y-1
y=[start_y]
steps_d=steps_d+1
end
plot(x(1),y(1))
title('Position of a random walk')
xlabel('x')
ylabel('y')
end

Answers (2)

Image Analyst
Image Analyst on 28 May 2019
Sorry I don't have time to analyze your code now, but in the meantime, you might want to take a look at my attached collection of random walk demos. They are well commented.
  2 Comments
Ahmed Salman
Ahmed Salman on 27 Nov 2021
Hi,
Can plot of the walker's position as a function of time in order to prove if the walker is truly random and not skewed by using comet() function.
How to run this simulation X times to generate an ensemble (a collection of simulations) in for example a family with 20 kids is an ensemble and each child is unique and has its own "destiny" when i have family of X simulations and each simulation will take 4000 steps assume X=200 that's 200 simulation and each simulation has 4000 steps. Can record or store the final location of the walker at the end of each simulation in the ensemble and generate a histogram for the ending position of the walker at the end of each simulation in the ensemble.
I wish you have a good day.
Thanks.
Image Analyst
Image Analyst on 28 Nov 2021
@Ahmed Salman you can use rand() or randi() and some for loops. Like
X = 200;
numSteps = 4000;
numChildren = 20;
distances = zeros(X, numChildren);
steps = rand(numSteps, numChildren); % or randi() if you want integers.
for k = 1 : X
% Run each child one at a time. Their steps are in the columns.
for c = 1 : numChildren
% Get the steps for this child.
theseSteps = steps(:, c);
% Do the walk somehow.
% Code for you to finish. Compute distance (say
% it's called "thisDistance) from origin or whatever
% you want.
% Now store the final result.
distances(k, c) = thisDistance;
end
end

Sign in to comment.


James Tursa
James Tursa on 28 May 2019
Edited: James Tursa on 28 May 2019
In this logic, you will never get to the y branches because one of the first two x branches will always be true:
if p_x<0.6
:
elseif p_x>=0.6
:
elseif p_y<0.7
:
else
:
end
You could separate them as follows:
if p_x<0.6
:
else
:
end
if p_y<0.7
:
else
:
end
But this will always give you a diagonal step. Maybe that is what you want, but I rather doubt it.
I suspect what you really want is to randomly change x or y but not both at the same time? In that case, simply assign probabilities to each possible step (+x, -x, +y, -y) such that they all add up to 1, and then use a variation of your current branching to do the stepping. I.e., at each iteration you pick one random number and then do one of the four possible steps based on that random value.

Community Treasure Hunt

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

Start Hunting!