29- In a one-dimensional random walk the position x of a walker is computed by x
ID: 3540504 • Letter: 2
Question
29- In a one-dimensional random walk the position x of a walker is computed
by
xj = xj + s
where s is a random number. Write a Matlab program that calculates the number of
steps required for the walker to reach a boundary x =+-B(x= plus minus B)
Use MATLAB's built-in function randn(1,1) to calculate s.Run the program 100 times(by using a loop) and
calculate the average number of steps when B = 10.
Explanation / Answer
% assumes x can be fractional as well
x=0;
B=10;
loop=100;
avgSteps=0;
for n=1:loop
xj=x;
steps=0;
while( abs(x-xj) < B)
s= randn(1,1);
xj = xj + s;
steps=steps+1;
end
avgSteps = avgSteps + steps;
end
avgSteps = avgSteps/loop;
fprintf('Average number of steps when B = %d, is %f ',B,avgSteps);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.