Guest User

Untitled

a guest
Jan 21st, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.28 KB | None | 0 0
  1. %----------------------
  2. % Solution to Question 1a
  3. %----------------------
  4. % Student 1: SID = 311178480
  5. %----------------------
  6. % One paragraph description of how this program works:
  7. %
  8. %
  9. %
  10. %----------------------
  11.  
  12. GRID_SIZE  = 100;
  13. N_STEPS    = 1000;
  14. START_LOC  = 0.5 * [GRID_SIZE GRID_SIZE];
  15.  
  16. grid = zeros(GRID_SIZE);
  17.  
  18. % r_dir = 0 corresponds to movement in the first dimension, r_dir = 1
  19. % corrresponds to movement in the second dimension
  20. r_dir  = round(rand(N_STEPS, 1));
  21. % r_sign = -1 corresponds to negative movement(left/up), r_sign = 1 correspods
  22. % to positive movement(right/down)
  23. r_sign = 2 * round(rand(N_STEPS, 1)) - 1;
  24. % Combine the directions and signs to find all the movements
  25. v = [r_sign r_sign] .* [r_dir ~r_dir];
  26. % Sum up the movements up to find the locations of the random walk over time
  27. loc = cumsum([START_LOC; v]);
  28. % Open a figure window to output the motion of the walk to screen
  29. fig = figure('Name', 'Single Random Walk', 'NumberTitle', 'off');
  30. for i = 1:N_STEPS
  31.     % At each step, add the locations up to that timestep to the grid
  32.     if all(loc(i,:) > 0 & loc(i,:) <= GRID_SIZE*GRID_SIZE)
  33.         grid(loc(i,1),loc(i,2)) = 1;
  34.     end
  35.     % Show the grid
  36.     imagesc(grid);
  37.     pause(0);
  38. end
  39. % Print the figure out to pdf
  40. print(fig, '-dpdf', 'single_random_walk.pdf');
Add Comment
Please, Sign In to add comment