%> =======================================================================
%> The file <henon1.m> displays the properties of the Henon 
%> map in R^2, including the form of the 'attractor'
%> The iteration is: [x,y] = [1 - a*x^2 + y, b*x]
%> You can input the parameter values a and b.
%> First, it makes an number of iterations of a set of initial values 
%> forming a rectangular pattern; this shows the horseshoe action of the map.
%> Second, it makes 100 or more iterations to make a rough display of the 
%> fractal form of the attractor. You can zoom in a smaller area by marking
%> the lower left and upper right corner of a smaller rectangle. 
%> G Lindblad  gli@theophys.kth.se - Version of 020924
%> =======================================================================
clear;


q0=1;

while  q0==1

help henon1,

%%%%%%%%% INPUT PARAMETERS

pp = input('> Give parameters [a,b];  default [1.4  .3] >>> ');

if isempty(pp)

p1=1.4; p2=.3;

else  

p1=pp(1); p2=pp(2);

end 

a1= (- 1 + p2 + sqrt((1-p2)^2 + 4*p1))/p1/2;
a2= (- 1 + p2 - sqrt((1-p2)^2 + 4*p1))/p1/2;

b1=[a1, p2*a1]; b2=[a2, p2*a2];

disp('> The fixed points are = ');
disp(b1), disp(b2);


disp('> First we use as IC a 20 x 20 lattice forming a rectangle..');

p3=input('> Give the center of the square as a vector, default = [0,0] >>> ');

if isempty(p3)

p3=[0 0];

end

p4=input('> Give the lengths of the edges, default = [1 , 1] >>> ');

if isempty(p4)

p4=[1  1];

end

%%%%%%%%% INVARIANT POINTS, DECIDE SIZE OF DISPLAY
a1= (- 1 + p2 + sqrt((1-p2)^2 + 4*p1))/p1/2;
a2= (- 1 + p2 - sqrt((1-p2)^2 + 4*p1))/p1/2;

b1=[a1, p2*a1]; b2=[a2, p2*a2];
 
m1=ceil(b1); m2=floor(b2);
xy=[ m2(1)  m1(1)+1  m2(2)  m1(2)];  

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure(gcf),
x0=p3(1) + p4(1)*linspace(-.5 , .5, 20);
y0=p3(2) + p4(2)*linspace(-.5 , .5, 20);
[x1,y1]=meshgrid(x0,y0);
plot(x1, y1, '. b'); hold off;
axis(xy);
title('Starting points');

uicontrol('Style','pushbutton','Units','normalized',...
'Position',[.75  .01  .15  .05],'String','ITERATE',...
'BackGroundColor',[.0 .0 .9],'ForeGroundColor','w',...
'Fontsize',12,'Callback','uiresume, q2=0;');
uiwait; 

qr=1;

while qr==1;

plot(x1, y1, '.r'); ;
axis(xy);hold on;


x2 =  1 - p1*x1.^2  + y1; y2 = p2*x1;
plot(x2, y2, '. b');
title('Two successive iterations');
xlabel('Last iteration is blue');
plot(b1(1), b1(2), 'o k','MarkerSize',20);
plot(b2(1), b2(2), 'o k', 'MarkerSize',20);
hold off 
x1=x2; y1=y2;

uicontrol('Style','pushbutton','Units','normalized',...
'Position',[.6  .01  .15  .05],'String','ITERATE',...
'BackGroundColor',[.0 .0 .9],'ForeGroundColor','w',...
'Fontsize',12,'Callback','uiresume; qr=1;');

uicontrol('Style','pushbutton','Units','normalized',...
'Position',[.75  .01  .15  .05],'String','STOP',...
'BackGroundColor',[.0 .0 .9],'ForeGroundColor','w',...
'Fontsize',12,'Callback','uiresume,qr=0;');
uiwait; 

end 

uicontrol('Style','pushbutton','Units','normalized',...
'Position',[.45  .01  .15  .05],'String','BACK',...
'BackGroundColor',[.0 .0 .9],'ForeGroundColor','w',...
'Fontsize',12,'Callback','uiresume; q1=0;q0=1;');

uicontrol('Style','pushbutton','Units','normalized',...
'Position',[.6  .01  .15  .05],'String','CONTINUE',...
'BackGroundColor',[.0 .0 .9],'ForeGroundColor','w',...
'Fontsize',12,'Callback','uiresume; q1=1;');

uicontrol('Style','pushbutton','Units','normalized',...
'Position',[.75  .01  .15  .05],'String','QUIT',...
'BackGroundColor',[.0 .0 .9],'ForeGroundColor','w',...
'Fontsize',12,'Callback','uiresume,q1=0;q0=0;');
uiwait;  close;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


