function F = ss_fun_BGG3(x)

%% Load model parameters
global M_
% 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.
%   if strcmp(paramname,'xx')==0
%   disp([ paramname ' = M_.params(' int2str(i) ');'])         %    Get the value of parameter i.
%   end
% end        

gy = M_.params(1);
alp = M_.params(2);
delta = M_.params(3);
zzetap = M_.params(4);
zzetaw = M_.params(5);
sigma_c = M_.params(6);
chi = M_.params(7);
phiX = M_.params(8);
xip = M_.params(9);
xiw = M_.params(10);
gammap = M_.params(11);
gammaw = M_.params(12);
PIE_bar = M_.params(13);
g_bar = M_.params(14);
Rn_bar = M_.params(15);
rho_r = M_.params(16);
theta_pie = M_.params(17);
theta_y = M_.params(18);
theta_dy = M_.params(19);
rhoA = M_.params(20);
rhoG = M_.params(21);
rhoMCS = M_.params(22);
rhoMRSS = M_.params(23);
rhoMPS = M_.params(24);
rhoRPS = M_.params(25);
rhoIS = M_.params(26);
ptarget = M_.params(27);
rhotarget = M_.params(28);
phitarget = M_.params(29);
CEtarget = M_.params(30);
sigma_s = M_.params(31);
wr = M_.params(32);
apsi = M_.params(33);

H        = exp(x(1));
KYW      = exp(x(2));
% sigmaE   = exp(x(3))/(1+exp(x(3)));
% sigmapsi = exp(x(4));
% psiFF    = exp(x(5));
% muFF     = exp(x(6))/(1+exp(x(6))); 
% xiE      = exp(x(7))/(1+exp(x(7)));
sigmapsi = exp(x(3));
psiFF    = exp(x(4));
muFF     = exp(x(5))/(1+exp(x(5))); 
xiE      = exp(x(6))/(1+exp(x(6)));
sigmaE   = 0.99;

g       = g_bar/100;
betta   = (PIE_bar/100+1)/(Rn_bar/100+1)/(1+g)^(-sigma_c); % (273)

A       = 1;
MCS     = 1;
MRSS    = 1;
IS      = 1;
MPS     = 1;
RPS     = 1;

PIE     = PIE_bar/100+1;

S       = 0; % (462)
Sdash   = 0; % (463)
PIEw    = (1+g)*PIE; % (464)
Q       = 1; % (465)
LAMBDA  = betta*(1+g)^(-sigma_c); % (466)
PO      = ((1-xip*PIE^((gammap-1)*(1-zzetap)))/(1-xip))^(1/(1-zzetap)); % (467)
Deltap  = (1-xip)/(1-xip*PIE^(zzetap*(1-gammap)))*PO^(-zzetap); % (468)
MC      = (1-1/zzetap)*1/MCS*(1-xip*(1+g)*LAMBDA*PIE^(zzetap*(1-gammap)))/(1-xip*(1+g)*LAMBDA*PIE^((zzetap-1)*(1-gammap)))*PO; % (469)
PWP     = MC; % (470)
WO      = ((1-xiw*PIE^(gammaw*(1-zzetaw))*PIEw^(zzetaw-1))/(1-xiw))^(1/(1-zzetaw)); % (471)
Deltaw  = (1-xiw)/(1-xiw*PIEw^(zzetaw)/PIE^(gammaw*zzetaw))*WO^(-zzetaw); % (472)
Hd      = H/Deltaw; % (473)
rK      = (1-alp)*(1+g)/KYW*PWP; % (474)
YW      = A*Hd*(KYW/(1+g))^((1-alp)/alp); % (475)
K       = YW*KYW; % (476)
Y       = YW/Deltap; % (477)
I       = K/IS*(g+delta)/(1+g); % (478)
G       = gy*Y; % (479)
RK      = rK+(1-delta); % (480)
JJw     = Hd/(1-xiw*LAMBDA*PIEw^zzetaw*PIE^(gammaw*(1-zzetaw)-1)); % (481)
W       = alp*PWP*YW/Hd; % (482)
Jw      = WO*W*JJw; % (483)
Wh      = (1-xiw*(1+g)*LAMBDA*PIEw^zzetaw/PIE^(gammaw*zzetaw))*(1-1/zzetaw)*Jw/(Hd*MRSS); % (484)
mupsi          = -0.5*(sigmapsi)^2;
p              = logncdf(psiFF,mupsi,sigmapsi);
Gofpsi         = normcdf(log(psiFF)/sigmapsi-sigmapsi/2);
Gammaofpsi     = Gofpsi+psiFF*(1-p);
Gammadashofpsi = 1-p;
Gdashofpsi     = 1/(psiFF*sigmapsi*sqrt(2*(4*atan(1))))*exp(-(log(psiFF)+mupsi)^2/(2*(sigmapsi)^2));
CE        = (1-sigmaE)*(1-xiE)*(1-Gammaofpsi)*RK*Q*K/(1+g); % (485)
rhoofpsi  = Gammadashofpsi/((Gammaofpsi-muFF*Gofpsi)*Gammadashofpsi+(1-Gammaofpsi)*(Gammadashofpsi-muFF*Gdashofpsi)); % (486)
C         = Y-G-I-CE-muFF*Gofpsi*RK*Q*K/(1+g); % (487)
NE        = (sigmaE+xiE)*(1-Gammaofpsi)*RK*Q*K/(1+g); % (488)
L         = Q*K-NE; % (489)
Rl        = psiFF*RK*Q*K*PIE/L; % (490)
R         = 1/(LAMBDA); % (392)
U       = (C*(1-chi/(1+g)))^(1-sigma_c)/(1-sigma_c)*exp((sigma_c-1)*H^(1+apsi)/(1+apsi)); % (494)
lambda  = (1-sigma_c)*U/(C-chi*C/(1+g))-betta*chi*(1-sigma_c)*U/(1+g)^(1-sigma_c)/((1+g)*C-chi*C); % (497)
UH      = -(1-sigma_c)*U*H^apsi;

% F       = [Wh+UH/lambda; RK-rhoofpsi*R; R*(Q*K-NE)-RK*Q*K*(Gammaofpsi-muFF*Gofpsi); Q*K/NE-phitarget; p-ptarget; rhoofpsi-rhotarget; CE/Y-CEtarget];
F       = [Wh+UH/lambda; RK-rhoofpsi*R; R*(Q*K-NE)-RK*Q*K*(Gammaofpsi-muFF*Gofpsi); p-ptarget; rhoofpsi-rhotarget; CE/Y-CEtarget];