Please explain your answer and use MatLab for any code Your starting position is
ID: 2292667 • Letter: P
Question
Please explain your answer and use MatLab for any code
Your starting position is (0, 0) on x-y coordinate. If you take continuous 50 steps, each step can only move "1" unit (along x or y direction only), but the direction has 4: up, down, left, and right. Each location is the starting location of next step. (see attached figure) Please use random process to select each direction and record it. Plot the track from step 1 to step 50 and plot it. step 3, moves from step 2, moves from Step 1, moves from (0,0) to (0,1) (0,0)Explanation / Answer
%%
posn=[0 0]; % initiating at origin
P=posn;
figure;grid; % creating a new figure to plot the lines
for i=1:50; % total number of steps
n=randi(4,1); % creating a random number from 1-4 to select the direction
poso=posn;
switch n % to give the direction
case 1 % for 'UP' direction
posn=poso+[0 1];
case 2 % for 'DOWN' direction
posn=poso+[0 -1];
case 3 % for 'LEFT' direction
posn=poso+[-1 0];
case 4 % for 'RIGHT' direction
posn=poso+[1 0];
end
% we can store the new point
P=[P;posn];
% Now we can plot a line between old point and new pont
line([poso(1,1) posn(1,1)],[poso(1,2) posn(1,2)]);
pause(1)
end
%% we can plot each line individually or at once by using plot command
figure;
plot(P(:,1),P(:,2));