function [ys,check] = ScotModelTest_steadystate(ys,exo)
% function [ys,check] = ScotModelTest_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 = M_.param_names(ii,:);
  eval([ paramname ' = M_.params(' int2str(ii) ');']);
end
% initialize indicator
check = 0;


%% Enter model equations here

x0=1.3*ones(1,22);
[Endoss,fval,exitflag] = fsolve(@SS_Solver,x0,optimset('MaxFunEvals',1e10, 'Display','iter', 'Maxiter', 20000),betta,chi0,chi1,b,ew,eta,alppha,ep,omega1,omega2,theta,mubar,tauk,tauc,taul,PercG,PercGdev,bbarUK,varpi,PercOilRec,PercOilRev,mcobar,pobar,dbarUK); 

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

Endoss=real(Endoss);

% 5.1 Home country variables 

%5.1.1 Endogenous variables


C=Endoss(1);   
Pi=1;     
Q=1;  
Rk=Endoss(17);  
Y=Endoss(11);  
I=Endoss(4);  
m=1;
K=Endoss(3);     
What=Endoss(5);     
W=What;      
AA=1;         
WL=What;    
N=Endoss(2);   
Z1=(N/W^(-ew))^(1+eta)*(1-thetaw*betta)^(-1);       
Z2=N*W^ew/(C-b*C)*(1-thetaw*betta)^(-1);                   
MC=Endoss(14);       
Pih=Pi;     
Ph=Endoss(7);      
Phhat=1;   
Lambda=1;      
PD=Phhat^(-ep);       
Yh=Endoss(8);      
Yhstar=Endoss(9)*varpi/(1-varpi); 
Yhstarstar=Endoss(19); 
F1=MC*(Yh+(1-varpi)/varpi*Yhstar+Yhstarstar)*(1-thetah*betta)^(-1);       
F2=Ph*(Yh+(1-varpi)/varpi*Yhstar+Yhstarstar)*(1-thetah*betta)^(-1);     
GDP =Yh+(1-varpi)/varpi*Yhstar+Yhstarstar;     
bb=bbarUK;       
d=dbarUK;             
Gdev=Endoss(15);  
DD=1;               
gg=0;                    
Tsc=Gdev+Endoss(6);             
G=Endoss(12);         


%5.1.2 Stochastic processes
Dc=1;         
Dn=1;
V=1;
A=1;
mu=mubar;
T=Endoss(6);



% 5.2 Foreign country variables 

%5.2.1 Endogenous variables

Cstar=C;     
Pistar=Pi;     
Qstar=Q;      
Rkstar=Rk;   
Ystar=Y;     
Istar=I;    
mstar=m;     
Kstar=K;   
Wstar=W;      
Whatstar=What;   
Z1star=Z1;   
Z2star=Z2;   
AAstar=1;      
Nstar=N;   
MCstar=MC;     
Pif=Pih;       
Pf=Ph;         
Pfhat=Phhat;   
Lambdastar=Lambda; 
F1star=F1;   
F2star=F2;     
PDstar=PD;      
Yfstar=omega1star/omega2star*Yhstar;          
Yf=Endoss(9);     
Yfstarstar=Yhstarstar;  
GDPstar=GDP;     
bbstar=bb;       
dstar=d;      
Gstar=G;


%5.2.2 Stochastic processes
Dcstar=1;         
Dnstar=1;
Vstar=1;
Astar=1;
Gdevstar=Endoss(15);
mustar=mubarstar;
Tstar=Endoss(6);

%5.3 Common variables 

%5.3.1 Endogenous variables

Rstar=Rbar;   
RER=1; 
TO=PercOilRec*Endoss(11);     
buk=bbarUK;  
RERstar=1/RER;
RERrow=Endoss(21);
RERrowstar=RERrow*RERstar;
Upsilon=0;
duk=varpi*d*RER*Y/Ystar+(1-varpi)*dstar;
TR=tauc*C+taul*W*N+tauk*Rk*m*K;
GDPuk=varpi*GDP*RERstar+(1-varpi)*GDPstar;
Cuk=varpi*C*RERstar+(1-varpi)*Cstar;       
Iuk=varpi*I*RERstar+(1-varpi)*Istar; 
Wuk=varpi*W*RERstar+(1-varpi)*Wstar; 
Tuk=varpi*T*RERstar+(1-varpi)*Tstar;      
Piuk=varpi*Pi+(1-varpi)*Pistar;     

%5.3.2 Stochastic processes

gx=1;
Gnodev=Endoss(13);        
po=pobar; 
mco=mcobar; 
Qo=Endoss(16);     
tauo=Endoss(22);

%5.4 Rest of the world

Pistarstar=1;
Rstarstar=1/betta;
Yrow=Endoss(10);
Yrowstar=Endoss(10);
Ystarstar=Endoss(11);
TB=Endoss(20);
TBstar=Endoss(20);

%%%%%Set VAR intercept to match SS ROW target (ensuring symmetrcy with UK)
Intercepts =[0.341968505534106 0.300000000000000 0.161616161616162];
VecCoeff=[ayy ayp ayr; apy app apr;ary arp arr];
VecEndo=[Ystarstar Pistarstar Rstarstar]';
VARSS=(eye(3,3)-VecCoeff)*VecEndo;
Intercepts=linsolve(eye(3,3),VARSS);
aay=Intercepts(1);
aap=Intercepts(2);
aar=Intercepts(3);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%5.5 Observational equations
UKinfl_obs=0; 
UKgdp_obs=0;
UKc_obs=0; 
UKi_obs=0;
UKgnodev_obs=0;															
UKtrans_obs=0;	            
UKw_obs=0; 
UKr_obs=0;
SCinfl_obs=0;  
SCgdp_obs=0;              
SCc_obs=0;                 
SCi_obs=0;
SCgdev_obs=0; 
SCtrans_obs=0; 
SCw_obs=0; 
OilPrice_obs=0;
OilProd_obs=0;           
OilCost_obs=0;            
ROWgdp_obs=0;
ROWinfl_obs=0;
ROWr_obs=0;

%% 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 = M_.endo_names(ii,:);
  eval(['ys(' int2str(ii) ') = ' varname ';']);
end
















 


