function R = res_iscmem1(Z,param)
%RES_ISCMEM1   Residuals for the international sticky price model with 
%              exogenous growth rates for money and N=1.

%              Ellen R. McGrattan, 10-11-96
%              Revised, ERM, 3-26-00

%______________________________________________________________________________
%
% PARAMETERS

ns         = 50;
beta       = param(11);
ome1       = param(12);
rho        = param(13);
theta      = param(14);
alpha      = param(15);
delta      = param(16);
b          = param(17);
mus        = param(19);
phs        = param(ns);
ys         = param(ns+1);
cs         = param(ns+2);
MPs        = param(ns+3);
UC         = param(ns+4);
UM         = param(ns+5);
UL         = param(ns+6);
UCC        = param(ns+7);
UMM        = param(ns+8);
ULL        = param(ns+9);
UCM        = param(ns+10);
UCL        = param(ns+11);
ULM        = param(ns+12);
ks         = param(ns+13);
xs         = param(ns+14);
yhs        = param(ns+15);
yhas       = param(ns+16);
ls         = param(ns+17);
ws         = -UL/UC;
ps         = mus/MPs;

alph1      = 1-alpha;
delt1      = 1-delta;
ome2       = 1-ome1;
rho1       = 1-rho;
thet1      = 1-theta;

%______________________________________________________________________________
%
% VARIABLES APPEARING IN THE RESIDUALS
%

nX         = 6;
z          = zeros(8,2);
z(:)       = Z(1:16);
z          = z(:,[2,1]);

mu         = Z([24,23]) - log(mus);
mua        = Z([26,25]) - log(mus);
           % mu  = [mu_t,mu_{t+1}]' 

ph         = z(1,:)' - log(phs);
pf         = z(2,:)' - log(phs);
pfa        = z(3,:)' - log(phs);
pha        = z(4,:)' - log(phs);
           % ph  = [ph_{t-1},ph_t]'

k          = [Z(21);z(5,:)']  - log(ks);
ka         = [Z(22);z(6,:)'] - log(ks);
           % k  = [k_{t-1}, k_t, k_{t+1}

y          = z(7,:)' - log(ys);
ya         = z(8,:)' - log(ys);
           % y   = [y_t,y_{t+1}]'

x          = (k(2:3)-delt1*k(1:2))/delta;
xa         = (ka(2:3)-delt1*ka(1:2))/delta;
           % x  = [x_t,x_{t+1}]'

c          = (ys*y-xs*x)/cs;
ca         = (ys*ya-xs*xa)/cs;
           % c  = [c_t,c_{t+1}]'

p          = (ps/phs)^(rho/rho1)*(ome1^(1/rho1)*ph+ome2^(1/rho1)*pf);
pa         = (ps/phs)^(rho/rho1)*(ome1^(1/rho1)*pfa+ome2^(1/rho1)*pha);
           % p  = [p_t,p_{t+1}]'

yh         = y+(p-ph)/rho1;
yf         = y+(p-pf)/rho1;
yfa        = ya+(pa-pfa)/rho1;
yha        = ya+(pa-pha)/rho1;
           % yh = [yh_t,yh_{t+1}]'

l          = (yh *yhs + yha *yhas)./(alph1*(yhs+yhas))-alpha*k(1:2)/alph1;
la         = (yfa*yhs + yf  *yhas)./(alph1*(yhs+yhas))-alpha*ka(1:2)/alph1;
           % l  = [l_t,l_{t+1}]'

m          = mu-p;
ma         = mua-pa;
           % m  = [m_t-p_t,m_{t+1}-p_{t+1}]'

w          = (UCL/UL-UCC/UC)*cs*c+(ULL/UL-UCL/UC)*ls*l+  ...
             (ULM/UL-UCM/UC)*MPs*m;
wa         = (UCL/UL-UCC/UC)*cs*ca+(ULL/UL-UCL/UC)*ls*la+ ...
             (ULM/UL-UCM/UC)*MPs*ma;
           % w  = [w_t,w_{t+1}]'

mc         = w+alpha*(l-k(1:2));
mca        = wa+alpha*(la-ka(1:2));
           % mc = [mc_t,mc_{t+1}]'

q          = -UCC*cs/UC*(c-ca)-UCL*ls/UC*(l-la)-UCM*MPs/UC*(m-ma);
           % q  = [q_t,q_{t+1}]'

%______________________________________________________________________________
%
% LINEARIZED RESIDUALS:
%
% 1. PRICING EQUATIONS
%

R(1,1)       = ph(1) -(p(1) +mc(1));
R(2,1)       = pf(1) -(p(1) +mca(1)+q(1));
R(3,1)       = pfa(1)-(pa(1)+mca(1));
R(4,1)       = pha(1)-(pa(1)+mc(1) -q(1));

%
% 2. MONOPOLISTS' EULER EQUATIONS FOR CAPITAL
%

R(5,1)       =-(UCC*cs /UC*(c(2)-c(1))+ ...
                UCL*ls /UC*(l(2)-l(1))+ ...
                UCM*MPs/UC*(m(2)-m(1)))- ...
                 (1-beta*delt1) * (w(2)+l(2)-k(2))+ ...
                 b*delta * (x(1)-k(1))- ...
                 beta*b*delta * (x(2)-k(2)) ;

R(6,1)       =-(UCC*cs /UC*(ca(2)-ca(1))+ ...
                UCL*ls /UC*(la(2)-la(1))+ ...
                UCM*MPs/UC*(ma(2)-ma(1)))- ...
                 (1-beta*delt1) * (wa(2)+la(2)-ka(2))+ ...
                 b*delta * (xa(1)-ka(1))- ...
                 beta*b*delta * (xa(2)-ka(2)) ;


%
% 3. MONEY DEMAND EQUATIONS
%

R(7,1)       = UM/UC*((UCM/UM-UCC/UC)*cs *c(1)+ ...
                      (ULM/UM-UCL/UC)*ls *l(1)+ ...
                      (UMM/UM-UCM/UC)*MPs*m(1))+ ...
             beta/mus*(UCC*cs /UC*(c(2)-c(1))+ ...
                       UCL*ls /UC*(l(2)-l(1))+ ...
                       UCM*MPs/UC*(m(2)-m(1))+p(1)-p(2)-mu(1));

R(8,1)       = UM/UC*((UCM/UM-UCC/UC)*cs *ca(1)+ ...
                      (ULM/UM-UCL/UC)*ls *la(1)+ ...
                      (UMM/UM-UCM/UC)*MPs*ma(1))+ ...
             beta/mus*(UCC*cs /UC*(ca(2)-ca(1))+ ...
                       UCL*ls /UC*(la(2)-la(1))+ ...
                       UCM*MPs/UC*(ma(2)-ma(1))+pa(1)-pa(2)-mua(1));

