function thestar = Funccapifliendomini(bta,muu,sig,gA,pistar,Rstar,ge,CG,EG,ImG,Nss,del,phi,am)
%function thestar = Funccapifliendomini
x_ini = 0.05;
% bta=0.995;
% muu=1;
% sig=0.9540; % 0.721;
% gA=1.02;
% pistar=1.005;
% Rstar=1.0175; % 1.0120;
% ge=1;
% CG=0.43;
% EG=0.23;
% ImG=0.20;
% Nss=0.40;
% del=0.025;
% phi=10;
% am=0.06;
thestar = broyden(@(thestar)SolveTheta(thestar,bta,muu,sig,gA,pistar,Rstar,ge,CG,EG,ImG,Nss,del,phi,am),x_ini);
end

function res = SolveTheta(thestar,bta,muu,sig,gA,pistar,Rstar,ge,CG,EG,ImG,Nss,del,phi,am)
IG      =1-CG-EG+ImG;
KG      =IG/(gA-1+del);
r       =gA/bta+del-1;
v       =(phi-1)/phi;
sigtil  = (log(1+sig^2/muu^2))^(1/2);
muutil  = log(muu)-1/2*sigtil^2;
F1      = normcdf((log(thestar)-muutil)/sigtil);
F2      = normcdf((log(thestar)-(muutil+sigtil^2))/sigtil);
Phi     = thestar*F1+muu*(1-F2);
Psi     = -Phi+thestar+muu;
The     = muu-Psi;
tau     = Phi*Rstar*ge*bta/(gA*pistar);
alf     = tau*Psi*(r*KG+ImG)/((tau-1)*The*v*CG+(1+ImG)*tau*Psi*v);
YG      = (r*KG+ImG)/(alf*v);
an      = (1-alf)/alf*Psi*Phi/(thestar*Nss)*(r*KG+ImG)/CG;
mG      = am*(1-alf)*gA*pistar/bta*v/(an*(gA*pistar/bta-1)*Nss)*YG;
res     = The*CG*(gA*pistar-Rstar)/(Psi*((EG-ImG)*gA*pistar-mG*(gA*pistar-1)))-tau;
%res     = tau-The*CG*(gA*pistar-Rstar)/(Psi*((EG-ImG)*gA*pistar-mG*(gA*pistar-1)));
end