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

please solve this question by using Matlab Write a function which takes as input

ID: 3777125 • Letter: P

Question

please solve this question by using Matlab

Write a function which takes as input an integer number of steps and performs a random walk of that many steps and returns the sequence of distances from the initial starting position. For example: A random walk (10) Note that each element of the array is exactly 1 step away from the previous element. Staying in place is not an option. Neither is taking more than 1 step at a time. You can make a quick and dirty visualization of this by using the plot function. plot (A) If you were to count how many times each distance occurred in the array, you would know how many times you occupied each spot. You can find the unique elements of an array using the unique function unique (A) anS You can find how many times a value occurs in an array using the length and find functions. length (find (A 3)) ans

Explanation / Answer

Solution:

% function for random walk
function [Aval] = random_walk(n);
  
   %two independent random variables x(n) and y(n)
   Aval = [0; cumsum(2*(rand(n,1)>0.5)-1)];
   Bval=[0; cumsum(2*(rand(n,1)>0.5)-1)];
  
   % for axis limit
   YA = max(abs(Bval))+1;
   XA=max(abs(Aval))+1;
   %plotting
   plot(Aval,Bval);       
   axis([-XA XA -YA YA]);

   %Labeling the plot
   title(['2-D Random walk for ' int2str(n) ' steps'] );
   xlabel('x(n)');
   ylabel('y(n)');

>>A=random_walk(10)