Value Function - Scoping problem

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 copy V_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.

3 Likes