error: Index in position 2 exceeds array bounds (must not exceed 2).
1 次查看(过去 30 天)
显示 更早的评论
Hi. I need help fixing this error in my code. I have compared it to similar issues/code back cant seem to figure out the fix.
eta=1.5;
beta=0.8; % discount factor
rho=0.8;
sigma=0.1;
K=(0:0.1:10)'; % grid over capital
dimK=size(K,1); % grid size of K
K_prime=K';
dimK_prime=dimK';
J=14; %life period
r=0.25; % interest rate
et=[ 1.00 1.30 1.53 1.68 1.76 1.77 1.76 1.72 1.61 0.54 0.54 0.54 0.54 0.54]; %%et
%meanY=ones(1, J);
%%shock and transition matrix%%
Y= [et*(1-sigma) et*(1+sigma)]; %income shock
dimY=2;
transY= [ (1+rho)/2 (1-rho)/2;(1-rho)/2 (1+rho)/2]; %transiiton matrix: technology shock
V=ones(dimK,J+1)*(-1e10); % initialize the value function. Rows are x, columns the iteration
U=ones(dimK,1)*(-1e10);
A=ones(dimK, J+1);
C=zeros(dimK, J);
A_ind=zeros(dimK, J+1);
V(:, J+1)=zeros(dimK,1); % initialize the value function. Rows are x, columns the iteration
Bet=ones(1,J)*beta;
Bet=cumprod(Bet)/beta;
%V0=ones(dimK,dimK);
indY=1:dimY;
V0=ones(dimK,dimY)*(-1e10);
for iter=J:-1:1 % loop over
iter;
for indY=1:dimY
V0_1(:, indY)=interp1(K, V0(:, indY), K_prime, 'linear', 'extrap');
end
for ik=1:dimK % loop over all current cake sizes
for ik2=1:dimK
c=K(ik)*(1+r)+Y(indY)-K(ik2);
if c>0
U(ik, ik2)=((c)^(1-eta)-1)/(1-eta)+beta*transY(indY, :).*squeeze(V0_1(ik2, iter+1)');
else
U(ik2)=-1e10;
end
end
[V(ik, iter), ind]=max(U); % optimizing over size of next period cake
A(ik, iter)=K(ind);
A_ind(ik, iter)=ind;
C(ik, iter)=K(ik)*(1+r)+Y(iter)-K(ind);
end
end
I assume ther error is in the bolded line, but Im not sure what the proper fix is.
Please help! Thank you, Matlab wizards!
0 个评论
采纳的回答
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!