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, 5-23-00

%______________________________________________________________________________
%
% PARAMETERS

a1       = param(1); 
a2       = param(2); 
beta     = param(3);
delc     = param(4);
deln     = 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 
%

kc(1)    = exp(Z(5));  kc(2)    = exp(Z(1));
kn(1)    = exp(Z(6));  kn(2)    = exp(Z(2));
hc(1)    = exp(Z(7));  hc(2)    = exp(Z(3));
ic(1)    = exp(Z(8));  ic(2)    = exp(Z(4));
z(1)     = exp(Z(16)); z(2)     = exp(Z(9));
tauc(1)  = Z(17);      tauc(2)  = Z(10);
taun(1)  = Z(18);      taun(2)  = Z(11);
tauh(1)  = Z(19);      tauh(2)  = Z(12);
g(1)     = exp(Z(20)); g(2)     = exp(Z(13));
theta(1) = Z(21);      theta(2) = Z(14);
phi(1)   = Z(22);      phi(2)   = Z(15);

hn       = ( a2/a1* (1-phi)./(1-theta).*((kn./z).^(phi*rho)./ ...
             ((kc./z).^(theta*rho))).* ...
             hc.^(1-rho*(1-theta)) ).^([1,1]./(1-rho*(1-phi)));
yc       = kc.^theta.*(z.*hc).^(1-theta);
yn       = kn.^phi  .*(z.*hn).^(1-phi);
y        = (a1*yc.^rho+a2*yn.^rho).^(1/rho);
pc       = a1 *(y./yc).^(1-rho);
pn       = a2 *(y./yn).^(1-rho);
l        = 1-hn-hc;
c        = l.*(1-tauh).*pc.*(1-theta).*(kc./hc).^theta.*z.^(1-theta)/psi;
i        = y-c-g;
in       = i-ic;

%______________________________________________________________________________
%
% RESIDUALS:
%

R     = [beta*mu^(-sigma)*c(2)^(-sigma)*l(2)^(psi*sigm1)* ...
          ((1-tauc(2))*(pc(2)*theta(2)*(z(2)*hc(2)/kc(2))^(1-theta(2))- ...
            delc)+1)/(c(1)^(-sigma)*l(1)^(psi*sigm1))-1;
         beta*mu^(-sigma)*c(2)^(-sigma)*l(2)^(psi*sigm1)* ...
          ((1-taun(2))*(pn(2)*phi(2)*(z(2)*hn(2)/kn(2))^(1-phi(2))- ...
            deln)+1)/(c(1)^(-sigma)*l(1)^(psi*sigm1))-1;
         kc(2)-((1-delc)*kc(1)+ic(1))/mu;
         kn(2)-((1-deln)*kn(1)+in(1))/mu];

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

