function [R,R2] = res_gmod_ar1(Z,param)
%RES_GMOD_AR1   Residuals for a two-sector growth model with AR(1) 
%               technology.  See gmodel.tex for details.

%               Ellen R. McGrattan, 5-22-00
%               Revised, ERM, 6-6-00

%______________________________________________________________________________
%
% PARAMETERS

a1       = param(1); 
a2       = param(2); 
b1       = param(3);
b2       = param(4);
beta     = param(5);
del1     = param(6);
del2     = param(7);
mphi     = param(8);
mthet    = param(9);
mu       = param(10);
psi      = param(11);
rho      = param(12);
sigma    = param(13);

sigm1    = 1-sigma;
%______________________________________________________________________________
%
% VARIABLES APPEARING IN THE RESIDUALS 
%

k1(1)    = exp(Z(5));  k1(2)    = exp(Z(1));
k2(1)    = exp(Z(6));  k2(2)    = exp(Z(2));
n1(1)    = exp(Z(7));  n1(2)    = exp(Z(3));
x1(1)    = exp(Z(8));  x1(2)    = exp(Z(4));
z(1)     = exp(Z(17)); z(2)     = exp(Z(9));
tauc(1)  = Z(18);      tauc(2)  = Z(10);
tau1(1)  = Z(19);      tau1(2)  = Z(11);
tau2(1)  = Z(20);      tau2(2)  = Z(12);
taun(1)  = Z(21);      taun(2)  = Z(13);
g(1)     = exp(Z(22)); g(2)     = exp(Z(14));
theta(1) = Z(23);      theta(2) = Z(15);
phi(1)   = Z(24);      phi(2)   = Z(16);

n2       = ( a2/a1* (1-phi)./(1-theta).*((k2./z).^(phi*rho)./ ...
              ((k1./z).^(theta*rho))).* ...
              n1.^(1-rho*(1-theta)) ).^([1,1]./(1-rho*(1-phi)));

y1       = k1.^theta.*(z.*n1).^(1-theta);
y2       = k2.^phi  .*(z.*n2).^(1-phi);
y        = (a1*y1.^rho+a2*y2.^rho).^(1/rho);
p1       = a1 *(y./y1).^(1-rho);
p2       = a2 *(y./y2).^(1-rho);
l        = 1-n2-n1;
c        = l.*(1-taun).*p1.*(1-theta).*(k1./n1).^theta.*z.^(1-theta)./ ...
           (1+tauc)/psi;
x        = y-c-g;
x2       = x-x1;
phi1     = b1*(x1./k1-mu+1-del1).^2/2;
phi2     = b2*(x2./k2-mu+1-del2).^2/2;
dphi1    = b1*(x1./k1-mu+1-del1);
dphi2    = b2*(x2./k2-mu+1-del2);
muc      = c.^(-sigma).*l.^(psi*sigm1);
r1       = p1.*theta.*(z.*n1./k1).^(1-theta);
r2       = p2.*phi.*(z.*n2./k2).^(1-phi);

%______________________________________________________________________________
%
% RESIDUALS:
%

R     = [beta*mu^(-sigma)*(1+tauc(1))/(1+tauc(2))* ...
            muc(2)/muc(1)*(r1(2)-tau1(2)*(r1(2)-del1)+ ...
            1/(1-dphi1(2))*(1-del1-phi1(2)+dphi1(2)*x1(2)/k1(2)))-  ...
            1/(1-dphi1(1));
         beta*mu^(-sigma)*(1+tauc(1))/(1+tauc(2))* ...
            muc(2)/muc(1)*(r2(2)-tau2(2)*(r2(2)-del2)+ ...
            1/(1-dphi2(2))*(1-del2-phi2(2)+dphi2(2)*x2(2)/k2(2)))-  ...
            1/(1-dphi2(1));
         k1(2)-((1-del1)*k1(1)+x1(1)-phi1(1)*k1(1))/mu;
         k2(2)-((1-del2)*k2(1)+x2(1)-phi2(1)*k2(1))/mu];

R2    =  log(c(1)^(-sigma)*l(1)^(psi*sigm1));


