Figure 5.4:

The mean square displacement versus time; D=2, V=0, n=500.

Code for Figure 5.4

Text of the GNU GPL.

main.m


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
%
% 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