Answer To: Random Walk assignment MATH 210-01, Fall 2021 Ryan Pellico, Department of Mathematics, Trinity...
Dr. Purnima Kumari answered on Oct 05 2021
Random Walk assignment
MATH 210-01, Fall 2021
Ryan Pellico, Department of Mathematics, Trinity College
1. (30 points) Create a new function three_d_walk(num_steps) that performs a 3-dimensional
random walk of length num_steps, by starting at the origin (x; y; z) = (0; 0; 0) and rolling a fair 6-sided die to determine the direction of each step (up, down, left, right, front, back). The function should return the column vectors [x, y, z] which record the coordinates of each step of the walk.
Hint: use the two_d_walk_UDLR(num_steps) function as a template.
Solution Code:
Function: three_d_walk
function [x,y,z] = three_d_walk(num_steps)
% This function performs a three-dimensional random walk in the xyz-plane of
% length num_steps (a positive integer),by starting at the origin (x; y; z) = (0; 0; 0) and rolling a fair 6-
% sided die to determine the direction of each step (up, down, left, right, front, back).
% The column vectors x, y and z will record the sequences of coordinates the
% walker encounters on the walk.
x = zeros(num_steps+1,1);
y = zeros(num_steps+1,1);
z = zeros(num_steps+1,1);
% (x0,y0,z0) are the coordinates of the starting point for the random walker
% in the plane
x0 = 0;
y0 = 0;
z0 = 0;
x(1) = x0;
y(1) = y0;
z(1) = z0;
for n = 1:num_steps
roll = randi(6);
if roll == 1 % if the roll is a 1, take a step UP
x(n+1) = x(n);
y(n+1) = y(n) + 1;
z(n+1) = z(n);
elseif roll == 2 % if the roll is a 2, take a step DOWN
x(n+1) = x(n);
y(n+1) = y(n) - 1;
z(n+1) = z(n);
elseif roll == 3 % if the roll is a 3, take a step LEFT
x(n+1) = x(n) - 1;
y(n+1) = y(n);
z(n+1) = z(n);
elseif roll == 4 % if the roll is a 4, take a step RIGHT
x(n+1) = x(n) + 1;
y(n+1) = y(n);
z(n+1) = z(n);
elseif roll == 5 % if the roll is a 5, take a step FRONT
x(n+1) = x(n);
y(n+1) = y(n);
z(n+1) = z(n)+1;
elseif roll == 6 % if the roll is a 6, take a step BACK
x(n+1) = x(n);
y(n+1) = y(n);
z(n+1) = z(n)-1;
end
end
Function:...