%
% Simulate a random walk in 2 dimensions.
% Compute and plot the mean square displacement,
% and its derivative with respect to the diffusivity.
% Also plot a typical trajectory of one particle.
%
% jbr;6/4/04
%
tfinal = 1000;
time = [0:tfinal]';
ntimes = length(time);
npart = 500;
ndim = 2;
D = 2;
v = 0; % drift term is v_x=v, v_y=v;
clear x S
x = zeros(ndim, npart, ntimes);
S = zeros(ndim, npart, ntimes);
rsq = zeros(ntimes, npart);
drsqD = zeros(ntimes, npart);
%reproducible random numbers
randn("seed",0);
for i = 1:ntimes-1
% choose a normally distributed 2-vector for the step
ranstep = sqrt(2*D)*randn(ndim,npart);
location = x(:,:,i) + v + ranstep;
x(:,:,i+1) = location;
partial = S(:,:,i) + 1/(2*D)*ranstep;
S(:,:,i+1) = partial;
rsq(i+1,:) = diag(location'*location)';
drsqD(i+1,:) = 2*diag(location'*partial)';
end
mrsq = mean(rsq, 2);
dmrsqD = mean(drsqD, 2);
% plot the mean of the r^2 displacement
figure()
plot(time, [mrsq]);
% plot the derivative with respect to diffusivity of the mean of the r^2 displacement
figure()
plot(time,[dmrsqD]);
% plot a typical particle trajectory
figure()
particle = 1;
traj = reshape(x(:,particle,:), ndim, ntimes)';
plot(traj(:,1),traj(:,2),'-o');
data1 = [time, mrsq, dmrsqD];
data2 = [traj];
% save brownian2d_1.dat data1;
% save brownian2d_2.dat data2;
save brownian2d.dat data1 data2