function res=musigma_higherleverage(x)
global solF etah kappa A delta gamma rho lambda sigma m phi lambdaprime criticalc 
pe=deval(solF,x,1);
[prime1,prime2]=deval(solF,x,3);
peprime=prime1;
peprime2=prime2;
qe=deval(solF,x,2);
[prime1,prime2]=deval(solF,x,4);
qeprime=prime1;
qeprime2=prime2;

we=pe+qe;weprime=peprime+qeprime;weprime2=peprime2+qeprime2;
%eta=etal.*(espan<eentry)+etah.*(espan>=eentry);
i=(qe-1)/kappa;
c=A-delta-i-i^2/2*kappa;

ttheta=max(we/x,1/(1-lambda)); %checked
tthetaprime=we/x*(x<criticalc)+1/(1-lambdaprime)*(x>=criticalc);

sigmae=(x*we*sigma*(m*ttheta-1))/(we-x*m*ttheta*weprime); %checked 

Fe=(pe+qe)/x-m*ttheta*(peprime+qeprime); %checked

Ge=c.*kappa.*Fe+gamma.*qe.*qeprime.*m.*(1-ttheta).*we; % checked

rr=(rho+gamma.*i).*c.*kappa.*(we./x-m.*ttheta.*(weprime))...
    -gamma.*qe.*qeprime.*((-i-etah).*we+m.*ttheta.*(0.5.*weprime2.*sigmae.^2+(A-delta)./phi+(pe-((1-phi)./phi)).*i-((1-phi)./phi).*0.5.*kappa.*i.^2+delta.*(1-qe)))...
    -gamma.*(qe.*qeprime2+qeprime.^2).*sigmae.^2.*0.5.*(we./x-m.*ttheta.*(weprime))...
    -0.5*gamma.*(1+gamma).*(we./x-m.*ttheta.*(weprime)).*kappa.*((c.*sigma-qe.*qeprime.*sigmae./kappa).^2)./c; 
r=rr./Ge;% rewritten, checked

sigmaeprime=(x*we*sigma*(m*tthetaprime-1))/(we-x*m*tthetaprime*weprime); %checked 

musigmaeprime=((((m*r*(1-tthetaprime)-i-etah)*we))...
    +m*tthetaprime*((1/2)*weprime2*sigmaeprime^2+((A-delta)/phi)+(pe-(((1-phi))/phi))*i-((1-phi)/phi)*(kappa/2)*i^2+delta*(1-qe)))...
/((we/x-m*tthetaprime*weprime));


% 
% Ge=c*kappa*Fe+gamma*qe*qeprime*m*(1-ttheta)*we; % checked
% 

%assuming that risk premium does not change
%strat here
 musigmae=c*kappa/Ge*((m*(1-ttheta)*(rho+gamma*i-gamma*(0.5*(qe*qeprime2+qeprime^2)*sigmae^2)/(c*kappa)...
     -0.5*gamma*(1+gamma)*(c*sigma-qe*qeprime*sigmae/kappa)^2/c^2)-i-etah)*we...
     +m*ttheta*(0.5*(weprime2)*sigmae^2+(A-delta)/phi+(pe-(1-phi)/phi)*i-(1-phi)/phi*kappa*0.5*(i^2)+delta*(1-qe)));  % rewritten, checked

rp_h=(peprime*musigmae+(1/2)*peprime2*sigmae^2+((1-phi)/phi)*c)/pe+i-r; % checked
rp_k=(qeprime*musigmae+(1/2)*qeprime2*sigmae^2+A)/qe-delta-r; %checked

riskpremium=qe/we*rp_k+pe/we*rp_h;
musigmaeprime=x*(m*r+m*tthetaprime*(riskpremium-r)-etah-i); %assuming that risk premium does not change

%up to here

mueprime=musigmaeprime-sigma.*sigmaeprime;


res=[mueprime;sigmaeprime;i+delta];