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

%                 Ellen R. McGrattan, 2-1-00
%                 Revised, ERM, 2-12-00

%______________________________________________________________________________
%
% PARAMETERS

beta       = param(11);
ome1       = param(12);
rho        = param(13);
theta      = param(14);
alpha      = param(15);
delta      = param(16);
b          = param(17);

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

%______________________________________________________________________________
%
% VARIABLES APPEARING IN THE RESIDUALS
%

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

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

g          = Z([28,27]);
ga         = Z([30,29]);
           % g   = [g_t,g_{t+1}]'

a          = Z([32,31]);
aa         = Z([34,33]);
           % a   = [a_t,a_{t+1}]'

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

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

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

kk         = k(2:3,:) ./k(1:2);
kka        = ka(2:3,:)./ka(1:2);

x          = (1+b*delta-sqrt(1+2*b-2*b*kk))/b .* k(1:2);
xa         = (1+b*delta-sqrt(1+2*b-2*b*kka))/b.* ka(1:2);
           % x  = [x_t,x_{t+1}]'

c          = y-x-g;
ca         = ya-xa-ga;
           % c  = [c_t,c_{t+1}]'

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

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

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

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

[U,UC,UM,UL]     = util(c,m,l,param);
[Ua,UCa,UMa,ULa] = util(ca,ma,la,param);

w          = -UL./UC;
wa         = -ULa./UCa;
           % w  = [w_t,w_{t+1}]'

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

q          = UCa./UC;
           % q  = [q_t,q_{t+1}]'

lh         = (ome1*p).^(1/rho1) .*ph .^((theta-rho)/(rho1*thet1)).*y;
lf         = (ome1*p).^(1/rho1) .*pf .^((theta-rho)/(rho1*thet1)).*y;
lfa        = (ome1*pa).^(1/rho1).*pfa.^((theta-rho)/(rho1*thet1)).*ya;
lha        = (ome1*pa).^(1/rho1).*pha.^((theta-rho)/(rho1*thet1)).*ya;
           % lh = [lam_{h,t},lam_{h,t+1}]'
%______________________________________________________________________________
%
% LINEARIZED RESIDUALS:
%
% 1. PRICING EQUATIONS
%

R(1,1)     = ph(1) *theta*UC(1)/p(1)        *lh(1) - UC(1) *mc(1) *lh(1);
R(2,1)     = pf(1) *theta*UCa(1)/(p(1)*q(1))*lf(1) - UCa(1)*mca(1)*lf(1);
R(3,1)     = pfa(1)*theta*UCa(1)/pa(1)      *lfa(1)- UCa(1)*mca(1)*lfa(1);
R(4,1)     = pha(1)*theta*UC(1)*q(1)/pa(1)  *lha(1)- UC(1) *mc(1) *lha(1);

%
% 2. MONOPOLISTS' EULER EQUATIONS FOR CAPITAL
%

phin       = b/2*(x(2)/k(2)-delta)^2;
dphi       = b*(x(1)/k(1)-delta);
dphin      = b*(x(2)/k(2)-delta);
R(5,1)     = UC(1)/(1-dphi) - beta*UC(2)*(w(2)*alpha/alph1* ...
                l(2)/k(2)+(1-delta-phin+dphin*x(2)/k(2))/(1-dphin));

phin       = b/2*(xa(2)/ka(2)-delta)^2;
dphi       = b*(xa(1)/ka(1)-delta);
dphin      = b*(xa(2)/ka(2)-delta);
R(6,1)     = UCa(1)/(1-dphi) - beta*UCa(2)*(wa(2)*alpha/alph1* ...
                la(2)/ka(2)+(1-delta-phin+dphin*xa(2)/ka(2))/(1-dphin));

%
% 3. MONEY DEMAND EQUATIONS
%

R(7,1)     = UM(1)/UC(1)  +beta*UC(2)/UC(1)*p(1)/p(2)/mu(1)-1;
R(8,1)     = UMa(1)/UCa(1)+beta*UCa(2)/UCa(1)*pa(1)/pa(2)/mua(1)-1;

