Hi everyone. I am trying to create a simple Value Function Iteration routine for a basic econ. problem.
I’m facing the common problem of global and local variable definition, but I don’t know how to correct in this case. Below is a simple version of the problem I am trying to solve:
agrid = 10 zgrid = 3 Pz = [0.11 0.85 0.04; 0.07 0.87 0.06; 0.03 0.85 0.12] a = zeros(agrid, 1) V = zeros(agrid, zgrid) V_new = zeros(agrid, zgrid) u = zeros(agrid) erro = 100 while erro > 0.1 for ia = 1:agrid for iz = 1:zgrid for ja = 1:agrid c = 0.02*a[ia] + exp(z[iz]) - a[ja] EV = transpose(Pz[iz,:])*V[ja,:] u[ja] = -1/c + 0.95*EV end Vmax, imax = findmax(u) V_new[ia,iz] = Vmax end end erro = maximum(abs.(V_new - V)) V = V_new end
The message I get is ‘UndefVarError: V not defined’. In this case, I don’t know how I can use ‘global definition variable’ or the ‘let’ operator to fix my problem.