%Note: this could be written more compactly using 'cumsum'
D=1;
deltat=1;
N=1;
Num=1000; %number of particles. 
T=100; %number of time steps. 
%% Random walk

%<x^2> = 2*N*D*delta
%so jump=+/- sqrt(2*N*D*deltat) 

clear pos_RW
for nn=1:Num
    x=0;
    for tt=1:T
        x=x+(-1)^(rand>.5)*sqrt(2*N*D*deltat);    
    end
    pos_RW(nn)=x; %final position
end
std(pos_RW)
%% Normal Distribution

%<x^2> = 2*N*D*delta
%so jump=sqrt(2*N*D*deltat)*randn; 

clear pos_Norm
for nn=1:Num
    x=0;
    for tt=1:T
        x=x+sqrt(2*N*D*deltat)*randn;    
    end
    pos_Norm(nn)=x;%final position
end
std(pos_Norm)
%% Uniform Distribution

%<x^2> = 2*N*D*delta
%L=(3*2*N*D*deltat)^(1/2)

L=(3*2*N*D*deltat)^(1/2);
clear pos_Unif
for nn=1:Num
    x=0;
    for tt=1:T
        x=x+L*(2*(rand-.5));    
    end
    pos_Unif(nn)=x;%final position
end
std(pos_Unif)

%% Make plots
sigma=sqrt(2*N*D*deltat*T);
dx=sigma/5;
BinCenters=(-4*sigma:dx:4*sigma);
PDF=1/sqrt(2*pi*sigma^2)*exp(-BinCenters.^2/(2*sigma^2));

%Random walk------------------------------------------------------------
[counts, BCs]=hist(pos_RW,BinCenters); %note BCs should be the same as BinCenters
A=1/sum(counts*dx)

figure
bar(BinCenters,A*counts*dx)
%now plot the PDF over the histogram:
hold on %keep old plot
plot(BinCenters,PDF*dx,'r','linewidth',2) 

%Note since we have finite sized bins, and a continuous PDF, to get the
%probability of a count in the bin, multiply by 'dx'.  This works if 'dx'
%is small enough, otherwise an intergral of the PDF over the bin width
%would be required. 

%now add some figure labels and legends:

xlabel('Final X Position','fontsize',14)
ylabel('Counts','fontsize',14)
legend('Normlized counts','PDF')
title('Random Walk')

%Normal------------------------------------------------------------
[counts, BCs]=hist(pos_Norm,BinCenters); %note BCs should be the same as BinCenters
A=1/sum(counts*dx)

figure
bar(BinCenters,A*counts*dx)
%now plot the PDF over the histogram:
hold on %keep old plot
plot(BinCenters,PDF*dx,'r','linewidth',2) 

%Note since we have finite sized bins, and a continuous PDF, to get the
%probability of a count in the bin, multiply by 'dx'.  This works if 'dx'
%is small enough, otherwise an intergral of the PDF over the bin width
%would be required. 

%now add some figure labels and legends:

xlabel('Final X Position','fontsize',14)
ylabel('Counts','fontsize',14)
legend('Normlized counts','PDF')
title('Normal Distribution')

%Uniform------------------------------------------------------------
[counts, BCs]=hist(pos_Unif,BinCenters); %note BCs should be the same as BinCenters
A=1/sum(counts*dx)

figure
bar(BinCenters,A*counts*dx)
%now plot the PDF over the histogram:
hold on %keep old plot
plot(BinCenters,PDF*dx,'r','linewidth',2) 

%Note since we have finite sized bins, and a continuous PDF, to get the
%probability of a count in the bin, multiply by 'dx'.  This works if 'dx'
%is small enough, otherwise an intergral of the PDF over the bin width
%would be required. 

%now add some figure labels and legends:

xlabel('Final X Position','fontsize',14)
ylabel('Counts','fontsize',14)
legend('Normlized counts','PDF')
title('Uniform Distribution')