while q1==1

disp('> The parameters are >>> ');
disp([p1 p2]);

pp = input('> Give new parameters [a,b], or <return> >>> ');

if isempty(pp)==0

p1=pp(1); p2=pp(2); 

else 

end 

%%%%%%%%% INVARIANT POINTS, DECIDE SIZE OF DISPLAY
a1= (- 1 + p2 + sqrt((1-p2)^2 + 4*p1))/p1/2;
a2= (- 1 + p2 - sqrt((1-p2)^2 + 4*p1))/p1/2;

b1=[a1, p2*a1]; b2=[a2, p2*a2];
 
m1=ceil(b1); m2=floor(b2);

xy=[ m2(1)  m1(1)+1  m2(2)  m1(2)];  

seed1=rand(1,300); seed2=rand(1,300);

x1=.2*seed1; y1=.1*seed2;

z1=[x1; y1]; 

disp('> There are 300 random starting points!!');

N = input('> Give the number of iterations, default N = 100 >>> ');

if isempty(N)

N = 100;

end

for iter=1:200

z1 = [1 - p1*z1(1,:).^2 + z1(2,:); p2*z1(1,:)];

end 

wwx = z1(1,:);  wwy = z1(2,:); %%% discard the 200 first iterates

for iter=1:N

z1 = [1 - p1*z1(1,:).^2 + z1(2,:); p2*z1(1,:)];

wwx=[wwx; z1(1,:)]; wwy=[wwy; z1(2,:)];

end 

figure(gcf), 
plot(wwx,wwy, '. k');
axis(xy);
hold on; 
plot(b1(1), b1(2), 'o r');
plot(b2(1), b2(2), 'o r');
title('Attractor with the two fixed points marked');
xlabel('You can zoom in a smaller area: click with mouse!');
hold off; 

uicontrol('Style','pushbutton','Units','normalized',...
'Position',[.6  .01  .15  .05],'String','ZOOM IN',...
'BackGroundColor',[.0 .0 .9],'ForeGroundColor','w',...
'Fontsize',12,'Callback','uiresume, q2=1;  ');

uicontrol('Style','pushbutton','Units','normalized',...
'Position',[.75  .01  .15  .05],'String','CONTINUE',...
'BackGroundColor',[.0 .0 .9],'ForeGroundColor','w',...
'Fontsize',12,'Callback','uiresume, q2=0;');
uiwait;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

while q2==1

[x3,y3] = ginput(2); 

xy1 = [min(x3)  max(x3)  min(y3)  max(y3)];

plot(wwx,wwy, '. k');
axis(xy1);
hold on; 
plot(b1(1), b1(2), 'o r');
plot(b2(1), b2(2), 'o r');
hold off;

uicontrol('Style','pushbutton','Units','normalized',...
'Position',[.6  .01  .15  .05],'String','ZOOM IN',...
'BackGroundColor',[.0 .0 .9],'ForeGroundColor','w',...
'Fontsize',12,'Callback','uiresume, q2=1;  ');

uicontrol('Style','pushbutton','Units','normalized',...
'Position',[.75  .01  .15  .05],'String','CONTINUE',...
'BackGroundColor',[.0 .0 .9],'ForeGroundColor','w',...
'Fontsize',12,'Callback','uiresume, q2=0;');
uiwait; 

end  %%%%%%%%%%%%%%%%%%% end of q2=1

uicontrol('Style','pushbutton','Units','normalized',...
'Position',[.45  .01  .15  .05],'String','REPEAT',...
'BackGroundColor',[.0 .0 .9],'ForeGroundColor','w',...
'Fontsize',12,'Callback','uiresume, q1=1;  ');

uicontrol('Style','pushbutton','Units','normalized',...
'Position',[.6  .01  .15  .05],'String','TOP',...
'BackGroundColor',[.0 .0 .9],'ForeGroundColor','w',...
'Fontsize',12,'Callback','uiresume, q1=0; q0=1; ');

uicontrol('Style','pushbutton','Units','normalized',...
'Position',[.75  .01  .15  .05],'String','QUIT',...
'BackGroundColor',[.0 .0 .9],'ForeGroundColor','w',...
'Fontsize',12,'Callback','uiresume, q1=0; q0=0;');
uiwait; close;

end %%%%%%%%%%%%%%%%%%%%%%%%% end of q1=1
end %%%%%%%%%%%%%%%%%%%%%%%%  end of q0=1
disp('> Closing <henon1>!');


