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

%==========================================================================
% jumping rabbit
%==========================================================================
clear all
close all
clc

% probablities for movement
p = [0.1 0.2 0.3 0.4];

d = [  0   0  5 -4;
      20 -10  0  0];
  
pcsum = cumsum(p);

Njumps = 1000;

x = zeros(1,Njumps);
y = zeros(1,Njumps);

for i=2:Njumps
    ip = find(rand(1)<pcsum);
    x(i) = x(i-1) + d(1,ip(1));
    y(i) = y(i-1) + d(2,ip(1));
end

figure(1), hold on
plot(x,y,'k')
plot(0,0,'xr','MarkerSize',20)
xlabel('x [cm]')
ylabel('y [cm]')
plot(x(Njumps),y(Njumps),'xb','MarkerSize',20)
legend('trajectory','start-point','end-point')

%%
% this plots the cumulative probability distribution
figure(2), hold on
set(gca,'fontsize',20)
plot(linspace(0,1,length(pcsum)+1),[0 pcsum],'s','MarkerSize',16,'LineWidth',5)
plot(linspace(0,1,length(pcsum)+1),[0 pcsum],'-','LineWidth',5)
ylabel('cumulative probability','FontSize',20)
xlabel('uniform random number in [0,1]','FontSize',20)
   