function[ys,check] = FO_OE_ss_steadystate(ys,exo)

% computes the steady state for the FO_OE_ss.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 for the the endogenous variables
%   - check     [scalar] set to 0 if steady state computation worked and to
%                    1 of not (allows to impose 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 - calls the function FO_d.m and uses fsolve
%%% to find the ss values of dt, d and star


% the steady state computation

x0=[2.5, 5.5, 5.5];
options = optimset('TolFun',1e-8,'TolX',1e-8,'MaxIter', 5000, 'MaxFunEvals', 5000, 'Display', 'Off'); % set options for numerical solver
[x,fval] =fsolve(@(x)FO_d(x, alfa, gama, csi, phi, mu),x0,options);

dt=x(1);
d=x(2);
dstar=x(3);

rd = gama*dt;                                              %% functional form returns on deposits
ri = 1/alfa-((phi/2)*dt);                          %% implicit function returns on firms' projects
rl = 1/alfa - phi*dt;                                        %% functional form returns on loans
pr =1- alfa*ri;                               %% functional form probability

%set the limitation condition for pr 
 if pr>=1 || pr <0  % parameter violates restriction; Preventing this cannot be implemented via prior restriction as it is a composite of different parameters and the valid prior region has unknown form 
 check=1; %set failure indicator 
  return; %return without updating steady states 
 end 

pai = pr*((1+rl)-(1+rd)-csi)*d;                              %% current banks' profits  domestic destination market
paistar = pr*((1+rl)-(1+rd)-csi-mu)*dstar;                       %% current banks' profits  foreign destination market


ne=1;                                   %%%% SET initial value ne=1
n = ne*(1-pho)/pho;
nstar =(dt*(1-pho)- n*d)/dstar;              
nestar = nstar*pho/(1-pho);
nt = n + nstar;                     %% aggregate banks
net = ne + nestar;               %% aggregate new banks




%%%%% end model %%%%%
%%% update parameters 
 
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


    
     

