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

A random walk in two dimensions: Write a program, rw2d.m, to describe a walker m

ID: 3834687 • Letter: A

Question

A random walk in two dimensions: Write a program, rw2d.m, to describe a walker making a random walk with unit steps in the x-y plane. The walker starts at the origin. Represent the walkers N position using MATLAB arrays x and y. A good starting value for total number of random walks would be 1000. The walk starts with x(1) = 0, y(1) = 0. Use a for loop to calculate the walker's moves. At each step the walker uses a random number to decide whether to take step: bullet North (y increases by 1; x stays the same). bullet South (y decrease by 1; x stays the same). bullet East (x increases by 1; y stays the same). bullet West (x decreases by 1; y stays the same). The walker always takes a step of length 1. Please plot the trajectory of the random walk.

Explanation / Answer


x = 0;
y = 0;
x_ = [];
y_ = [];

for i = 1:1000
    r = mod(round(rand()*32003), 4); %please note that 32003 is a large random number suchthat mod (32003,4) = 3
    switch r
        case 0 %north
            y = y + 1;
        case 1 %south
            y = y - 1;
        case 2 %East
            x = x + 1;
        case 3 %West
            x = x - 1;
    end
    x_ = [x_ x]; % add to array to trace
    y_ = [y_ y];
end
figure
plot (x_, y_) %trace the path

I tried my best to keep the code simple and straightforward. If you have any query please feel free to comment below. I shall be glad to help you.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote