function [ys,params,check] = testnocapital_steadystate(ys,exo,M_,options_)
% Inputs: 
%   - ys        [vector] vector of initial values for the steady state of
%                   the endogenous variables
%   - exo       [vector] vector of values for the exogenous variables
%   - M_        [structure] Dynare model structure
%   - options   [structure] Dynare options structure
%
% Output: 
%   - ys        [vector] vector of steady state values for the the endogenous variables
%   - params    [vector] vector of parameter values
%   - check     [scalar] set to 0 if steady state computation worked and to
%                    1 of not (allows to impose restrictions on parameters)

% Copyright (C) 2013-2020 Dynare Team
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.

% read out parameters to access them with their name
NumberOfParameters = M_.param_nbr;
for ii = 1:NumberOfParameters
  paramname = M_.param_names{ii};
  eval([ paramname ' = M_.params(' int2str(ii) ');']);
end
n = exo(strmatch('n',M_.exo_names,'exact'));
pargamma = exo(strmatch('pargamma',M_.exo_names,'exact'));
% initialize indicator
check = 0;


%% Enter model equations here

options = optimset('Display', 'off');
Psi=(1-omega)/(1+n-pargamma);
RK=Rast*(1+tauB)-(1-pardelta);
DA=1+gast;
PiN=DA^(1-zetaN);
Pir=PiN^(1-alphar);
Piw=PiN^(1-alphaw);


[guessresult,faval,exitflag]=fsolve(@(guess)AAstequation(guess,DA,rhoL,RK,omega,Rast,alphaw,alphar,n,tauB,zetaT,zetaN,pardelta,pargamma,Pir,Piw,parsigma,parbeta),[Rast*(1+0.5*tauB),0.5],options);
if exitflag <1
    %indicate the SS computation was not sucessful; this would also be detected by Dynare
    %setting the indicator here shows how to use this functionality to
    %filter out parameter draws
    check=1; %set failure indicator
    return; %return without updating steady states
end
R=guessresult(1);
TNwTLT=guessresult(2);
xir=1-pargamma*parbeta^(parsigma)*(R/Pir)^(parsigma-1);
findxiw=@(xiw)(1/xiw-1-parbeta^(parsigma)*(R/Piw*(omega+(1-omega)*(xir/xiw)^(1/(1-parsigma))))^(parsigma-1)/xiw);
[xiw,faval,exitflag]=fzero(findxiw,xir,options);
if exitflag <1
    %indicate the SS computation was not sucessful; this would also be detected by Dynare
    %setting the indicator here shows how to use this functionality to
    %filter out parameter draws
    check=1; %set failure indicator
    return; %return without updating steady states
end
Omega=omega+(1-omega)*(xir/xiw)^(1/(1-parsigma));
lambda=(1-omega)/(1-omega*(1-xir)*R/DA/(1+n));
AAst=1-(DA-1)/(c*TNwTLT);
TANwTKT=(zetaT*TNwTLT^(1-zetaT)/RK)^(1/(1-zetaT))*DA*(1+n);
TATwT=RK*TANwTKT/(DA*(1+n))/TNwTLT*(1-zetaT)/zetaT;
TNwTLN=(1-TNwTLT^((rhoL+1)/rhoL))^(rhoL/(rhoL+1));
TATwN=(TNwTLN/TNwTLT)^(1/rhoL)*TATwT;
TANwTKN=TATwN*TNwTLN*zetaN/(1-zetaN)*DA*(1+n)/RK;
TNwTYN=(TANwTKN/(DA*(1+n)))^(zetaN)*TNwTLN^(1-zetaN);
TANwTYT=(TANwTKT/(DA*(1+n)))^(zetaT)*TNwTLT^(1-zetaT);
TATPN=TATwN*TNwTLN/(1-zetaN)/TNwTYN;
TANwTH= (TATwT*TNwTLT+TATwN*TNwTLN)/(1-omega*DA/(Omega*R));
TANwTB=(TATPN*TNwTYN-(1-alphaw)*xiw*TANwTH)/((1-alphaw)*xiw*(1-lambda)*R/(DA*(1+n))+(1-alphar)*xir*lambda*R/(DA*(1+n)));
TANwTCT=alphaw*xiw*((1-lambda)*R*TANwTB/(DA*(1+n))+TANwTH)+alphar*xir*lambda*R*TANwTB/(DA*(1+n));
TANwTBF=TANwTB-(TANwTKT+TANwTKN);
TNwTCN=TNwTYN;


