%% Example of 2D Diffusion

%units are dimensionless

N=2;        % number of dimensions
D=1;        % Diffusion coefficient
deltat=1;   % timestep   

T=1000;      % Total number of jumps

% Using the 'randn' function to create a set of normally distributed 
% jumps with <x^2>= 2*D and <y^2>=2*D

Jumps=sqrt(2*D*deltat)*randn(T,N);

%check <x^2> and <y^2>
mean(Jumps.^2,1)
2*D*deltat

%check <r^2>
mean(sum(Jumps.^2,2),1)
2*N*D*deltat

% create a trajectory by adding up jumps.  This can be using cumsum
Traj=cumsum(Jumps,1);

%plot the results
figure
plot(Traj(:,1),Traj(:,2),'linewidth',2)
xlabel('X Position','fontsize',14)
ylabel('Y Position','fontsize',14)

