
function [bv iter_num k]=NewtonMethod2(bet1,bet2,lon12,ns,bv,e)
%bv=acosd(c);
Z=bet1*0;
%c=cosd(bv);%Revert Clairnaut constant to avoid the error in iteration
PV=(ns>0); %Pass Vertex
%t1=Z;
%t2=Z;
dc=Z+inf;
iter_num=Z;
k=[]
dlon=lon12*0+1;
%alp1=asind(c./cosd(bet1))
%alp1= 62.065795632882+1
%lon12a=lon12
od1=ceil(log2(abs(bv)));
od2=ceil(log2(abs(lon12)));
eps1=2.^od1*eps; 
eps2=2.^od2*eps; 
for i=0:20
      
    cont=abs(dbv)>eps1 & abs(dlon)>eps2;
    if ~any(cont), break, end 
    iter_num(cont)=i;              
    bvt=bv(cont);                 
     % t1t(PVt)=-2*bvt(PVt)-bet1t(PVt);
     % t1t(~PVt)=bet1t(~PVt);
    %t1=-2*acosd(ct).*PV(cont)+bet1(cont).*(-1).^PV(cont);
    %t2=bet2(cont);
   
    %alp1=asind(ct./cosd(bet1(cont)));
    %alp1(PV(cont))=180-alp1(PV(cont));
    %lon12ca=lambda12(bet1(cont),bet2(cont),alp1,PV(cont),e);
    %difflon12a=dlambda12(bet1(cont),bet2(cont),alp1,PV(cont),e);
    
    difflon12=dlongitude12(bet1(cont),bet2(cont),bv(cont),PV(cont),e);
    lon12c =longitude12(bet1(cont),bet2(cont),bv(cont),PV(cont),e);  
    
%    difflon12= DerivativeDLon(t2,ct,e)-DerivativeDLon(t1,ct,e);   
 %   lon2c=LongGeodesicLat2Lon(t2,ct,e)-LongGeodesicLat2Lon(t1,ct,e);     
    %lon12cb=lon2cb-lon1cb;
    %[difflon12]
    dlon(cont)=lon12c-lon12(cont);
    delta=dlon(cont)./difflon12;               
    slowcon1=abs((dc(cont)+delta))<eps/100; 
    slowcon2=abs((dc(cont)-delta))<eps/100;
  
    delta(slowcon1)=0;
    delta(slowcon2)=0;
     dc(cont)=delta;
     
    %delta(slowcon2)=(1.618033)*delta(slowcon2);
    %delta(slowcon1)=(0.5+rand*2/10)*delta(slowcon1);
     bvt=bvt-delta;  
         criticalbv=abs(bet1(cont));
     falure=ct>criticalbv;
     bvt(falure)=criticalbv(falure)-(i+2)*eps*2^10;      
    %overbet1=ct>cosd(bet1(cont))
    %ct(overbet1)=cosd(bet1(overbet1))-10*eps;    
    
    bv(cont)=bvt;
    
    
      %bv(cont)=acosd(ct);         
   %k=[k;c lon12cb-lon12c  difflon12-difflon12b alp1 w2-w1 omg12];
       k=[k;c' dc' dlon' ];%dlona' asind(c./cosd(bet1))' ];%dlon'
      %dlon'
    
      
end


end