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:

Yes, because your code is not right, z was not defined anyway in your original code, and it was used in line 19. So, there is an error, and we cannot fix it, because it is not a problem with the language, it is a logic error. Using a function (or using Jupyter( avoid the problem to access variables.

I have update the code with the z values you say, and it is working (I cannot help you more because I have not idea what are you trying to calculate).

However, in the second while iteration, the algorithm is updating V before computing the error, making maximum(abs.(V_new - V)) = max(abs.([-2.53 -1.96 -1.57; ...]- [-2.53 -1.96 -1.57; ....])) = 0 > 0.1 and stoping the iteration.

Matlab doesnt have this ‘behavior’ and dont know how to handle it in Julia.

I dont know if it is clear.
Any way, you already contributed a lot. Thanks!

You’ve got a lot of code here, so it’s hard to be sure, but I would suspect that the problem is here:

V = V_new

in Matlab, this would copyV_new and create a new vector called V. Any future changes to V_new would not affect V. In Julia, however, this just makes V a label for the same vector as V_new, so any changes to V_new will be shared with V. This is actually how all assignment operations in Julia work: Julia doesn’t silently copy things when you assign them or when you pass them to functions (unlike Matlab).

If you do want a copy, you can simply do: V = copy(V_new).

Perhaps that will help you figure out what’s different in the results you’re seeing.