function [xavg,xmax]=mybrownian3d
%My BROWNIAN3d Three-Dimensional Random Walk
%Keeps track of average and maximum particle distance from origin for a
%random walk in three dimensions

delta=.002;
x=zeros(100,3);

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 
    x=x+delta*randn(size(x));
    xavg(n)=mean(sqrt(x(:,1).^2+x(:,2).^2)+x(:,3).^2);    %compute distance of each particle from origin
    xmax(n)=max(sqrt(x(:,1).^2+x(:,2).^2)+x(:,3).^2);     %take max and average
    n=n+1;
   
end