%% end own model equations

params=NaN(NumberOfParameters,1);
for iter = 1:length(M_.params) %update parameters set in the file
  eval([ 'params(' num2str(iter) ') = ' M_.param_names{iter} ';' ])
end

NumberOfEndogenousVariables = M_.orig_endo_nbr; %auxiliary variables are set automatically
for ii = 1:NumberOfEndogenousVariables
  varname = M_.endo_names{ii};
  eval(['ys(' int2str(ii) ') = ' varname ';']);
end


end

function [result] = AAstequation(guess,DA,rhoL,RK,omega,Rast,alphaw,alphar,n,tauB,zetaT,zetaN,pardelta,pargamma,Pir,Piw,parsigma,parbeta)
    options = optimset('Display', 'off');
    result=nan(2,1);
    R=guess(1);
    TNwTLT=guess(2);
    xir=1-pargamma*parbeta^(parsigma)*(R/Pir)^(parsigma-1);

    findxiw=@(xiw)(1/xiw-1-parbeta^(parsigma)*(R/Piw*(omega+(1-omega)*(xir/xiw)^(1/(1-parsigma))))^(parsigma-1)/xiw);
    [xiw,faval,exitflag]=fzero(findxiw,xir,options);
    if exitflag <1
        %indicate the SS computation was not sucessful; this would also be detected by Dynare
        %setting the indicator here shows how to use this functionality to
        %filter out parameter draws
        check=1; %set failure indicator
        return; %return without updating steady states
    end
    Omega=omega+(1-omega)*(xir/xiw)^(1/(1-parsigma));
    lambda=(1-omega)/(1-omega*(1-xir)*R/DA/(1+n));
    TANwTKT=(zetaT*TNwTLT^(1-zetaT)/RK)^(1/(1-zetaT))*DA*(1+n);
    TATwT=RK*TANwTKT/(DA*(1+n))/TNwTLT*(1-zetaT)/zetaT;
    TNwTLN=(1-TNwTLT^((rhoL+1)/rhoL))^(rhoL/(rhoL+1));
    TATwN=(TNwTLN/TNwTLT)^(1/rhoL)*TATwT;
    TANwTKN=TATwN*TNwTLN*zetaN/(1-zetaN)*DA*(1+n)/RK;
    TNwTYN=(TANwTKN/(DA*(1+n)))^(zetaN)*TNwTLN^(1-zetaN);
    TANwTYT=(TANwTKT/(DA*(1+n)))^(zetaT)*TNwTLT^(1-zetaT);
    TATPN=TATwN*TNwTLN/(1-zetaN)/TNwTYN;
    TANwTH= (TATwT*TNwTLT+TATwN*TNwTLN)/(1-omega*DA/(Omega*R));
    TANwTB=(TATPN*TNwTYN-(1-alphaw)*xiw*TANwTH)/((1-alphaw)*xiw*(1-lambda)*R/(DA*(1+n))+(1-alphar)*xir*lambda*R/(DA*(1+n)));
    TANwTCT1=alphaw*xiw*((1-lambda)*R*TANwTB/(DA*(1+n))+TANwTH)+alphar*xir*lambda*R*TANwTB/(DA*(1+n));
    TANwTBF=TANwTB-(TANwTKT+TANwTKN);
    TANwTCT2=TANwTYT-TANwTBF+Rast*TANwTBF/(DA*(1+n))-(TANwTKN+TANwTKT)+(1-pardelta)*(TANwTKN+TANwTKT)/(DA*(1+n));
    R1=((RK+(1-pardelta))*(TANwTKN+TANwTKT)+Rast*TANwTBF)/TANwTB;
    result(1)=TANwTCT1-TANwTCT2;
    result(2)=R-R1;
end