function res = tcapfoc(x,param);
% TCAPFOC  
%   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);
Kud0  = param(7);
Kuf0  = param(8);
Krd0  = param(9);
Krf0  = param(10);
Md0   = param(11);
Mf0   = param(12);
T     = param(13);

Au    = param(14:T+13);
Ar    = param(T+14:2*T+13);
Nu    = param(2*T+14:3*T+13);
Nr    = param(3*T+14:4*T+13);
sigu  = param(4*T+14:5*T+13);
sigr  = param(5*T+14:6*T+13);

%
% Variables
%

X     = zeros(T,10);
X(:)  = x;
Kudn  = X(:,1);
Kufn  = X(:,2);
Krdn  = X(:,3);
Krfn  = X(:,4);
Mdn   = X(:,5);
Mfn   = X(:,6);
Hud   = X(:,7);
Huf   = X(:,8);
Hrd   = X(:,9);
Hrf   = X(:,10);

%
% Intermediate variables and residuals
%

Nun   = [Nu(2:T);Nu(T)];
Nrn   = [Nr(2:T);Nr(T)];
Kud   = [Kud0;Kudn(1:T-1)];
Kuf   = [Kuf0;Kufn(1:T-1)];
Krd   = [Krd0;Krdn(1:T-1)];
Krf   = [Krf0;Krfn(1:T-1)];
Md    = [Md0;Mdn(1:T-1)];
Mf    = [Mf0;Mfn(1:T-1)];
Xu    = (Kudn+Kufn)-(1-delta)*(Kud+Kuf);
Xr    = (Krdn+Krfn)-(1-delta)*(Krd+Krf);
Xd    = Mdn-(1-delta)*Md;
Xf    = Mfn-(1-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);
Cun   = [Cu(2:T);Cu(T)];
Crn   = [Cr(2:T);Cr(T)];
cu    = Cu./Nu;
cun   = Cun./Nun;
Yudn  = [Yud(2:T);Yud(T)];
Yufn  = [Yuf(2:T);Yuf(T)];
Yrdn  = [Yrd(2:T);Yrd(T)];
Yrfn  = [Yrf(2:T);Yrf(T)];

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*cu./cun.*(alpha*(1-phi)*Yudn./Kudn+1-delta);
         1-beta*cu./cun.*(alpha*(1-phi)*Yufn./Kufn+1-delta);
         1-beta*cu./cun.*(alpha*(1-phi)*Yrdn./Krdn+1-delta);
         1-beta*cu./cun.*(alpha*(1-phi)*Yrfn./Krfn+1-delta);
         1-beta*cu./cun.*(phi*Yudn./Mdn+phi*Yrdn./Mdn+1-delta);
         1-beta*cu./cun.*(phi*Yufn./Mfn+phi*Yrfn./Mfn+1-delta)];
