SHARE
TWEET

Untitled

a guest Feb 20th, 2019 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. %% 4 sequential version
  2. clear all
  3. N = 500; % sample walks
  4. maxLengthWalk = 500;
  5. nbrFails = zeros(maxLengthWalk,1);
  6. weights = zeros(maxLengthWalk,N);
  7.  
  8. dirs = [eye(2); -eye(2)];
  9.  
  10. for k = 1:N
  11.  
  12.     Grid = zeros(2*maxLengthWalk+1, 2*maxLengthWalk+1);
  13.     X = zeros(maxLengthWalk+1, 2);
  14.    
  15.     %Initiate in center of grid
  16.     X(1,:) = [maxLengthWalk+1; maxLengthWalk+1];
  17.     Grid(X(1,1),X(1,2)) = 1;
  18.     w = 1; % weight
  19.    
  20.     for indexWalk = 1:maxLengthWalk
  21.                
  22.         % Take a step
  23.         free = 4;
  24.         freedirs = [];
  25.         for d = 1:4
  26.             neighbour = X(indexWalk,:)+dirs(d,:);
  27.             if(Grid(neighbour(1),neighbour(2)) == 1)
  28.                 free = free - 1;
  29.             else
  30.                 freedirs = [freedirs; dirs(d,:)];
  31.             end
  32.         end
  33.        
  34.         % If we don't self avoid
  35.         if(free == 0)
  36.             for kk = indexWalk:maxLengthWalk
  37.                 nbrFails(kk) = nbrFails(kk) +1;
  38.             end
  39.             break; % Should break indexWalk
  40.         end
  41.        
  42.         % We take a step
  43.         dir = freedirs(randi(free),:);
  44.         X(indexWalk + 1,:) = X(indexWalk,:) + dir;
  45.        
  46.         % Update weights
  47.         w = w * 1 / free;
  48.         weights(indexWalk, k) = w;
  49.        
  50.         % Update grid
  51.         Grid(X(indexWalk+1,1),X(indexWalk+1,2)) = 1;
  52. %         figure(3)
  53. %         imagesc(Grid)
  54. %         pause(0.01);
  55.     end
  56.    
  57. end
  58.  
  59. ratio= zeros(maxLengthWalk,1);
  60. ratiow= zeros(maxLengthWalk,1);
  61. for i = 1:maxLengthWalk
  62.     ratio(i) = ((N-nbrFails(i))/N )*4^i;
  63.     ratiow(i) = ((N-nbrFails(i))/N )/mean(weights(i,:));
  64.  
  65. end
  66. %
  67. % figure(1)
  68. % imagesc(Grid)
  69.  
  70.  
  71.  
  72. figure(2)
  73. semilogy(ratio)
  74. title('Random walks divided by nbr of walks')
  75. ylabel('ratio')
  76. xlabel('n'); % mult med 4^n  för bra skattning
  77.  
  78. figure(3)
  79. semilogy(ratiow)
  80. title('Probability of walk divided by nbr of walks')
  81. ylabel('ratio')
  82. xlabel('n'); % mult med 4^n  för bra skattning
  83.  
  84. % Right now the X values are staring in the center of the grid. Need
  85. % to push them back to 0 in order to be correct
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