
%% Example of 2D Diffusion

%units are dimensionless

N=2;        % number of dimensions
D=.1;        % Diffusion coefficient in micron^2/s
deltat=.01; % timestep   
T=10;       %Total time in seconds
Njumps=round(T/deltat);      % 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(Njumps,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 (microns)','fontsize',14)
ylabel('Y Position (microns)','fontsize',14)
title('Homework #1 Solution','fontsize',16)








