function [res,V] = dectcapfoc(x,param);
% DECTCAPFOC  
%   u = US
%   r = ROW
%   d = Dell Corporation
%   f = Fuji Corporation
%
%   max E sum  beta^t log(Cu[t]/Nu[t])+psi(1-Hu[t]/Nu[t])}Nu[t] 
%          t    
%   s.t.  Cu[t]+Xu[t]+Xd[t]+Bu[t+1] = Wu[t]Hu[t] + rku[t] Ku[t]
%                                     + rmd[t]*Md[t] +(1+rb[t])Bu[t]
%                           Ku[t+1] = (1-delta)Ku[t]+Xu[t]
%                           Md[t+1] = (1-delta)Md[t]+Xd[t]
%                           Ku[t]   = Kud[t]+Kuf[t]
%                           Xu[t]   = Xud[t]+Xuf[t]
%                           Ku[0],Md[0],Bu[0], {Au[t],Nu[t],rb[t]} given
%
%   max E sum beta^t {log(Cr[t]/Nr[t])+psi*log(1-Hr[t]/Nr[t])}Nr[t],
%          t
%   s.t.  Cr[t]+Xr[t]+Xf[t]+Br[t+1] = Wr[t]Hr[t] + rkr[t] Kr[t]
%                                     + rmf[t]*Mf[t] +(1+rb[t])Br[t]
%                           Kr[t+1] = (1-delta)Kr[t]+Xr[t]
%                           Mf[t+1] = (1-delta)Mf[t]+Xf[t]
%                           Kr[t]   = Krd[t]+Krf[t]
%                           Xr[t]   = Xrd[t]+Xrf[t]
%                           Kr[0],Mf[0],Br[0], {Ar[t],Nr[t],rb[t]} given
%
%   Market clearing: 
%         sum_i Bi[t]=0
%         Cu[t]+Cr[t]+Xu[t]+Xr[t]+Xd[t]+Xf[t] = Yud[t]+Yuf[t]+Yrd[t]+Yrf[t]
%         Wage and rental rates = marginal products
                                                                                
%   Ellen McGrattan, 11-11-07
%   Revised, ERM, 1-17-08

%
% Parameters
%
alpha = param(1);
beta  = param(2);
delta = param(3);
phi   = param(4);
psi   = param(5);
Kud0  = param(6);
Kuf0  = param(7);
Krd0  = param(8);
Krf0  = param(9);
Md0   = param(10);
Mf0   = param(11);
B0    = 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,11);
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);
Bn    = X(:,11);

%
% 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)];
B     = [B0;Bn(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    = (1-alpha)*(1-phi)*Yud.*(Nu-Hu)./(psi*Hud);
Cr    = (1-alpha)*(1-phi)*Yrf.*(Nr-Hr)./(psi*Hrf);
Cun   = [Cu(2:T);Cu(T)];
Crn   = [Cr(2:T);Cr(T)];
cu    = Cu./Nu;
cun   = Cun./Nun;
cr    = Cr./Nr;
crn   = Crn./Nrn;
Yudn  = [Yud(2:T);Yud(T)];
Yufn  = [Yuf(2:T);Yuf(T)];
Yrdn  = [Yrd(2:T);Yrd(T)];
Yrfn  = [Yrf(2:T);Yrf(T)];
GDPu  = Yud+Yuf-Xd;
NXu   = GDPu-Cu-Xu;
rb    = [1/beta-1;cu(2:T)./cu(1:T-1)/beta-1];
res   = [Yud+(1-phi)*Yuf+phi*Yrd-(Xu+Xd+Bn-(1+rb).*B)-Cu;
         Yrf+(1-phi)*Yrd+phi*Yuf-(Xr+Xf-Bn+(1+rb).*B)-Cr;
         Yud./Hud-Yuf./Huf
         Yrd./Hrd-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);
         [cu(1:T-1)./cu(2:T)-cr(1:T-1)./cr(2:T);Bn(T)-B(T)]];

V     = [NXu./GDPu*100];
