Loop with no index
이전 댓글 표시
hello,
I feel quite strange when the below loop can run normally eventhough it loops over "l" which never appear inside the code.
for i=1:n
for l=1:m
Vnext(i) = u(prodf(k(i)) + (1-delta) * k(i) - k(h(i))) + beta * Vhoward(h(i));
Vhoward = Vnext;
end;
end;
Is there anyone know why? Thank you in advance!
Here is the complete code. ( I somehow feel the code is not compact; if some one could suggest me to write the code in a more efficient way, I really appreciate!)
%% Value Function Iteration with Howard improvement
%clc;
clear;
tic;
global theta;
% Parameters
delta = 0.0196;
beta = 0.9896;
theta = 0.36;
n = 101; % number of grids
error = 0.01; % error criterion
% Steady State capital level
kstar = (1/theta*(1/beta-1+delta))^(1/(theta-1))
% Set up the grid for k
klow = 1;
kup = kstar * 1.20;
gridlength = kup - klow;
fineness = (kup-klow)/(n-1);
k = zeros(n,1); % grid for both k and k'
Vguess = zeros(n,1); % Guess for value function
Vnext = Vguess; % Guess for value function next period
gk = zeros(n,1); % Decision rules
V = zeros(n,n); % matrix of value functions
iter = 0; % Save the number of iterations
h = zeros(n,1);
for i=1:n
k(i) = klow + (i-1) * fineness;
Vguess(i) = u(prodf(k(i)) - delta*k(i))/(1-beta); % Store initial guess
%Vguess(i) = 0; % Store initial guess
end;
% Run the loop as long as Vguess and Vnext are not too close and at least
% once. We set the variable maxdist very high to ensure it runs at least
% once.
maxdist = 1000;
while(maxdist>error)
iter = iter + 1;
% Calculate the Value function matrix (k * k')
for i=1:n % Loop for k
for j=1:n % Loop for j
if(prodf(k(i)) + (1-delta)*k(i)-k(j) < 0)
V(i,j) = -1000;
else
V(i,j) = u( prodf(k(i)) + (1-delta)*k(i) - k(j)) + beta * Vguess(j);
end;
end;
end;
% Pick the optimal solution for each k and update guess
for i=1:n % Loop for k
[Vnext(i) andechs] = max(V(i,:)); % Update V
gk(i) = k(andechs); % Store the optimal decision
h(i) = andechs;
end;
% Howard improvement step
m = 10; % number of iterations without maximization
Vhoward = Vnext;
for i=1:n
for l=1:m
Vnext(i) = u(prodf(k(i)) + (1-delta) * k(i) - k(h(i))) + beta * Vhoward(h(i));
Vhoward = Vnext;
end;
end;
maxdist = abs(max(Vnext-Vguess)); % Check maximum distance between iteration
Vguess = Vnext; % Update the guess
end;
toc;
disp(['Number of iterations:' num2str(iter)]);
plot(k,Vguess)
댓글 수: 3
madhan ravi
2019년 2월 9일
See a simple example below what you are doing is waste of time:
>> A=1:3
tic
for k = 1:3
for l = 1:2
A(k);
end
end
toc
tic
for k = 1:3
A(k);
end
toc
A =
1 2 3
Elapsed time is 0.044132 seconds. % nested for loop
Elapsed time is 0.004503 seconds. % single for loop
>>
It is not required that a loop index is used inside the loop: a loop can still be very useful if the results of the previous iteration are used on the next iteration, which seems to be the case for your code (see the variable Vhoward ). However when I tried your code I got this error:
Undefined function or variable 'prodf'.
Error in temp0 (line 27)
Vguess(i) = u(prodf(k(i)) - delta*k(i))/(1-beta); % Store initial guess
Please provide everything that we need to run your code.
heidi pham
2019년 2월 9일
답변 (1개)
Cris LaPierre
2019년 2월 9일
편집: Cris LaPierre
2019년 2월 9일
You do not have to use your loop index variable inside your loop. It increments automatically.
However, because it is not used, I think you can just eliminate that loop completely.
for i=1:n
Vnext(i) = u(prodf(k(i)) + (1-delta) * k(i) - k(h(i))) + beta * Vhoward(h(i));
Vhoward = Vnext;
end;
The only difference I see is that Vhoward might have a different value when i==1 and l==1. After that, Vhoward = Vnext. That may matter. We actually can't check as prodf is missing.
댓글 수: 1
Sebastian
2022년 7월 25일
Hello how and where can you include de prodf function.
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!