Random walk-MatLab Code

Saturday, October 10, 2009

One can efficiently generate and analyze random
walks on the computer.

(a)
Write a routine to generate an N-step random walk in d dimensions, with each step uniformly distributed in the range (−1/2, 1/2) in each dimension. (Generate the steps first as an N × d array, then do a cumulative sum.) Plot xt versus t for a few 10 000-step random walks. Plot x versus y for a few two-dimensional random walks, with N = 10, 1000, and 100 000.(Try to keep the aspect ratio of the XY plot equal to one.) Does multiplying the number of steps by one hundred roughly increase the net distance by ten?


(b)
Write a routine to calculate the endpoints of W random walks with N steps each in d dimensions. Do a scatter plot of the endpoints of 10 000 random walks with N = 1 and 10, superimposed on the same plot. Notice that the longer random walks are distributed in a circularly symmetric pattern, even though the single step random walk N =1 has a square probability distribution


(c)
Calculate the RMS step-size a for onedimensional steps uniformly distributed in (−1/2, 1/2). Write a routine that plots a histogram of the endpoints of W one-dimensional random walks with N steps and 50 bins, along with the prediction of eqn 2.35 for x in (−3σ, 3σ). Do a histogram with W = 10 000 and N = 1, 2, 3, and 5. How quickly does the Gaussian distribution become a good approximation to the random walk?



MATLAB CODE



N=3; % no of trials


k=100; %no of walks

t3=get(handles.dim,'string');
d=2; %dimension
s=0.5; %step size




z=(2*s).*(rand(d,N))-s;


vec=[zeros(1,d); cumsum(z')];

%x=[zeros(1,d); cumsum(z(2,N))]
colorstr=['b' 'r' 'g' 'y'];
for i=1:k
z=(2*s).*(rand(d,N))-s;
if N==1
for j=1:d
D(i,j)=cumsum(z(j,N));
%G(j)=cumsum(z(j,N));
K(i)=i;
end


else

vec=[zeros(1,d); cumsum(z')];


X(i)=vec(N+1,1);
if d>1
Y(i)=vec(N+1,2);
end
K(i)=i;
%plot(x(N+1,1),x(N+1,2),'.')

end


end

if N==1


% L=[0 ,0;1 ,D(k,1) ]
figure
% plot(L(:,1),L(:,2),'.-r')

plot(cumsum(z(1,:)'),'.-')
title(['Xt versus t, ','( Number of Steps- ',int2str(N),')'])
xlabel('t')
ylabel('Xt-X position')


figure

if d>1
plot(D(:,1),D(:,2),'.b')
else
plot(D(:,1)','.b')
end
axis([-2*s 2*s -2*s 2*s])
title(['X versus Y, ','( Number of Steps- ',int2str(N),')'])
xlabel('X position')
ylabel('Y position')


else

figure

%M=[0 ,0;K' ,X' ] ;
%plot(M(:,1),M(:,2),'.-b')
plot(cumsum(z(1,:)'),'.-')
title(['Xt versus t, ','( Number of Steps- ',int2str(N),')'])
xlabel('t-Number of walks')
ylabel('Xt-X position')
grid on

figure
plot(cumsum(z(1,:)'),cumsum(z(2,:)'),'.-b')
title(['X versus Y, ','( Number of Steps- ',int2str(N),')'])
xlabel('X position')
ylabel('Y position')
grid on;
end


% PART(B)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
N=10;
k=10000;
for i=1:k
z=(2*s).*(rand(d,N))-s;

vec=[zeros(1,d); cumsum(z')];

X(i)=vec(N+1,1);
if d>1
Y(i)=vec(N+1,2);
end
K(i)=i;
%plot(x(N+1,1),x(N+1,2),'.')

end



figure
if d>1
plot(Y,X,'.g')
else
plot(X,'.g')
end
title(['X versus Y, ','( Number of Steps- ',int2str(N),')'])
xlabel('X position')
ylabel('Y position')
hold on
grid on

N=1;
k=10000;
for i=1:k
z=(2*s).*(rand(d,N))-s;

for j=1:d
D(i,j)=cumsum(z(j,N));
%G(j)=cumsum(z(j,N));
K(i)=i;
end


end



plot(D(:,1),D(:,2),'.b')
%%axis([-2*s 2*s -2*s 2*s])
title(['X versus Y, ','( Number of Steps- ',int2str(N),')'])
xlabel('X position')
ylabel('Y position')


%PART C>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
N=1;
k=10000;
d=1;
for i=1:k
N=1;
z=(2*s).*(rand(d,N))-s;
H1(i)=z;


N=2;
z=(2*s).*(rand(d,N))-s;
t=cumsum(z');
H2(i)=t(N);

N=3;
z=(2*s).*(rand(d,N))-s;
t=cumsum(z');
H3(i)=t(N);


N=5;
z=(2*s).*(rand(d,N))-s;
t=cumsum(z');
H4(i)=t(N);

end
figure
h1=subplot(4,1,1);
hist(H1,50);



[mu,s]=normfit(H1)
normal=normpdf(H1,mu,s);


h2=subplot(4,1,2);
hist(H2,50);

h3=subplot(4,1,3);
hist(H3,50);

h4=subplot(4,1,4);
hist(H4,50);

0 comments

Post a Comment