% C3_3.m
% Solution to problem 3 of control 3, 2015/04/17
% J.M.Soler

% Set system parameters
clear all
nj = 200;                    % number of jumps
np = 1000;                   % number of particles
d = 2.55;                    % lattice parameter in Ang

% Find random walks of np particles and nj jumps
[x,y] = SaltosC3_3(nj,np);

% Find and plot histogram of final x and y positions
figure(1)
xh = (-30:5:30)*d;           % histogram bin centers
nxh = hist( x(nj,:), xh );   % number of particles in each bin
nyh = hist( y(nj,:), xh );
bar( xh', [nxh',nyh'] )
legend('x','y')
xlabel('distance (Ang)')
ylabel('number of walks')

% Find and plot average distance to origin
figure(2)
r = sqrt(x.^2+y.^2);
rmean = sum(r,2)/np;
plot(rmean)
xlabel('time step')
ylabel('mean distance')