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

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

%______________________________________________________________________________
%
% PARAMETERS

a1       = param(1); 
a2       = param(2); 
bm1      = param(3); 
bu1      = param(4); 
b2       = param(5); 
beta     = param(6);
delm1    = param(7);
delu1    = param(8);
del2     = param(9);
eta      = param(10);
gamma    = param(11);
mphi     = param(12);
mthetm   = param(13);
mthetu   = param(14);
psi      = param(15);
rho      = param(16);
scale    = param(17);
sigma    = param(18);

grate    = gamma+eta+gamma*eta;
sigm1    = 1-sigma;
%______________________________________________________________________________
%
% VARIABLES APPEARING IN THE RESIDUALS 
%

km1(1)   = exp(Z(7));  km1(2)   = exp(Z(1));
ku1(1)   = exp(Z(8));  ku1(2)   = exp(Z(2));
k2(1)    = exp(Z(9));  k2(2)    = exp(Z(3));
xm1(1)   = exp(Z(10)); xm1(2)   = exp(Z(4));
xu1(1)   = exp(Z(11)); xu1(2)   = exp(Z(5));
n1(1)    = exp(Z(12)); n1(2)    = exp(Z(6));

omega(1) = Z(24);      omega(2) = Z(13);
tauc(1)  = Z(25);      tauc(2)  = Z(14);
tau1(1)  = Z(26);      tau1(2)  = Z(15);
tau2(1)  = Z(27);      tau2(2)  = Z(16);
taun(1)  = Z(28);      taun(2)  = Z(17);
g(1)     = exp(Z(29)); g(2)     = exp(Z(18));
thetm(1) = Z(30);      thetm(2) = Z(19);
thetu(1) = Z(31);      thetu(2) = Z(20);
phi(1)   = Z(32);      phi(2)   = Z(21);
tauk1(1) = Z(33);      tauk1(2) = Z(22);
tauk2(1) = Z(34);      tauk2(2) = Z(23);

n2       = ( a2/a1* (1-phi)./(1-thetm-thetu).*(k2.^(phi*rho)./ ...
             (km1.^(thetm*rho).*ku1.^(thetu*rho))).* ...
             n1.^(1-rho*(1-thetm-thetu)) ).^([1,1]./(1-rho*(1-phi)));
y1       = km1.^thetm.*ku1.^thetu.*n1.^(1-thetm-thetu);
y2       = k2.^phi.*n2.^(1-phi);
y        = scale*(a1*y1.^rho+a2*y2.^rho).^(1/rho);
p1       = scale^rho*a1 *(y./y1).^(1-rho);
p2       = scale^rho*a2 *(y./y2).^(1-rho);
l        = 1-n2-n1;
c        = l.*(1-taun).*p2.*(1-phi).*y2./(n2.*(1+tauc))/psi;
x        = y-c-g;
x2       = x-xm1-xu1;
phim1    = bm1*(xm1./km1-grate-delm1).^2/2;
phiu1    = bu1*(xu1./ku1-grate-delu1).^2/2;
phi2     = b2*(x2./k2-grate-del2).^2/2;
dphim1   = bm1*(xm1./km1-grate-delm1);
dphiu1   = bu1*(xu1./ku1-grate-delu1);
dphi2    = b2*(x2./k2-grate-del2);
muc      = c.^(-sigma).*l.^(psi*sigm1);
rm1      = p1.*thetm.*y1./km1;
ru1      = p1.*thetu.*y1./ku1;
r2       = p2.*phi.*y2./k2;

%______________________________________________________________________________
%
% RESIDUALS:
%

R     = [
         beta*(1+tauc(1))/(1+tauc(2))*((1+gamma)*exp(omega(2)))^(-sigma)* ...
          muc(2)/muc(1)*(rm1(2)-tau1(2)*(rm1(2)-delm1-tauk1(2))-tauk1(2)+ ...
          1/(1-dphim1(2))*(1-delm1-phim1(2)+dphim1(2)*xm1(2)/km1(2)))-  ...
          1/(1-dphim1(1));
         beta*(1+tauc(1))/(1+tauc(2))*((1+gamma)*exp(omega(2)))^(-sigma)* ...
          muc(2)/muc(1)*(1-tau1(2))/(1-tau1(1))*(ru1(2)+ ...
          1/(1-dphiu1(2))*(1-delu1-phiu1(2)+dphiu1(2)*xu1(2)/ku1(2)))-  ...
          1/(1-dphiu1(1));
         beta*(1+tauc(1))/(1+tauc(2))*((1+gamma)*exp(omega(2)))^(-sigma)* ...
          muc(2)/muc(1)*(r2(2)-tau2(2)*(r2(2)-del2-tauk2(2))-tauk2(2)+ ...
          1/(1-dphi2(2))*(1-del2-phi2(2)+dphi2(2)*x2(2)/k2(2)))-  ...
          1/(1-dphi2(1));
         km1(2)-((1-delm1)*km1(1)+xm1(1)-phim1(1)*km1(1))* ...
          exp(-omega(2))/(1+grate);
         ku1(2)-((1-delu1)*ku1(1)+xu1(1)-phiu1(1)*ku1(1))* ...
          exp(-omega(2))/(1+grate);
         k2(2)-((1-del2)*k2(1)+x2(1)-phi2(1)*k2(1))*exp(-omega(2))/(1+grate)];

 
R2    =  log(muc(1));

