% Unit 4 (MATLAB Course @ UAM)
% (c) Alexander Knebe

%==========================================================================
% Jumper
%==========================================================================
clear all
close all
clc

% the data
k   = 1.3806488e-23;
T   = 307.5;
dE  = 6.885e-21;
xl  = 2.88;

% probablities: [no_jump, right_jump, left_jump]
p = [1-exp(-dE/(k*T)), 0.5*exp(-dE/(k*T)), 0.5*exp(-dE/(k*T))];
d = [               0,              +0.36,              -0.36]; % jump distances
pcsum = cumsum(p);

%==========================================================================
% a)
%==========================================================================
Njumps = 20;
Npart  = 5000;

x = zeros(Npart,Njumps);
for k=1:Npart
    for i=2:Njumps
        ip = find(rand(1)<pcsum);
        x(k,i) = x(k,i-1) + d(1,ip(1));
        
        % at the border no jumps are allowed
        if(x(k,i) < -xl | x(k,i) > +xl)
            x(k,i) = x(k,i-1);
        end
    end
end


%==========================================================================
% b)
%==========================================================================
hist(x(:,Njumps),25)
xlabel('position [nm]')
ylabel('number of positions')

%==========================================================================
% c)
%==========================================================================
xmean = mean(x(:,Njumps))
xstd  = std(x(:,Njumps))