function [ys,check] = Thesis_nln_steadystate(ys,exo)
% function [ys,check] = NK_baseline_steadystate(ys,exo)
% computes the steady state for the NK_baseline.mod and uses a numerical
% solver to do so
% Inputs: 
%   - ys        [vector] vector of initial values for the steady state of
%                   the endogenous variables
%   - exo       [vector] vector of values for the exogenous variables
%
% Output: 
%   - ys        [vector] vector of steady state values fpr the the endogenous variables
%   - check     [scalar] set to 0 if steady state computation worked and to
%                    1 of not (allows to impos restriction on parameters)

global M_ 

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


%% Enter model equations here

options=optimset(); % set options for numerical solver

% 1. Steady state policy interest rate
r_ib   = betta^(-1)-1;

% Set parameter r_ib_ss
r_ib_bar = r_ib;

% 10-13. Interest rates
R_b    = r_ib;
r      = rstar+pbar;
r_b    = R_b+muy_b;

% Debt
d      = (r_ib-(rstar+pbar))/varrpsi;

% Set parameter d_ss
d_bar   = d;

% 4. FOC investment
q      = 1;

% exogenous processes
% 33. Technology Shock
a      = 1;
xia    = 0;
% 34. Foreign output Shock
ystar  = 1;
xiy    = 0;

%Set parameter ystar_ss
ystar_ss = ystar;

% 35. Monetary shock
u      = 1;
xiu    = 0;

% 32.Constant world price
pstar  = 1;

% 22. CPI inflation
pi_cpi = 1;

% 23-24. Domestic goods & imported goods inflation
pi_h   = 1;
pi_f   = 1;

% Normalize exchange rate
e = 1;

%8-9. Imported good optimal price, price index
p_fo   = (epsl/(epsl-1))*e*pstar;
p_f    = p_fo;

%6-7. Domestic good price index, optimal price, wholesale price
p_h    = p_f;
p_ho   = p_h;
pw     = p_ho*(epsl-1)/epsl;

% 21. CPI price 
p      = p_h;

% 19. Foreign demand for domestic goods
c_hstar = gam*(p_f/(e*pstar))^etta*ystar; 

% 20. Definition of terms of trade
s      = p_f/p_h;

% 25. Law of one price gap
lopg   = e*pstar/p_f;

% Nominal wage
w      = (r_b+delt_k)^(alppha/(alppha-1))*(1-alppha)/(alppha*(a*alppha*pw)^(1/(alppha-1)));

% {solve numerically for n, k, w, e, c}
[FVAL,fval,exitflag] = fsolve(@F,[0.45;4.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

c = FVAL(1);
k = FVAL(2);

% Labor
n      = k*(a*alppha*pw/(r_b+delt_k))^(1/(alppha-1));

% 26. Production function
y      = a*k^alppha*n^(1-alppha);

% 16. Local demand for domestic goods
c_h     = y-c_hstar;

% Local demand for imported goods
c_f     = gam*c;

% 27. Law of motion for capital of domestic producers
invest  = delt_k*k;

% 28. New investment financed by bank loans (credit market clearing) CHECK if necessary
b      = k;

% Leverage
k_b    = b*nuy;

% 29. Law of motion for capital of banks
PI_b   = k_b*delt_b;

% 30. Bank balance sheet
m      = b-d*e-k_b;



g1=p_h^epsl*pw*y/(1-betta*thetta_h*((c(+1))^(-siggma)/c^(-siggma)));

g2=p_h^epsl*y/(1-betta*thetta_h*((c(+1))^(-siggma)/c^(-siggma)));

x1=p_f^epsl*e*pstar*c_f/(1-betta*thetta_f*((c(+1))^(-siggma)/c^(-siggma)));

x2=p_f^epsl*c_f/(1-betta*thetta_f*((c(+1))^(-siggma)/c^(-siggma)));


%% end own model equations

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

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