% computes the steady state of dog.mod (growthless deterministic growth model). 
% stephane [DOT] adjemian [AT] ens [DOT] fr

function [ys,check] = dog_steadystate(ys,exe)
global M_

%% DO NOT CHANGE THIS PART.
%%
%% Here we load the values of the deep parameters in a loop.
%%
NumberOfParameters = M_.param_nbr;                            % Number of deep parameters.
for i = 1:NumberOfParameters                                  % Loop...
  paramname = deblank(M_.param_names(i,:));                   %    Get the name of parameter i. 
  eval([ paramname ' = M_.params(' int2str(i) ');']);         %    Get the value of parameter i.
end                                                           % End of the loop.  
check = 0;
%%
%% END OF THE FIRST MODEL INDEPENDENT BLOCK.


%% THIS BLOCK IS MODEL SPECIFIC.
%%
%% Here the user has to define the steady state.
%%

path(path, 'C:\Publication and research\Journal Articles\2014 - Human capital & inequality\analysis');
P = parameters; %always double check to confirm they are updated / in analysis2 folder

x0=[11.108; 0.56094; 0.0513694; 100.229; 15.4804; 17.6992; 8.63636; 72.8738; 0.070101]; 
options = optimset('Algorithm','trust-region-reflective','Display','on','MaxFunEvals',1000,'TolX',1e-9);

%this is where the benchmark file is saved
cd 'C:\Publication and research\Journal Articles\2014 - Human capital & inequality\analysis\matlab steady state'

%use homotopy to solve
M=100; lambdaset=linspace(.1,1,M);
for j=1:M
    lambda=lambdaset(j);
    [SSvals,fval,exitflag,output]=fsolve(@ss_bmark,x0,options,P.lambda,P.beta,P.alpha,P.chipp,P.chifw,P.xi,P.gamma,P.iota,P.psipp,P.psifw,P.pipp,P.eta,P.delta,P.tau_marg)
    x0=[SSvals(1); SSvals(2); SSvals(3); SSvals(4); SSvals(5); SSvals(6); SSvals(7); SSvals(8); SSvals(9)]; 
end

cstar=SSvals(1); lppstar=SSvals(2);
lfwstar=SSvals(3); hstar=SSvals(4);
ystar=SSvals(5); wppstar=SSvals(6);  
wfwstar=SSvals(7); kstar=SSvals(8);
rstar=SSvals(9); nstar=1-lfwstar-lppstar;

%set endogenous variables
c=cstar; lpp=lppstar; lfw=lfwstar;
h=hstar; y=ystar; wpp=wppstar; wfw=wfwstar;
k=kstar; r=rstar; n=nstar;

%shocks are 1 in SS
A_h=1; A_pp=1; z_h=0; z_pp=0;

%put back in dynare folder
cd 'C:\Publication and research\Journal Articles\2014 - Human capital & inequality\analysis\dynare\'

%%
%% END OF THE MODEL SPECIFIC BLOCK.


%% DO NOT CHANGE THIS PART.
%%
%% Here we define the steady state values of the endogenous variables of
%% the model.
%%
NumberOfEndogenousVariables = M_.endo_nbr;                    % Number of endogenous variables.
ys = zeros(NumberOfEndogenousVariables,1);                    % Initialization of ys (steady state).
for i = 1:NumberOfEndogenousVariables                         % Loop...
  varname = deblank(M_.endo_names(i,:));                      %    Get the name of endogenous variable i.                     
  eval(['ys(' int2str(i) ') = ' varname ';']);                %    Get the steady state value of this variable.
end                                                           % End of the loop.
%%
%% END OF THE SECOND MODEL INDEPENDENT BLOCK.