function res = tcapss(X,param);
% TCAPSS  
%   u = US
%   r = ROW
%   d = Dell Corporation
%   f = Fuji Corporation
%
%   max E sum  beta^t lam {log(Cu[t]/Nu[t])+psi(1-Hu[t]/Nu[t])}Nu[t] 
%          t     + (1-lam){log(Cr[t]/Nr[t])+psi(1-Hr[t]/Nr[t])}Nr[t]
% 
%   s.t.  Cu[t]+Cr[t]+Xu[t]+Xr[t]+Xd[t]+Xf[t]
%                   = Au[t] (Nu[t]Md[t])^phi *Zud[t]^(1-phi)
%                   + Au[t] sigu[t] (Nu[t]Mh[t])^phi *Zuh[t]^(1-phi)
%                   + Ar[t] sigr[t] (Nr[t]Md[t])^phi *Zrd[t]^(1-phi)
%                   + Ar[t] (Nr[t]Mh[t])^phi *Zrh[t]^(1-phi)
%         Zij[t]  = Kij[t]^theta * Hij[t]^(1-theta), i=u,r, j=d,f
%         Ki[t]   = Kid[t]+Kif[t], i=u,r
%         Hi[t]   = Hid[t]+Hif[t], i=u,r
%         Ki[t+1] = (1-delta)Ki[t]+Xi[t], i=u,r
%         Mj[t+1] = (1-delta)Mj[t]+Xj[t], j=d,f
%         Kij[0], Mj[0] given
%
%   Ellen McGrattan, 9-1-07
%   Revised, ERM, 1-17-08

%
% Parameters
%

alpha = param(1);
beta  = param(2);
delta = param(3);
lam   = param(4);
phi   = param(5);
psi   = param(6);
Au    = param(7);
Ar    = param(8);
Nu    = param(9);
Nr    = param(10);
sigu  = param(11);
sigr  = param(12);

%
% Variables
%

Kud   = X(1);
Kuf   = X(2);
Krd   = X(3);
Krf   = X(4);
Md    = X(5);
Mf    = X(6);
Hud   = X(7);
Huf   = X(8);
Hrd   = X(9);
Hrf   = X(10);

%
% Intermediate variables and residuals
%

Xu    = delta*(Kud+Kuf);
Xr    = delta*(Krd+Krf);
Xd    = delta*Md;
Xf    = delta*Mf;
Hu    = Hud+Huf;
Hr    = Hrd+Hrf;
Yud   = Au*(Nu*Md)^phi*(Kud^alpha*Hud^(1-alpha))^(1-phi);
Yuf   = Au*sigu*(Nu*Mf)^phi*(Kuf^alpha*Huf^(1-alpha))^(1-phi);
Yrd   = Ar*sigr*(Nr*Md)^phi*(Krd^alpha*Hrd^(1-alpha))^(1-phi);
Yrf   = Ar*(Nr*Mf)^phi*(Krf^alpha*Hrf^(1-alpha))^(1-phi);
Cu    = (Yud+Yuf+Yrd+Yrf-Xu-Xr-Xd-Xf)/(1+(1-lam)*Nr/(lam*Nu));
Cr    = (1-lam)*Cu*Nr/(lam*Nu);
res   = [psi*Cu/(Nu-Hu)-(1-alpha)*(1-phi)*Yud/Hud;
         psi*Cu/(Nu-Hu)-(1-alpha)*(1-phi)*Yuf/Huf;
         psi*Cr/(Nr-Hr)-(1-alpha)*(1-phi)*Yrd/Hrd;
         psi*Cr/(Nr-Hr)-(1-alpha)*(1-phi)*Yrf/Hrf;
         1-beta*(alpha*(1-phi)*Yud/Kud+1-delta);
         1-beta*(alpha*(1-phi)*Yuf/Kuf+1-delta);
         1-beta*(alpha*(1-phi)*Yrd/Krd+1-delta);
         1-beta*(alpha*(1-phi)*Yrf/Krf+1-delta);
         1-beta*(phi*Yud/Md+phi*Yrd/Md+1-delta);
         1-beta*(phi*Yuf/Mf+phi*Yrf/Mf+1-delta)];
