clear all; close all;

% delta=1; beta=2; alfa=2; gamma=1; x0=1/3*0.1; tfin=10; % ASINTOTICAMENTE STABILE
delta=4; beta=2; alfa=2; gamma=1; x0=1/6*0.9; tfin=10; % INSTABILE
% delta=2; beta=2; alfa=2; gamma=1; x0=1/4; tfin=10; % SEMPLICEMENTE STABILE X0=0.25=equilibrio
% delta=2; beta=2; alfa=2; gamma=1; x0=1/4*0.4; tfin=10; % SEMPLICEMENTE STABILE X0=0.1
% delta=2; beta=2; alfa=2; gamma=1; x0=1/4*0.8; tfin=10; % SEMPLICEMENTE STABILE X0=0.2

x0in=x0;
stato=x0;
for ind=2:tfin
    x=stato(ind-1);
    stato=[stato;(alfa-gamma)/beta-delta/beta*gamma*x];
end;
% ------------------------
%%%%%%%%%%% MOVIMENTO %%%%%%%%%%%
figure;
plot(stato,'-*');
xlabel( 'tempo' );
ylabel( 'x(t)' );
pause;
% ------------------------
%%%%%%%%%% COSTRUZIONE DEL DIAGRAMMA DI MORAN "DINAMICO" %%%%%%%%%%%
moran1=ones(2*tfin,1);
moran2=ones(2*tfin,1);
for h=1:tfin
    moran1(2*h-1)=stato(h);
    moran1(2*h)=stato(h);
end;
moran2=[0;moran1(3:2*tfin);1];
figure;
hold on
x=0:0.01:1;
y=(alfa-gamma)/beta-delta/beta*gamma*x;
plot(x,y,'r');
plot(x,x,'k');
xlabel( 'x(t)' )
ylabel( 'x(t+1)' )
title('diagramma di Moran');
plot(x0in,0,'bo'); pause;
for ind=1:length(moran1)-2
    plot([moran1(ind) moran1(ind+1)],[moran2(ind) moran2(ind+1)],'b-');
    % plot(moran1(ind),moran2(ind),'b.');
    % plot(stato(1:length(stato)-1),stato(2:length(stato)),'*');
    pause(0.5)
end;
pause;

% -----------------------
% massimo=1.2*max(x0,alfa/beta*exp(-1));
figure;
hold on;
axis([0 1.1 -1 1]);
plot([0 1.1],[0 0],'k');
axis('off');
text(1.1,-0.1,'x(t)');
text(0,-0.1,'0');
title('traiettoria nello spazio di stato x');
plot(stato(1),0,'ro','markersize',5);
pause;
for ind=1:length(stato)
    plot(stato(ind),0,'b*','markersize',15);
    pause(0.5);
end

