function [ys,check] = NK_baseline_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;

c1welf = c1welfss;
c3welf = c3welfss;
c2welf = c2welfss;
c1lambdaq = log(c1lambdaqss);
c3lambdaq = log(c3lambdaqss);
c1q = log(c1qss);
c3q = log(c3qss);
c1i = log(c1iss);
c3i = log(c3iss);
c1k = log(c1kss);
c3k = log(c3kss);
c1c = log(c1css);
c3c = log(c3css);
c1cne = log(c1cness);
c3cne = log(c3cness);
c1cd = log(c1cdss);
c3cd = log(c3cdss);
c1mc = log(c1mcss);
c3mc = log(c3mcss);
c1oc = log(c1ocss);
c3oc = log(c3ocss);
c1id = log(c1idss);
c3id = log(c3idss);
c1rpipd = log(c1rpipdss);
c3rpipd = log(c3rpipdss);
c1mi = log(c1miss);
c3mi = log(c3miss);
c1v = log(c1vss);
c3v = log(c3vss);
c1y = log(c1yss);
c3y = log(c3yss);
c1rk = log(c1rkss);
c3rk = log(c3rkss);
c1l = log(c1lss);
c3l = log(c3lss);
c1o = log(c1oss);
c3o = log(c3oss);
c1rrs = c1rrsss;
c3rrs = c3rrsss;
c1m = log(c1mss);
c3m = log(c3mss);
c1tbal = c1tbalss;
c3tbal = c3tbalss;
c1gtbal = c1gtbalss;
c3gtbal = c3gtbalss;
c1g = log(c1gss);
c3g = log(c3gss);
c1shockgy = c1shockgyss;
c3shockgy = c3shockgyss;
c1op = log(c1opss);
c3op = log(c3opss);
c1os = log(c1osss);
c3os = log(c3osss);
c1ow = log(c1owss);
c3ow = log(c3owss);
c1rpopd = log(c1rpopdss);
c3rpopd = log(c3rpopdss);
c1oi = log(c1oiss);
c3oi = log(c3oiss);
c1zc = c1zcss;
c3zc = c3zcss;
c1zclag = c1zclagss;
c3zclag = c3zclagss;
c1zi = c1ziss;
c3zi = c3ziss;
c1yo = c1yoss;
c3yo = c3yoss;
c1lagyo = c1lagyoss;
c3lagyo = c3lagyoss;
c1muoc = c1muocss;
c3muoc = c3muocss;
c1lagmuoc = c1lagmuocss;
c3lagmuoc = c3lagmuocss;
c1muc = c1mucss;
c3muc = c3mucss;
c1omegacm = c1omegacmss;
c3omegacm = c3omegacmss;
c1lagomegacm = c1lagomegacmss;
c3lagomegacm = c3lagomegacmss;
c1omegaim = c1omegaimss;
c3omegaim = c3omegaimss;
c2lambdaq = log(c2lambdaqss);
c2q = log(c2qss);
c2i = log(c2iss);
c2k = log(c2kss);
c2c = log(c2css);
c2cne = log(c2cness);
c2cd = log(c2cdss);
c2mc = log(c2mcss);
c2oc = log(c2ocss);
c2id = log(c2idss);
c2rpipd = log(c2rpipdss);
c2mi = log(c2miss);
c2v = log(c2vss);
c2y = log(c2yss);
c2rk = log(c2rkss);
c2l = log(c2lss);
c2o = log(c2oss);
c2rrs = c2rrsss;
c2m = log(c2mss);
c2tbal = c2tbalss;
c2gtbal = c2gtbalss;
c2g = log(c2gss);
c2shockgy = c2shockgyss;
c2op = log(c2opss);
c2os = log(c2osss);
c2ow = log(c2owss);
c2rpopd = log(c2rpopdss);
c2oi = log(c2oiss);
c2zc = c2zcss;
c2zclag = c2zclagss;
c2zi = c2ziss;
c2yo = c2yoss;
c2lagyo = c2lagyoss;
c2muoc = c2muocss;
c2lagmuoc = c2lagmuocss;
c2muc = c2mucss;
c2omegacm = c2omegacmss;
c2lagomegacm = c2lagomegacmss;
c2omegaim = c2omegaimss;
c1rer = log(c1rerss);
c3rer = log(c3rerss);
c2rpwopd = log(c2rpwopdss);
c1rpwopd = log(c1rpwopdss);
c3rpwopd = log(c3rpwopdss);
c1b = c1bss;
c3b = c3bss;
c1rpmcpd = log(c1rpmcpdss);
c3rpmcpd = log(c3rpmcpdss);
c2rpmcpd = log(c2rpmcpdss);
c1z = c1zss;
c3z = c3zss;
c2z = c2zss;
c1zw = c1zwss;
c3zw = c3zwss;
c1zwlag = c1zwlagss;
c3zwlag = c3zwlagss;
c1muo = c1muoss;
c3muo = c3muoss;
c2muo = c2muoss;
c1muow = c1muowss;
c3muow = c3muowss;
c1muowlag = c1muowlagss;
c3muowlag = c3muowlagss;
c1rpmipd = log(c1rpmipdss);
c3rpmipd = log(c3rpmipdss);
c1x = log(c1xss);
c3x = log(c3xss);
c2rpmipd = log(c2rpmipdss);
c2x = log(c2xss);
c2w = log(c2wss);
c1w = log(c1wss);
c3w = log(c3wss);
c2oy = log(c2oyss);
c1oy = log(c1oyss);
c3oy = log(c3oyss);
c2thetapvar = c2thetapvarss;
c1thetapvar = c1thetapvarss;
c3thetapvar = c3thetapvarss;
c2wdes = log(c2wdesss);
c1wdes = log(c1wdesss);
c3wdes = log(c3wdesss);
c2thetawvar = c2thetawvarss;
c1thetawvar = c1thetawvarss;
c3thetawvar = c3thetawvarss;
c1lambdaq_pot = log(c1lambdaq_potss);
c3lambdaq_pot = log(c3lambdaq_potss);
c1q_pot = log(c1q_potss);
c3q_pot = log(c3q_potss);
c1i_pot = log(c1i_potss);
c3i_pot = log(c3i_potss);
c1k_pot = log(c1k_potss);
c3k_pot = log(c3k_potss);
c1c_pot = log(c1c_potss);
c3c_pot = log(c3c_potss);
c1cne_pot = log(c1cne_potss);
c3cne_pot = log(c3cne_potss);
c1cd_pot = log(c1cd_potss);
c3cd_pot = log(c3cd_potss);
c1mc_pot = log(c1mc_potss);
c3mc_pot = log(c3mc_potss);
c1oc_pot = log(c1oc_potss);
c3oc_pot = log(c3oc_potss);
c1id_pot = log(c1id_potss);
c3id_pot = log(c3id_potss);
c1rpipd_pot = log(c1rpipd_potss);
c3rpipd_pot = log(c3rpipd_potss);
c1mi_pot = log(c1mi_potss);
c3mi_pot = log(c3mi_potss);
c1v_pot = log(c1v_potss);
c3v_pot = log(c3v_potss);
c1y_pot = log(c1y_potss);
c3y_pot = log(c3y_potss);
c1rk_pot = log(c1rk_potss);
c3rk_pot = log(c3rk_potss);
c1l_pot = log(c1l_potss);
c3l_pot = log(c3l_potss);
c1o_pot = log(c1o_potss);
c3o_pot = log(c3o_potss);
c1rrs_pot = c1rrs_potss;
c3rrs_pot = c3rrs_potss;
c1m_pot = log(c1m_potss);
c3m_pot = log(c3m_potss);
c1tbal_pot = c1tbal_potss;
c3tbal_pot = c3tbal_potss;
c1g_pot = log(c1g_potss);
c3g_pot = log(c3g_potss);
c1op_pot = log(c1op_potss);
c3op_pot = log(c3op_potss);
c1os_pot = log(c1os_potss);
c3os_pot = log(c3os_potss);
c1ow_pot = log(c1ow_potss);
c3ow_pot = log(c3ow_potss);
c1rpopd_pot = log(c1rpopd_potss);
c3rpopd_pot = log(c3rpopd_potss);
c1oi_pot = log(c1oi_potss);
c3oi_pot = log(c3oi_potss);
c2lambdaq_pot = log(c2lambdaq_potss);
c2q_pot = log(c2q_potss);
c2i_pot = log(c2i_potss);
c2k_pot = log(c2k_potss);
c2c_pot = log(c2c_potss);
c2cne_pot = log(c2cne_potss);
c2cd_pot = log(c2cd_potss);
c2mc_pot = log(c2mc_potss);
c2oc_pot = log(c2oc_potss);
c2id_pot = log(c2id_potss);
c2rpipd_pot = log(c2rpipd_potss);
c2mi_pot = log(c2mi_potss);
c2v_pot = log(c2v_potss);
c2y_pot = log(c2y_potss);
c2rk_pot = log(c2rk_potss);
c2l_pot = log(c2l_potss);
c2o_pot = log(c2o_potss);
c2rrs_pot = c2rrs_potss;
c2m_pot = log(c2m_potss);
c2tbal_pot = c2tbal_potss;
c2g_pot = log(c2g_potss);
c2op_pot = log(c2op_potss);
c2os_pot = log(c2os_potss);
c2ow_pot = log(c2ow_potss);
c2rpopd_pot = log(c2rpopd_potss);
c2oi_pot = log(c2oi_potss);
c1rer_pot = log(c1rer_potss);
c3rer_pot = log(c3rer_potss);
c2rpwopd_pot = log(c2rpwopd_potss);
c1rpwopd_pot = log(c1rpwopd_potss);
c3rpwopd_pot = log(c3rpwopd_potss);
c1b_pot = c1b_potss;
c3b_pot = c3b_potss;
c1rpmcpd_pot = log(c1rpmcpd_potss);
c3rpmcpd_pot = log(c3rpmcpd_potss);
c2rpmcpd_pot = log(c2rpmcpd_potss);
c1rpmipd_pot = log(c1rpmipd_potss);
c3rpmipd_pot = log(c3rpmipd_potss);
c1x_pot = log(c1x_potss);
c3x_pot = log(c3x_potss);
c2rpmipd_pot = log(c2rpmipd_potss);
c2x_pot = log(c2x_potss);
c2w_pot = log(c2w_potss);
c1w_pot = log(c1w_potss);
c3w_pot = log(c3w_potss);
c2oy_pot = log(c2oy_potss);
c1oy_pot = log(c1oy_potss);
c3oy_pot = log(c3oy_potss);
c2rmcpd_pot = log(c2rmcpd_potss);
c1rmcpd_pot = log(c1rmcpd_potss);
c3rmcpd_pot = log(c3rmcpd_potss);
c2wdes_pot = log(c2wdes_potss);
c1wdes_pot = log(c1wdes_potss);
c3wdes_pot = log(c3wdes_potss);
c2rpcpd_pot = log(c2rpcpd_potss);
c1rpcpd_pot = log(c1rpcpd_potss);
c3rpcpd_pot = log(c3rpcpd_potss);
c2rmcpd = log(c2rmcpdss);
c1rmcpd = log(c1rmcpdss);
c3rmcpd = log(c3rmcpdss);
c2rpcpd = log(c2rpcpdss);
c1rpcpd = log(c1rpcpdss);
c3rpcpd = log(c3rpcpdss);
c2dpd = c2dpdss;
c1dpd = c1dpdss;
c3dpd = c3dpdss;
c2dcore = log(c2pistar);
c1dcore = log(c1pistar);
c3dcore = log(c3pistar);
c1dhead = log(c1pistar);
c3dhead = log(c3pistar);
c1dpwo = log(c1grmuzoss)+log(c1pistar);
c3dpwo = log(c3grmuzoss)+log(c3pistar);
c2rpnepd = log(c2rmcpdss);
c1rpnepd = log(c1rmcpdss);
c3rpnepd = log(c3rmcpdss);
c1rs = c1rsss;
c3rs = c3rsss;
c2rs = c2rsss;
c1pitar = c1pitarss;
c3pitar = c3pitarss;
c2pitar = c2pitarss;
c1hw = log((1+c1thetaw)/c1thetaw*c1lss/(1-c1xiw*c1beta));
c3hw = log((1+c3thetaw)/c3thetaw*c3lss/(1-c3xiw*c3beta));
c1gw = log((1+c1tauw)/c1thetaw*c1lss/(1-c1xiw*c1beta));
c3gw = log((1+c3tauw)/c3thetaw*c3lss/(1-c3xiw*c3beta));
c1wstar = log(c1wss);
c3wstar = log(c3wss);
c1omegabar = c1pistar*c1grmuzss;
c3omegabar = c3pistar*c3grmuzss;
c1omega = c1pistar*c1grmuzss;
c3omega = c3pistar*c3grmuzss;
c2hw = log((1+c2thetaw)/c2thetaw*c2lss/(1-c2xiw*c2beta));
c2gw = log((1+c2tauw)/c2thetaw*c2lss/(1-c2xiw*c2beta));
c2wstar = log(c2wss);
c2omegabar = c2pistar*c2grmuzss;
c2omega = c2pistar*c2grmuzss;
c1hp = log(((1+c1thetap)/c1thetap*(c1rmcpdss)*(c1yss)/(1-c1xip*c1beta)));
c3hp = log(((1+c3thetap)/c3thetap*(c3rmcpdss)*(c3yss)/(1-c3xip*c3beta)));
c1gp = log(((1+c1taup)/c1thetap*(c1yss)/(1-c1xip*c1beta)));
c3gp = log(((1+c3taup)/c3thetap*(c3yss)/(1-c3xip*c3beta)));
c1pid = c1pistar;
c3pid = c3pistar;
c1pstar = log(1);
c3pstar = log(1);
c1pidbar = c1pistar;
c3pidbar = c3pistar;
c2hp = log(((1+c2thetap)/c2thetap*(c2rmcpdss)*(c2yss)/(1-c2xip*c2beta)));
c2gp = log(((1+c2taup)/c2thetap*(c2yss)/(1-c2xip*c2beta)));
c2pid = c2pistar;
c2pstar = log(1);
c2pidbar = c2pistar;
c1yd = log(c1yss);
c3yd = log(c3yss);
c2yd = log(c2yss);
c1yd_pot = log(c1yss);
c3yd_pot = log(c3yss);
c2yd_pot = log(c2yss);
c1deltad = 0;
c3deltad = 0;
c2deltad = 0;
c1ygap = 1;
c3ygap = 1;

