function [out,uu,step1,tt] = secant_cont_circle(func,x,x_old,delta)
% 
%function [out,uu,step1,tt] = secant_cont(func,x,x_old)
%
global vv n x_pred teller Y
global IX IU
global uorg

%%%%%%%%%%%%%%%%%%%%%
% rotation=0
% newR=x(IU(4:5))-uorg(4:5);
% newR=newR/norm(newR);
% prevR=newR;  
%%%%%%%%%%%%%%%%%%%%%

%constants 
teller = 1;
%delta = .04;
%delta = .004;
%delta=0.04;
deltamax=10*delta;
max_cont=60000;
max_iter=100;
out(1,:)=x';
tt(1)=0;

%Initialization
uu=[];
tt=zeros(1,max_cont);
step1=zeros(1,max_cont);

   n = (x-x_old)/norm((x-x_old),2)*delta;
   x_pred = x+n;
   x_old=x;

   t=cputime;[temp1,B,iter]=broyden_secant(func,x_pred);
   teller=teller+1;

   tt(teller)=cputime-t;
   out(teller,:) = temp1';  
   %out(teller,218)
   temp1(IU(2))
   x=out(teller,:)';
   step1(teller)=norm((x-x_old),2);
   %uu(teller,:)=vv;      

  success = 0;
  delta=delta*1.5;
  ntries=teller;
while (success ~=1 )
  delta=delta/1.5
  
  while ntries < max_cont 
     ntries = ntries+1;
     teller = teller +1   
%%%%%if (teller> max_cont)
%%    if (teller>= max_cont | temp1(IU(2))>out(1,IU(2))*1.3)
    if (ntries>= max_cont | x(IU(1))>=1 | ~isfinite(temp1(IU(1))))
         success=1
	 break
    end	 
    n = (x-x_old)/norm((x-x_old),2)*delta;
    x_pred = x+n;
    x_old=x;
    t=cputime;
    [temp1,B,no_iter]=broyden_secant(func,x_pred,B);
    if no_iter< 30 %max_iter
      disp([temp1(IU(1))',no_iter])
      %plot(temp1(IU(3)),Y(1),'.');drawnow;
      
      tt(teller)=cputime-t;
      out(teller,:) = temp1';
      %uu(teller,:)=vv; 
      x= out(teller,:)';
      step1(teller)=norm((x-x_old),2);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
%       newR=x(IU(4:5))-uorg(4:5);
%       newR=newR/norm(newR);
%       M =[prevR(1),-prevR(2);prevR(2),prevR(1)];
%       XY=inv(M)*newR;
%       angle=atan2(XY(2),XY(1));
%       rotation=rotation+angle;
%     
%       prevR=newR;  
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

   
      if(no_iter<5)
      	delta=min(delta*1.5,deltamax);
      end
   else % no_iter >max_iter   
      disp('Backstepping');
       success = 0;
       teller=teller-1;
       x_old=out(teller-1,:)';
       x    =out(teller,:)';
       break 
    end %if        
  end %while 


end %while














