Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %----------------------
- % Solution to Question 1a
- %----------------------
- % Student 1: SID = 311178480
- %----------------------
- % One paragraph description of how this program works:
- %
- %
- %
- %----------------------
- GRID_SIZE = 100;
- N_STEPS = 1000;
- START_LOC = 0.5 * [GRID_SIZE GRID_SIZE];
- grid = zeros(GRID_SIZE);
- % r_dir = 0 corresponds to movement in the first dimension, r_dir = 1
- % corrresponds to movement in the second dimension
- r_dir = round(rand(N_STEPS, 1));
- % r_sign = -1 corresponds to negative movement(left/up), r_sign = 1 correspods
- % to positive movement(right/down)
- r_sign = 2 * round(rand(N_STEPS, 1)) - 1;
- % Combine the directions and signs to find all the movements
- v = [r_sign r_sign] .* [r_dir ~r_dir];
- % Sum up the movements up to find the locations of the random walk over time
- loc = cumsum([START_LOC; v]);
- % Open a figure window to output the motion of the walk to screen
- fig = figure('Name', 'Single Random Walk', 'NumberTitle', 'off');
- for i = 1:N_STEPS
- % At each step, add the locations up to that timestep to the grid
- if all(loc(i,:) > 0 & loc(i,:) <= GRID_SIZE*GRID_SIZE)
- grid(loc(i,1),loc(i,2)) = 1;
- end
- % Show the grid
- imagesc(grid);
- pause(0);
- end
- % Print the figure out to pdf
- print(fig, '-dpdf', 'single_random_walk.pdf');
Add Comment
Please, Sign In to add comment