% Scripts for MAT 331, Nov 16, 2017 % Random Walks % plot a n-step random walk on the integers as a % function of time function rw(n) x=2*randi(2,n,1)-3 y=cumsum(x) figure; grid on; hold on; title(['Simple random walk, steps = ',num2str(n)]); plot(y) return %-------------------------------------------- % plot multiple n-step random walk on the integers as a % function of time function rw_mult(n,N) clear endpt; figure; grid on; hold on; title(['Simple random walk, steps = ',num2str(n)]); for k=1:N clear y; x=2*randi(2,n,1)-3; y=cumsum(x); endpt(k)=y(end); %plot(y) end figure; hold on; grid on; title('Histogram of final point'); a=min(endpt); b=max(endpt); delta=2*ceil((b-a)/100); edges=[a-.5:delta:b+.5] histogram(endpt,edges); return %---------------------------------------------- % plot a n-step random walk in the plane as a % function of time function rw_2d(n) flag=3; % there are many ways to make a random walk % in the plane; here are three possibilities switch flag case 1 % choose each coordinate to be a random walk x1=2*randi(2,n,1)-3; y1=cumsum(x1); x2=2*randi(2,n,1)-3; y2=cumsum(x2); % choose one of four dirctions to move case 2 x=randi(4,n,1); y=cumsum(1i.^x); y1=real(y); y2=imag(y); case 3 % choose a random direction to move in x=exp(2*pi*1i*rand(n,1)); y=cumsum(x); y1=real(y); y2=imag(y); end % switch flag figure; grid on; hold on; title(['Simple random walk, steps = ',num2str(n)]); plot(y1,y2) return % compute distance from origin clear d; for k=1:n d(k)=sqrt(y1(k)^2+y2(k)^2); end % for figure; grid on; hold on; title(['Distance from origin in 2D, steps = ',num2str(n)]); plot(d) return %--------------------------------------------- % plot a n-step random walk in the plane as a % function of time function rw_3d(n) flag=2; % there are many ways to make a random walk % in the plane; here are three possibilities switch flag case 1 % choose each coordinate to be a random walk x1=2*randi(2,n,1)-3; y1=cumsum(x1); x2=2*randi(2,n,1)-3; y2=cumsum(x2); x3=2*randi(2,n,1)-3; y3=cumsum(x3); % choose one of four dirctions to move case 2 y1=cumsum(2*rand(n,1)-1); y2=cumsum(2*rand(n,1)-1); y3=cumsum(2*rand(n,1)-1); end % switch flag figure; grid on; hold on; title(['Random walk in 3D, steps = ',num2str(n)]); plot3(y1,y2,y3) return % compute distance from origin for k=1:n d(k)=sqrt(y1(k)^2+y2(k)^2+y3(k)^2); end % for figure; grid on; hold on; title(['Distance from origin in 3D, steps = ',num2str(n)]); plot(d) return % ------------------------------------------ % plot a n-step random walk in the plane as a % function of time function rw_3d(n) flag=2; % there are many ways to make a random walk % in the plane; here are three possibilities switch flag case 1 % choose each coordinate to be a random walk x1=2*randi(2,n,1)-3; y1=cumsum(x1); x2=2*randi(2,n,1)-3; y2=cumsum(x2); x3=2*randi(2,n,1)-3; y3=cumsum(x3); % choose one of four dirctions to move case 2 y1=cumsum(2*rand(n,1)-1); y2=cumsum(2*rand(n,1)-1); y3=cumsum(2*rand(n,1)-1); end % switch flag figure; grid on; hold on; title(['Random walk in 3D, steps = ',num2str(n)]); plot3(y1,y2,y3) return % compute distance from origin for k=1:n d(k)=sqrt(y1(k)^2+y2(k)^2+y3(k)^2); end % for figure; grid on; hold on; title(['Distance from origin in 3D, steps = ',num2str(n)]); plot(d) return