function [R,R2] = res_gmodel(Z,param)
%RES_GMODEL     Residuals for a two-sector growth model.
%               See gmodel.tex for details.

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

%______________________________________________________________________________
%
% PARAMETERS

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

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));
eta(1)   = Z(17);      eta(2)   = 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.^(phi*rho)./k1.^(theta*rho)).* ...
             n1.^(1-rho*(1-theta)) ).^([1,1]./(1-rho*(1-phi)));
y1       = k1.^theta.*n1.^(1-theta);
y2       = k2.^phi  .*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./(1+tauc)/psi;
x        = y-c-g;
x2       = x-x1;

%______________________________________________________________________________
%
% RESIDUALS:
%

R     = [beta*(1+tauc(1))/(1+tauc(2))* ...
          (mu*exp(eta(2)))^(-sigma)*c(2)^(-sigma)*l(2)^(psi*sigm1)* ...
          ((1-tau1(2))*(p1(2)*theta(2)*(n1(2)/k1(2))^(1-theta(2))-del1)+1)/ ...
          (c(1)^(-sigma)*l(1)^(psi*sigm1))-1;
         beta*(1+tauc(1))/(1+tauc(2))* ...
          (mu*exp(eta(2)))^(-sigma)*c(2)^(-sigma)*l(2)^(psi*sigm1)* ...
          ((1-tau2(2))*(p2(2)*phi(2)*(n2(2)/k2(2))^(1-phi(2))-del2)+1)/ ...
          (c(1)^(-sigma)*l(1)^(psi*sigm1))-1;
         k1(2)-((1-del1)*k1(1)+x1(1))*exp(-eta(2))/mu;
         k2(2)-((1-del2)*k2(1)+x2(1))*exp(-eta(2))/mu];

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

