Advertisement
Guest User

Untitled

a guest
Jun 21st, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 3.51 KB | None | 0 0
  1. function [arena] = ConcentrationRandomMultipleOdourSourcesNEW(X,Y)
  2. arena = zeros(X, Y);
  3.  
  4. nos_min = 3; % minimum number of food sources
  5. nos_max = 10; % maximum number of food sources
  6. nos = randi([nos_min nos_max],1,1); % the actual number we are going to use
  7.  
  8. dist_min = 0; % minimum distance between food sourses where larvae can still sense odour
  9. dist_max = 27; % maximum distance between food sourses where larvae can still sense odour
  10.  
  11. sources_arr = zeros(nos, 2); % array of positions of food sourses
  12.  
  13. sources_arr(1, 1) = -650+(1300*rand(1,1)); % random X-coordinate of the first food source
  14. sources_arr(1, 2) = -1000+(2000*rand(1,1)); % random Y-coordinate of the first food source
  15.  
  16. dist = randi([dist_min dist_max],1,1); % random distance from the first food source to the second
  17. angle_mem = 0; % angle for the direction of the second food source
  18. rx = 0;
  19. ry = 0;
  20.  
  21. while check ~= 1
  22.     angle_mem = randi([0 359], 1, 1);
  23.     rx = sources_arr(1, 1) + dist * sind(angle_mem); % hypothetical X-coordinate of the 2nd food source
  24.     ry = sources_arr(1, 2) + dist * cosd(angle_mem); % hypothetical Y-coordinate of the 2nd food source
  25.     if (rx > 0) and (rx < 650) and (ry > 0) and (ry < 1000)
  26.         sources_arr(2, 1) = rx;
  27.         sources_arr(2, 2) = ry;
  28.         check = 1;
  29.     end
  30. end
  31.  
  32.  
  33. dist_mem = dist; % distance between 2 previous food sources
  34. angle = 0; % angle for the direction of the next food source
  35. dist_p = 0; % distance from the (i-2)nd food source to the current one
  36. dist_unimp = 0;
  37. dist_unimp2 = 0;
  38. angle_add = 0; % angle for completeting the angle of a triangle
  39.  
  40. % cycle for filling the array with random locations of food sources
  41. i = 3; % because I already defined the position of the first 2 food sources
  42.  
  43. for i = 3 : nos
  44.     while check ~= 1
  45.         dist = randi([dist_min dist_max],1,1); % random distance from the current food source to the next one
  46.         angle = randi([0 359],1,1);
  47.        
  48.         rx = sources_arr(i, 1) + dist * sind(angle); % hypothetical X-coordinate of the 2nd food source
  49.         ry = sources_arr(i, 2) + dist * cosd(angle); % hypothetical Y-coordinate of the 2nd food source
  50.        
  51.         if (rx > 0) and (rx < 650) and (ry > 0) and (ry < 1000)
  52.        
  53.             dist_unimp = dist_mem * sind(angle_mem);
  54.             dist_unimp2 = sqrt(dist_unimp^2 + dist_mem^2);
  55.             angle_add = 90 - asind(dist_unimp2 / dist_mem);
  56.        
  57.             dist_p = sqrt(dist_mem^2 + dist^2 - 2 * dist_mem * dist * cosd(angle+angle_add)); % dist from the (i-2)nd to i'th food source (pythagorian)
  58.        
  59.             if (dist_p <= dist_max) % check the distance between (i-2)nd and i'th sources
  60.                 sources_arr(i, 1) = rx;
  61.                 sources_arr(i, 2) = ry;
  62.                 check = 1;
  63.             end
  64.         end
  65.     end
  66. end
  67.  
  68. all_dist = zeros(nos, X, Y); % distances to all the food sources from each point in arena
  69. k = 1;
  70. p = 1;
  71. q = 1;
  72.  
  73. for k = 1 : nos
  74.     for p = 1 : X
  75.         for q = 1 : Y
  76.             all_dist(k, p, q) = sqrt((p - (X + sources_arr(k, 1))^2) + (q - (Y + sources_arr(k, 2)))^2);
  77.         end
  78.     end
  79. end
  80.  
  81. % cycle for filling the concentrations "arena" matrix
  82.  
  83. j = 1;
  84. f = 1;
  85. l = 1;
  86.  
  87. for j = 1 : X
  88.     for f = 1 : Y
  89.         for l = 1 : nos
  90.             arena(j, f) = arena(j, f) + exp(-(all_dist(l, j, f)^2)/40000))
  91.         end
  92.     end
  93. end
  94.  
  95. im = zeros(Y, X);
  96. v = 1;
  97. w = 1;
  98.  
  99. for v = 1 : X
  100.     for w = 1 : Y
  101.         im(w, v) = arena(v, w);
  102.     end
  103. end
  104.  
  105. imagesc([0 650], [0 1000], im);
  106. hold on;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement