% Created by Max Riekeles 2020, 
%adapted from http://experimentationlab.berkeley.edu/node/83

clear all;
    
particleCount = 200;
d = ; %Type here the diameter in meters
    eta = ; ; %Type here the viscosity of solvent
        T = 313.15; %Type here the Temperature in Kelvin
             

  kB = 1.38e-23; % Boltzman constant
  
  D = kB*T/(3*pi*eta*d)

    
  N = 56
tau = .178
  k=sqrt(2*D*tau);
time = 0:tau:(N)*tau;

particle = {};

for i = 1:particleCount
    particle{i}=struct();
    particle{i}.dx=k*randn(1,N);
    particle{i}.x=cumsum(particle{i}.dx);
    particle{i}.dy=k*randn(1,N);
    particle{i}.y=cumsum(particle{i}.dy);
    particle{i}.drsquared = particle{i}.dx.^2+particle{i}.dy.^2;
    particle{i}.D=mean(particle{i}.drsquared)/(2*d*tau*sqrt(N));
end;


   A =cell2mat(particle)

for g = 1 : particleCount
   %Z = zeros(g, 1);
    ZX(1:N,g) = particle{g}.x;
    ZY(1:N,g) = particle{g}.y;
    %Speed in micrometers/sec
    Speed(1:N,g)= 1e6*(sqrt((particle{g}.dx).^2+(particle{g}.dy).^2))/tau; 
    
end

for ss = 1: particleCount
SmallSpeed (ss) = length(find(Speed(1:end, ss)<1.4))/56;
end
% Matrix= [ZX; ZY]*1e6;

for l = 1:particleCount
for ii = 1:54

     x12(ii, l)= ZX(ii+1, l)-ZX(ii, l);
    y12(ii, l)= ZY(ii+1, l)-ZY(ii, l);
      x23(ii, l)= ZX(ii+1+1, l)-ZX(ii+1, l);
      y23(ii, l)= ZY(ii+1+1, l)-ZY(ii+1, l);

end;
end;
  MM = atan2(x12.*y23-x23.*y12, x12.*x23+y12.*y23)
  winkel = rad2deg(MM);

  
  
  
sqrt((particle{g}.dx).^2+(particle{g}.dy).^2)
  
  for g = 1 : particleCount
  Abstand(g)= 1e6*sqrt((particle{g}.x(56)-particle{g}.x(1)).^2+(particle{g}.y(56)-particle{g}.y(1))^2)
  
  end
  
  for w = 1:particleCount
  meanwinkel(w)=mean(winkel(1:end,w));
  stwinkel(w)=std(winkel(1:end,w));
   nna1(w)=length(find(winkel(1:end,w)<0))/56
    npa1(w)=length(find(winkel(1:end,w)>0))/56
 nnn1(w)=length(find(abs(winkel(1:end,w))< 20))/56
    end;
  for s = 1:particleCount
      meanspeed(s)=mean(Speed(1:end, s));
      sdtpeed(s)=std(Speed(1:end, s));

  end;
  
  %Aggregated data
  AG = [  
    meanspeed' sdtpeed' nna1' npa1' nnn1' meanwinkel' stwinkel' Abstand' SmallSpeed']; 
  

