%% Scripts used in class, MAT 331 August 28, 2018 %----------------------------------------------- % script to plot a one dimensional random walk % this walk steps -1,+1 with equal probability. N=1000; % make N-long vector of random -1 and +1's x=2*(randi(2,1,N)-1.5); % take cumulative sum y=cumsum(x); figure; hold on; grid on; plot(y,'r'); title('A random walk') return; %----------------------------------------------- % script to plot to compute where a random walk ends % this random walk steps -1,0,1 with equal probability trials=10000; N=1000; y=zeros(1,trials); for k=1:trials % make N-long vector of random -1 and +1's x=(randi(3,1,N)-2); y(k)=sum(x); end % for k figure; hold on; grid on; hist(y,100); title(['Histogram of ',num2str(trials),... ' random walks of length ',num2str(N)]) return;