function KKK =myfunequity_higherleverage(Z)
global solF currente currentK kappa sigma elbarnew beta KKK lambda criticalc dt lambdaprime 
dt=0.25;  
xx=musigma_higherleverage(currente);
mue=xx(1);sigmae=xx(2);
ee=currente+mue*dt+sigmae*Z;
pe=deval(solF,currente,1);
qe=deval(solF,currente,2);
Zcritical=(elbarnew-currente-mue*dt)/sigmae;
currentequity=min((1-lambdaprime)*(pe+qe),currente);

if Z>Zcritical
    if ee>criticalc
        KKK=currentK*(1+(qe-1)/kappa*dt+sigma*Z);
    else
        KKK=currentK*(1+(qe-1)/kappa*dt+sigma*Z);
    end
else 
   gap=Z-Zcritical;
   KK=currentK*(1+(qe-1)/kappa*dt+sigma*Zcritical);
   x=(KK*elbarnew)/(1+elbarnew*beta)*gap;
   KKK=KK*(1-sigma*gap)+beta*x;
end


% global solF currente currentK targetR kappa sigma elbarnew beta KKK lambda criticalc dt lambdaprime 
% xx=musigma_higherleverage(currente);
% mue=xx(1);sigmae=xx(2);
% ee=min(currente+mue*dt+sigmae*Z,50);
% pe=deval(solF,currente,1);
% qe=deval(solF,currente,2);
% Zcritical=(elbarnew-currente-mue*dt)/sigmae;
% currentequity=min((1-lambda)*(pe+qe),currente);
% if Z>Zcritical
% ppe=deval(solF,ee,1);
% qqe=deval(solF,ee,2);
%     if ee>criticalc
%         KKK=currentK*(1+(qe-1)/kappa*dt+sigma*Z);
%         res=(1-lambda)*(ppe+qqe)*(1+(qe-1)/kappa*dt+sigma*Z)/currentequity-1-targetR;
%     else
%         KKK=currentK*(1+(qe-1)/kappa*dt+sigma*Z);
%         res=ee*(1+(qe-1)/kappa*dt+sigma*Z)/currentequity-1-targetR;
%     end
%     
% else
%     
%     gap=Z-Zcritical;
%    ppe=deval(solF,elbarnew,1);qqe=deval(solF,elbarnew,2);
%    KK=currentK*(1+(qe-1)/kappa*dt+sigma*Zcritical);
%    x=(KK*elbarnew)/(1+elbarnew*beta)*gap;
%    KKK=KK*(1-sigma*gap)+beta*x;
%    res=elbarnew*(1+(qe-1)/kappa*dt+sigma*Z+beta*x/currentK)/currentequity-1-targetR;
% end
 

   