Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

matlab!!!!!! 1. A random walk is represented by a vector beginning with 0 and wh

ID: 3004060 • Letter: M

Question

matlab!!!!!!

1. A random walk is represented by a vector beginning with 0 and where each subsequent entry in the vector is attained from the previous entry by adding a random choice of either 1 or -­1. For example, a random walk might begin [0 -­1 0 1 2 3 2 3 2 3 4 3 2 3 4 5]. Write a function rand_walk1 that takes as input a positive integer n and as output returns a length n random walk. Our example is a length 16 random walk, because it contains 16 total entries.

2. Write a function rand_walk2 that takes as input a positive integer n and as output returns a random walk that stops the first time it reaches an entry of n or -­n. For example, the above random walk example stopped the first time it reached 5 or -­5.

Explanation / Answer

function v = r_walk1(n)
   v = zeros(1,n);
   for i = 2:n
       rnd = rand
       if (rnd == 0)
           v(1,i) = v(1,i-1) - 1;
       else
           v(1,i) = v(1,i-1) + 1;
       end
   end

end