UndefVarError: θ not defined

This is due to Julia’s scoping rules, which regularly trip up beginners. Variables defined in global scope (in this case, θ) aren’t visible within for loops unless they’re explicitly annotated as global variables. See here for further discussion.

A quick example:

julia>  θ = 0
0

julia> for i = 1:10
         θ += 1
       end
ERROR: UndefVarError: θ not defined
Stacktrace:
 [1] top-level scope at .\REPL[8]:2
julia> for i = 1:10
         global θ += 1
       end

julia> θ
10
2 Likes