Loop with no index

11 visualizzazioni (ultimi 30 giorni)
heidi pham
heidi pham il 9 Feb 2019
Commentato: Sebastian il 25 Lug 2022
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 Commenti
Stephen23
Stephen23 il 9 Feb 2019
Modificato: Stephen23 il 9 Feb 2019
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
heidi pham il 9 Feb 2019
Thank all of you.
Thanks @Stephen for pointing out this.

Accedi per commentare.

Risposte (1)

Cris LaPierre
Cris LaPierre il 9 Feb 2019
Modificato: Cris LaPierre il 9 Feb 2019
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 Commento
Sebastian
Sebastian il 25 Lug 2022
Hello how and where can you include de prodf function.

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Tag

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by