function [xavg,xmax]=mybrownian
%My BROWNIAN Two-Dimensional Random Walk
%Keeps track of average and maximum particle distance from origin

shg     %Show Graph
clf     %Clear Plotting Window

set(gcf,'doublebuffer','on')
delta=.002;
x=zeros(100,2);
h=plot(x(:,1),x(:,2),'.');
axis([-1 1 -1 1])
axis square
stop=uicontrol('style','toggle','string','stop');

n=1;                     %step number
nmax=10000;             %Max number of steps        
xavg=zeros(1,nmax);     %Initialize array for avg position
xmax=zeros(1,nmax);     %initialize array for max position

while n<=nmax && get(stop,'value')==0
    x=x+delta*randn(size(x));
    xavg(n)=mean(sqrt(x(:,1).^2+x(:,2).^2);    %compute distance of each particle from origin
    xmax(n)=max(sqrt(x(:,1).^2+x(:,2).^2);     %take max and average
    n=n+1;
    set(h,'xdata',x(:,1),'ydata',x(:,2);
    drawnow 
end
