LoadError: UndefVarError in julia for loop

Viewed 141

I am defining a variable phi_old at the start of my function, and assigning it a value of phi_start. I then have an iterative loop that:

  • uses phi_old
  • solves a PDE to get a solution T
  • uses the solution T to calculate phi_new
  • checks the difference between phi_new and phi_old, if it is larger than error the iterative loop begins again, with phi_old using the values of phi_new.
  • if the difference is less than the error, the iteration stops.

But I am getting this error:

Warning: Assignment to `T` in soft scope is ambiguous because a global variable by the same name exists:
ERROR: LoadError: UndefVarError: phi_old not defined

Here is an example of my code's structure:

phi_old = createCellVariable(m,phi_start)

for i=1:i_end
 phi_new = myfunction(T)
 error = sum(phi_new.value[2,1:end]-phi_old.value[2,1:end])
 if error > 1E-03
  phi_old = phi_new
 else
  i=i+1
  break
 end
end

Are there any techniques I could use to better initialize/assign my arrays and fix this error? The arrays are quite large, so I would like to preallocate if possible, and only copy arrays if necessary.

0 Answers
Related