error: Index in position 2 exceeds array bounds (must not exceed 2).
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposta accettata
Adam Danz
il 23 Set 2020
Modificato: Adam Danz
il 23 Set 2020
The error is happening here
squeeze(V0_1(ik2, iter+1)')
% or, V0_1( 1 , 15 )
V0_1 is 101 x 2.
iter is equal to 14 on the first iteration of the J-loop.
So, you're trying to access the 15th column (iter+1 ) of V0_1 but it only has 2 columns.
0 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Matrix Indexing in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!