# 1D randomwalk matlab tutorial on youtube

2 views (last 30 days)
Dilpuneet Grewal on 20 Oct 2021
Edited: John D'Errico on 21 Oct 2021
so i started learning matlab by myself, and looked up a few beginners courses on it. eventually i stumbled upon the youtube channel for matlab, and upon digging, found out this video: https://www.youtube.com/watch?v=zr_aB7V79DE&list=PL7CAABC40B2825C8B&index=3
the person doesnt explain how to code the randomwalk function. (8:40 for the timestamp)
i have a rough idea of how it would be coded but dont yet understand how the stuff works.
i tried using "for" in place of "while" but that didnt work wither. any insights on the matter wouldbe greaty appreciated. thanks :) edit:
this randomwalk function is to be designed such that 'x' is the number of steps from the starting point to the finish. like a coin flip, having a 0.5 chance to take 1 step in either direction after each iteration. the input for the function being the distance from the starting point to the finish, and the output being the number of coin flips (randi([0,1]) iterations in this case) it took such that cumulative sum = x (assuming heads = 1, tails = -1) for the given sequence of coin flips (randi([0,1]) iterations).
edit :2
got it working after several trial and error runs. thanks for helping me out! John D'Errico on 20 Oct 2021
Edited: John D'Errico on 20 Oct 2021
I think you need to read things like the syntax of various tools, like while. You also need to learn the syntax of just how to write a function.
The goal here is to build a random walk function, that takes nsteps. (Learn to use descriptive variable names. MATLAB does not charge more for the extra chars. But your code will improve, because you can now read it better, and THAT means you can debug your code more easily and faster.) So +1 or 1, with equal probability. The walk will start at 0. And you want the function to return the end point of that random walk? Perhaps better, to see that the code is working, is if we return the entire randomwalk. But I'll follow what you did.
First, a function starts with the word FUNCTION. Look at my code for randomwalk1. I'll follow a style like you used in this first attempt. I used a for loop, as well as an if. (Why do you use a for loop here. A while loop is appropriate when you don't know how many steps the loop will run. A for loop applies when you DO know that. Here, you know the walk has nsteps steps.)
randomwalk1(100)
ans = 4
randomwalk1(100)
ans = 14
randomwalk1(100)
ans = 2
I could probably do some statistics that will tell me the distribution of the final state of such a random walk, since the final state is essentially the sum of iid binomial random variables. But those walks seem reasonable.
Can we improve on that code? Of course. Taking baby steps, consider randomwalk2. randi can generate other results than simply 0 and 1. Use that capability!
randomwalk2(100)
ans = 4
I suppose I never really needed to generate step as a separate variable.
But this begs the question of why I even need a loop!
randomwalk3(100)
ans = -9
Next, we might consider if sometimes, we really want to generate multiple parallel random walks. And that suggests randomwalk4.
randomwalk4(100,10)
ans = 10×1
-8 -3 3 5 24 6 7 -14 6 10
Randomwalk1 function:
function finalstate = randomwalk1(nsteps)
% A random walk for Nsteps, equal prob in each direction. Returns the
% final state of the random walk.
finalstate = 0;
for n = 1:nsteps
step = randi([0 1]);
if step == 0
finalstate = finalstate - 1;
else
finalstate = finalstate + 1;
end
end
end
Randomwalk2 function:
function finalstate = randomwalk2(nsteps)
% A random walk for Nsteps, equal prob in each direction. Returns the
% final state of the random walk.
finalstate = 0;
for n = 1:nsteps
step = randi([-1 1]);
finalstate = finalstate + step; % don't need no steenkin if.
end
end
Randomwalk3 function:
function finalstate = randomwalk3(nsteps)
% A random walk for Nsteps, equal prob in each direction. Returns the
% final state of the random walk.
steps = randi([-1 1],1,100);
finalstate = sum(steps);
end
Randomwalk4 function:
function finalstate = randomwalk4(nsteps,nwalks)
% nwalks parallel random walks for Nsteps, equal prob in each direction. Returns the
% final state of each random walk.
if (nargin < 2) || isempty(nwalks)
nwalks = 1;
end
steps = randi([-1 1],nwalks,100);
finalstate = sum(steps,2);
end
Dilpuneet Grewal on 21 Oct 2021
i managed to compete the program, after a few trial and error runs. thanks for your help :)