%RANDOM WALK TO BROWNIAN MOTION

clf reset;
maxn=10000;

%simple random walk
rv=2*(rand(1,maxn)<0.5)-1; 

%r.w. with uniform steps 
%rv=sqrt(3)*(2*rand(1,maxn)-1); 

%%% below list the number of steps in the 
%%% random walk to approximate B.m. on [0,1] 

for (n=[20 40 80 1000])


   dt=1/n;
   t=[0:dt:1]; 
   vec=zeros(1,n);
   
   vec(1:n)=rv(1:n);
   Bmt = [0 cumsum(sqrt(dt)*vec)];
   
   cv=[0 0 0];
   if (n<=50)
       cv=[1-n/100,n/50, 0];
   end;
   if ((n>50)&&(n<=100))
       cv=[0,1-n/100, n/100];
   end;
   
   plot(t,Bmt,'Color',cv, 'LineWidth', 1.5);
   hold on; 
   
end

title('Random walks to Brownian motion');
hold off;

