
clear all

global a h e

%my first integration  xdot = x(1-x)
% x(i+1)=x(i)+ delta *x(i)*(1-x(i));
a=1;

e=1;

Nt=400;
x=zeros(Nt,1);
T=linspace(0,2*pi,Nt);
dt=T(2)-T(1);

X=linspace(0.0001,1.2,200);
X1=X(X<0.5);
X2=X(X>=0.5);
H=linspace(0,1/4,31);
%H=H(10);
for ih=1:length(H);
    h=H(ih);

for ix=1:length(X);
x0=X(ix);
x(1)=x0;
    
for ia=2:Nt;
    y=x(ia-1);t=T(ia-1);
    x(ia)=x(ia-1)+dt*xdotht(y,t);
    if x(ia)<0;x(ia)=0;end;
end;

% 
%     figure(37)
%     plot(T,x,'b','Linewidth',2)
%     xlabel('t','Fontsize',24)
%     ylabel('x(t)','Fontsize',24)
%     xlim([0 2*pi])
%     title('Logistic')
%     hold on
%     
      P(ix)=x(Nt);
      if x(Nt)==0;P(ix)=NaN;end;
%     x(1)=x(Nt);
%     for ia=2:Nt;
%     y=x(ia-1);t=T(ia-1);
%     x(ia)=x(ia-1)+dt*xdotht(y,t);
%    % if x(ia)<0;x(ia)=0;end;
% end;
% 
%    P2(ix)=x(Nt);
     
end



figure(38)
    plot(X,P,'ob',X,X,'r','Linewidth',2)
    xlabel('$X$','Interpreter','latex','Fontsize',24)
    ylabel('$P(X)$','Interpreter','latex','Fontsize',24)
    ylim([0 1])
    title('Poincare Map')
    drawnow
    hold on
    
    
    
   
    
   %if min(abs(X-P))<0.01
   [sd1,ind1]=min(abs(X1-P(X<0.5)));
   Fm(ih)=X1(ind1);
  
  
   [sd2,ind2]=min(abs(X2-P(X>=0.5)));
    Fp(ih)=X2(ind2);
%     else;
%         Fm(ih)=NaN;
%         Fp(ih)=NaN;
%     end;
%     
   %[sd2,ind2]=min(abs(X-P2));[X(ind1),P(ind1)]

end;
hp=(1+sqrt(1-4*H))/2;
hm=(1-sqrt(1-4*H))/2;
   
figure(39)
    plot(H,Fm,'--r',H,Fp,'b',H,hp,'--k',H,hm,'--k','Linewidth',2)
    xlabel('$h$','Interpreter','latex','Fontsize',24)
    ylabel('$X_e(h)$','Interpreter','latex','Fontsize',24)
    title('$Bifurcation ~diagram$','Interpreter','latex','Fontsize',20)
    hold on
% 

