daily pastebin goal
12%
SHARE
TWEET

Untitled

a guest Oct 17th, 2018 192 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. % EA 1, Homework program assignment 3
  2. %
  3. % Name: Dhileepan, Vishal
  4. % Section: 21
  5. % Date: 10/18/2018
  6.  
  7. %Task 1
  8. %Prompt the user for an input
  9. steps = input('How many steps would you like to take? ');
  10. %Creates matrix of possible random movements
  11. possible_points = randi([1,4],steps,1);
  12. figure; %creates a new figure for a plot
  13. h= animatedline(0,0); %initializes an animation of the random walk at the origin
  14. x=0;
  15. y=0;
  16. %Iterates the loop for all steps
  17. for i=1:steps
  18.     if possible_points(i) == 1
  19.         x = x + 1;
  20.     elseif possible_points(i) == 2
  21.         x = x - 1;
  22.     elseif possible_points(i) == 3
  23.         y = y + 1;
  24.     elseif possible_points(i) == 4
  25.         y = y - 1;
  26.     end
  27.    
  28.     addpoints(h,x,y); %adds a line to point (x,y)
  29.     drawnow %displays the current plot where x and y are the coordinates of the new position
  30.    
  31. end
  32.  
  33. %Task 2
  34. %Prompt the user for inputs
  35. steps = input('How many steps would you like to take? ');
  36. trials = input('How many trials would you like? ');
  37. trial = 1;
  38. trial_vector = zeros(1,trials);
  39. x
  40. y
  41. figure;
  42.  
  43. %Iterates the loop for the number of trials
  44. while trial<=trials
  45.     possible_points = randi([1,4],steps,1);
  46.     x=0;
  47.     y=0;
  48.     %Iterates the loop for all steps
  49.     for i=1:steps
  50.         if possible_points(i) == 1
  51.             x = x + 1;
  52.         elseif possible_points(i) == 2
  53.             x = x - 1;
  54.         elseif possible_points(i) == 3
  55.             y = y + 1;
  56.         elseif possible_points(i) == 4
  57.             y = y - 1;
  58.         end
  59.         final_distance = sqrt(x^2+y^2);  
  60.     end
  61.     trial_vector(trial) = final_distance;
  62.     trial = trial+1;
  63. end
  64. trial_vector
  65. average_distance = sum(trial_vector)/trials
  66. histogram(trial_vector)
  67. xlabel('Distance')
  68. ylabel('Number of instances')
  69.  
  70. %Task 3
  71. %Prompt the user for inputs
  72. steps = input('How many steps would you like to take? ');
  73. trials = input('How many trials would you like? ');
  74. distance_threshold = input('Enter a distance threshold array: ');
  75. steps_array = [1:length(distance_threshold)];
  76. trial=1;
  77. figure;
  78.  
  79. %Iterates the loop for all values of the distance threshold
  80. for j = 1:length(distance_threshold)
  81.     %Iterates the loop for all trials
  82.     while trial <= trials
  83.         if mod(trial, 400) == 0
  84.             fprintf("distance_threshold= %6.2f, trial= %6.2f\n", distance_threshold(j), trial);
  85.         end
  86.         step = 1;
  87.         x=0;
  88.         y=0;
  89.         %Iterates the loop for all steps
  90.         while step <= steps
  91.             possible_points = randi([1,4],steps,1);
  92.             distance = sqrt(x^2 + y^2);
  93.             if distance >= distance_threshold(j)
  94.                 steps_array(j) = steps_array(j) + step;
  95.                 break
  96.             end
  97.             if possible_points(i) == 1
  98.                 x = x + 1;
  99.             elseif possible_points(i) == 2
  100.                 x = x - 1;
  101.             elseif possible_points(i) == 3
  102.                 y = y + 1;
  103.             elseif possible_points(i) == 4
  104.                 y = y - 1;
  105.             step = step+1;
  106.             end
  107.             if step > steps
  108.                 fprintf("Maximum number of steps exceeded!\n");
  109.                 break
  110.             end
  111.         end
  112.         trial = trial+1;        
  113.     end
  114.     trial = 1;
  115. end
  116.  
  117. %Calculating and printing average number of steps
  118. fprintf("(distance, average number of steps)\n");
  119. hold on;
  120. for j = 1:length(distance_threshold)
  121.     fprintf("(%6.2f, %6.2f)\n", distance_threshold(j), steps_array(j)/trials);
  122. end
  123. plot(distance_threshold, steps_array/steps);
  124. xlabel("Distance threshold");
  125. ylabel("Average step count");
  126.  
  127. %%Outputs
  128. %Task 1:
  129. %The final position is (0,-10)
  130. %The distance from the origin is 10
  131.  
  132. %Task 2
  133. %The average distance was 39.5620
  134.  
  135. %Task 3
  136. %distance_threshold=   5.00, trial= 400.00
  137. % distance_threshold=   5.00, trial= 800.00
  138. % distance_threshold=   5.00, trial= 1200.00
  139. % distance_threshold=   5.00, trial= 1600.00
  140. % distance_threshold=   5.00, trial= 2000.00
  141. % distance_threshold=  10.00, trial= 400.00
  142. % distance_threshold=  10.00, trial= 800.00
  143. % distance_threshold=  10.00, trial= 1200.00
  144. % distance_threshold=  10.00, trial= 1600.00
  145. % distance_threshold=  10.00, trial= 2000.00
  146. % distance_threshold=  15.00, trial= 400.00
  147. % distance_threshold=  15.00, trial= 800.00
  148. % distance_threshold=  15.00, trial= 1200.00
  149. % distance_threshold=  15.00, trial= 1600.00
  150. % distance_threshold=  15.00, trial= 2000.00
  151. % distance_threshold=  20.00, trial= 400.00
  152. % distance_threshold=  20.00, trial= 800.00
  153. % distance_threshold=  20.00, trial= 1200.00
  154. % distance_threshold=  20.00, trial= 1600.00
  155. % distance_threshold=  20.00, trial= 2000.00
  156. % distance_threshold=  25.00, trial= 400.00
  157. % distance_threshold=  25.00, trial= 800.00
  158. % distance_threshold=  25.00, trial= 1200.00
  159. % distance_threshold=  25.00, trial= 1600.00
  160. % distance_threshold=  25.00, trial= 2000.00
  161. % distance_threshold=  30.00, trial= 400.00
  162. % distance_threshold=  30.00, trial= 800.00
  163. % distance_threshold=  30.00, trial= 1200.00
  164. % distance_threshold=  30.00, trial= 1600.00
  165. % distance_threshold=  30.00, trial= 2000.00
  166. % distance_threshold=  35.00, trial= 400.00
  167. % distance_threshold=  35.00, trial= 800.00
  168. % distance_threshold=  35.00, trial= 1200.00
  169. % distance_threshold=  35.00, trial= 1600.00
  170. % distance_threshold=  35.00, trial= 2000.00
  171. % distance_threshold=  40.00, trial= 400.00
  172. % distance_threshold=  40.00, trial= 800.00
  173. % distance_threshold=  40.00, trial= 1200.00
  174. % distance_threshold=  40.00, trial= 1600.00
  175. % distance_threshold=  40.00, trial= 2000.00
  176. % distance_threshold=  45.00, trial= 400.00
  177. % distance_threshold=  45.00, trial= 800.00
  178. % distance_threshold=  45.00, trial= 1200.00
  179. % distance_threshold=  45.00, trial= 1600.00
  180. % distance_threshold=  45.00, trial= 2000.00
  181. % distance_threshold=  50.00, trial= 400.00
  182. % distance_threshold=  50.00, trial= 800.00
  183. % distance_threshold=  50.00, trial= 1200.00
  184. % distance_threshold=  50.00, trial= 1600.00
  185. % distance_threshold=  50.00, trial= 2000.00
  186. % (distance, average number of steps)
  187. % (  5.00,   7.41)
  188. % ( 10.00,  26.77)
  189. % ( 15.00,  60.50)
  190. % ( 20.00, 101.89)
  191. % ( 25.00, 158.06)
  192. % ( 30.00, 233.35)
  193. % ( 35.00, 315.02)
  194. % ( 40.00, 413.74)
  195. % ( 45.00, 507.54)
  196. % ( 50.00, 638.38)
  197. %The specific function that approximates this is line 124, where the steps
  198. %array divides the number of trials that is chosen by the user
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top