%c1rNgdpPdY = c1sharengdpny;

%c1rpopgdp = c1rpopgdpss;

%c1gdp  = log(c1gdpss);
%c2gdp = c2gdpss; 

%log_oil_price =  c1grmuzoss + ( c1grmuzoss * c1mugdpss - c1grmuzss ) / c1grmuzss  ;

dpcexfe_obs = log(c1pistar) + ( c1pstar  - 1);          
dcomp_obs = log(c1wdesss);
fedfunds_obs = c1rrsss;
rer_log_obs = log(c1rerss);
log_gdp_us_obs = 1; 
fixi_share_us_obs =  c1rpcpd * c1i / c1y  * (1 / ( 1 - ( c1rpopd * c1oy / c1y ) + ( c1rpopd   * c1yo / c1y )) );
pce_share_us_obs = c1rpcpd * c1c / c1y  * (1 / ( 1 - ( c1rpopd * c1oy / c1y ) + ( c1rpopd   * c1yo / c1y ))) ;
%c1rpcpdss * c1css / c1yss  * (1 /  ( 1 - ( 1 - ( c1rpopdss * c1oyss / c1yss ) + ( c1rpopdss   * c1yoss / c1yss )) ) ) ;

exp_share_us_obs = c1x / c1y  * (1 / ( 1 - ( c1rpopd * c1oy / c1y ) + ( c1rpopd   * c1yo / c1y )) );
imp_share_us_obs = c1m / c1y  * (1 / ( 1 - ( c1rpopd * c1oy / c1y ) + ( c1rpopd   * c1yo / c1y )) ); 

