Guest User

Untitled

a guest
Dec 11th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.67 KB | None | 0 0
  1. % MS&E 319 Project 2
  2. % By Tyler Zellmer
  3.  
  4. % Clear the workspace
  5. clc;
  6. clear;
  7. close all;
  8. tic;
  9.  
  10. % What fraction of B particles should our space have?
  11. b_fraction = .01;
  12.  
  13. % Create a 3-D space of particles of dimension s x s x s
  14. s = 10;
  15. numB = 0;
  16.  
  17. % In x-direction
  18. for l=1:s
  19.     % In y-direction
  20.     for k=1:s
  21.         % In z-direction
  22.         for j=1:s
  23.             % Generate a random number between 0 and 1
  24.             random_number = rand(1,1);
  25.            
  26.             % If the random number is greater than our b_fraction, then our
  27.             % current point is an A particle. Otherwise, it's a B particle.
  28.             % This ensures that, overall, approximately b_fraction of our
  29.             % space will be B particles
  30.             if random_number > b_fraction
  31.                 space(j,k,l) = 0;
  32.             else
  33.                 space(j,k,l) = 1;
  34.                 numB = numB + 1;
  35.                 % Let's also record the location of each B particle
  36.                 loc(:,numB) = [j k l]';
  37.             end
  38.         end
  39.     end
  40. end
  41.  
  42. % Now, recurse through each B point and find nearest neighboring B particle
  43. for a = 1:length(loc)
  44.     difference(a) = 10*s;
  45.     % Recurse through all B-particles in the space and measure their
  46.     % from the current B-particle
  47.     for b = 1:length(loc)
  48.         % Find this particle's distance from the particle of interest. If
  49.         % it is closer than the previous closest, update difference to
  50.         % reflect that.
  51.         temp_length = norm(loc(:,b) - loc(:,a));
  52.         if (temp_length < difference(a) && temp_length ~= 0)
  53.             difference(a) = temp_length;
  54.         end
  55.     end
  56. end
  57.  
  58. time = toc;
  59. % Display useful information
  60. disp('Number of B-particles: ');
  61. disp(numB);
  62. disp('Max difference (units): ');
  63. disp(max(difference));
  64. disp('Least difference (units): ');
  65. disp(min(difference));
  66. disp('Mean difference (units): ');
  67. disp(mean(difference))
  68. disp('Execution time (s): ');
  69. disp(time);
  70.  
  71. % Plot location of B-particles
  72. hold on;
  73. for i=1:length(loc)
  74.     plot3(loc(1,i),loc(2,i),loc(3,i), 'o', 'linewidth', 2);
  75.     grid on;
  76. end
  77.  
  78.    
  79. % In the x-direction
  80. %     for l=1:s
  81. %         % In y-direction
  82. %         for k=1:s
  83. %             % In z-direction
  84. %             for j=1:s
  85. %                 % If the point at this location is a  B particle, measure
  86. %                 % the distance from
  87. %                 if space(j,k,l) == 1
  88. %                     temp_length = norm([j k l] - loc(a));
  89. %                     if temp_length < difference(a) && temp_length ~= 0
  90. %                         difference(a) = temp_length;
  91. %                     end
  92. %                 end
  93. %             end
  94. %         end
  95. %     end
Add Comment
Please, Sign In to add comment