oilimp_share_us_obs = (1 / ( 1 - c1rpopd * c1oy / c1y + c1rpopd * c1yo/c1y) ) * c1rpopd  * ( c1o / c1y - c1yo / c1y ) ;

oilprod_us_deltalog_obs = c1grmuoss  - 1;

oilprod_for_deltalog_obs = c2grmuoss  - 1;

delta_logoilprice_obs = ( c1grmuzoss * c1mugdpss  -  c1grmuzss ) / c1grmuzss; 
 
ys =[c1welf
c3welf    
c2welf
c1ygap
c3ygap 
c1lambdaq
c3lambdaq 
c1q
c3q
c1i
c3i
c1k
c3k
c1c
c3c
c1cne
c3cne
c1cd
c3cd
c1mc
c3mc
c1oc
c3oc
c1id
c3id
c1rpipd
c3rpipd
c1mi
c3mi
c1v
c3v
c1y
c3y
c1rk
c3rk
c1l
c3l
c1o
c3o
c1rrs
c3rrs
c1m
c3m
c1tbal
c3tbal
c1gtbal
c3gtbal
c1g
c3g
c1shockgy
c3shockgy
c1op
c3op
c1os
c3os
c1ow
c3ow
c1rpopd
c3rpopd
c1oi
c3oi
c1zc
c3zc
c1zclag
c3zclag
c1zi
c3zi
c1yo
c3yo
c1lagyo
c3lagyo
c1muoc
c3muoc
c1lagmuoc
c3lagmuoc
c1muc
c3muc
c1omegacm
c3omegacm
c1lagomegacm
c3lagomegacm
c1omegaim
c3omegaim
c2lambdaq
c2q
c2i
c2k
c2c
c2cne
c2cd
c2mc
c2oc
c2id
c2rpipd
c2mi
c2v
c2y
c2rk
c2l
c2o
c2rrs
c2m
c2tbal
c2gtbal
c2g
c2shockgy
c2op
c2os
c2ow
c2rpopd
c2oi
c2zc
c2zclag
c2zi
c2yo
c2lagyo
c2muoc
c2lagmuoc
c2muc
c2omegacm
c2lagomegacm
c2omegaim
c1rer
c3rer
c2rpwopd
c1rpwopd
c3rpwopd
c1b
c3b
c1rpmcpd
c3rpmcpd
c2rpmcpd
c1z
c3z
c2z
c1zw
c3zw
c1zwlag
c3zwlag
c1muo
c3muo
c2muo
c1muow
c3muow
c1muowlag
c3muowlag
c1rpmipd
c3rpmipd
c1x
c3x
c2rpmipd
c2x
c2w
c1w
c3w
c2oy
c1oy
c3oy
c2thetapvar
c1thetapvar
c3thetapvar
c2wdes
c1wdes
c3wdes
c2thetawvar
c1thetawvar
c3thetawvar
c1lambdaq_pot
c3lambdaq_pot
c1q_pot
c3q_pot
c1i_pot
c3i_pot
c1k_pot
c3k_pot
c1c_pot
c3c_pot
c1cne_pot
c3cne_pot
c1cd_pot
c3cd_pot
c1mc_pot
c3mc_pot
c1oc_pot
c3oc_pot
c1id_pot
c3id_pot
c1rpipd_pot
c3rpipd_pot
c1mi_pot
c3mi_pot
c1v_pot
c3v_pot
c1y_pot
c3y_pot
c1rk_pot
c3rk_pot
c1l_pot
c3l_pot
c1o_pot
c3o_pot
c1rrs_pot
c3rrs_pot
c1m_pot
c3m_pot
c1tbal_pot
c3tbal_pot
c1g_pot
c3g_pot
c1op_pot
c3op_pot
c1os_pot
c3os_pot
c1ow_pot
c3ow_pot
c1rpopd_pot
c3rpopd_pot
c1oi_pot
c3oi_pot
c2lambdaq_pot
c2q_pot
c2i_pot
c2k_pot
c2c_pot
c2cne_pot
c2cd_pot
c2mc_pot
c2oc_pot
c2id_pot
c2rpipd_pot
c2mi_pot
c2v_pot
c2y_pot
c2rk_pot
c2l_pot
c2o_pot
c2rrs_pot
c2m_pot
c2tbal_pot
c2g_pot
c2op_pot
c2os_pot
c2ow_pot
c2rpopd_pot
c2oi_pot
c1rer_pot
c3rer_pot
c2rpwopd_pot
c1rpwopd_pot
c3rpwopd_pot
c1b_pot
c3b_pot
c1rpmcpd_pot
c3rpmcpd_pot
c2rpmcpd_pot
c1rpmipd_pot
c3rpmipd_pot
c1x_pot
c3x_pot
c2rpmipd_pot
c2x_pot
c2w_pot
c1w_pot
c3w_pot
c2oy_pot
c1oy_pot
c3oy_pot
c2rmcpd_pot
c1rmcpd_pot
c3rmcpd_pot
c2wdes_pot
c1wdes_pot
c3wdes_pot
c2rpcpd_pot
c1rpcpd_pot
c3rpcpd_pot
c2rmcpd
c1rmcpd
c3rmcpd
c2rpcpd
c1rpcpd
c3rpcpd
c2dpd
c1dpd
c3dpd
c2dcore
c1dcore
c3dcore
c1dhead
c3dhead
c1dpwo
c3dpwo
c3dhead
c2rpnepd
c1rpnepd
c3rpnepd
c3dhead
c1rs
c3rs
c2rs
c1pitar
c3pitar
c2pitar
c1hw
c3hw
c1gw
c3gw
c1wstar
c3wstar
c1omegabar
c3omegabar
c1omega
c3omega
c2hw
c2gw
c2wstar
c2omegabar
c2omega
c1hp
c3hp
c1gp
c3gp
c1pstar
c3pstar
c1pidbar
c3pidbar
c1pid
c3pid
c1deltad
c3deltad
c2hp
c2gp
c2pstar
c2pidbar
c2pid
c2deltad
c1yd
c3yd
c2yd
c1yd_pot
c3yd_pot
c2yd_pot
dpcexfe_obs
dcomp_obs
fedfunds_obs
rer_log_obs
fixi_share_us_obs
log_gdp_us_obs
pce_share_us_obs
exp_share_us_obs
imp_share_us_obs
oilimp_share_us_obs
oilprod_us_deltalog_obs
oilprod_for_deltalog_obs
delta_logoilprice_obs
];


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

