Yes, sorry I jumped the gun. Thanks for clarifying. 

But it still does not have anything to do with Optim :)

The problem is due to defining an inline function (line 43) that creates 
closure over the "x_previous" variable.  To test this, just comment that 
line (and adjust the Optim.optimize call), the problem goes away. 

A simpler version of the code that fails is as follows: 

julia> function f() 
         for i=1:10
           if i>2; println(z); end
           z=2
           g() = 2z
         end
       end
f (generic function with 1 method)

julia> f()
ERROR: z not defined
 in f at none:3

A fix to get it to work is to declare the variable at the start of your 
function. Similarly, adding a "local x_previous" at the top of your 
function makes it work correctly. Remember, variables in Julia are lexical 
in scope. 

julia> function f()
         local z
         for i=1:10
           if i>2; println(z); end
           z=2
           g()=2z
         end
       end
f (generic function with 1 method)

julia> f()
2
2
2
2
2
2
2
2


On Wednesday, 29 April 2015 02:23:28 UTC+1, Pooya wrote:
>
> If you comment out lines 42-49, you will see that it works fine!
>
> On Tuesday, April 28, 2015 at 9:20:49 PM UTC-4, Pooya wrote:
>>
>> Thanks, but I think "if iter > 2" (line 21) makes sure that x_previous 
>> is defined in the previous iteration. Just to be clear, the condition to 
>> check here was "g_norm > g_norm_old", but I changed it to get there as 
>> early as the second iteration.  
>>
>> On Tuesday, April 28, 2015 at 9:13:49 PM UTC-4, Avik Sengupta wrote:
>>>
>>> I'm seeing the error in line 22 of your gist where you are trying to 
>>> print the current value of "x_previous". However, x_previous is first 
>>> defined in line 38 of your gist, and so the error is correct and doesnt 
>>> have anything to do with Optim, as far as I can see. 
>>>
>>> On Wednesday, 29 April 2015 01:39:02 UTC+1, Pooya wrote:
>>>>
>>>> Hi all,
>>>>
>>>> I have a problem that has made me scratch my head for many hours now! 
>>>> It might be something obvious that I am missing. I have a Newton-Raphson 
>>>> code to solve a system of nonlinear equations. The error that I get here 
>>>> does not have anything to do with the algorithm, but just to be clear, I 
>>>> need to find the best possible solution if the equations are not solvable, 
>>>> so I am trying to stop simulation when the direction found by 
>>>> Newton-Raphson is not correct! In order to do that I put an if-loop in the 
>>>> beginning of the main loop to take x from the previous iteration 
>>>> (x_previous), but I get x_previous not defined! I am using the Optim 
>>>> package to do a line search after the direction has been found by 
>>>> Newton-Raphson. If Optim is not used, things work perfectly (I tried by 
>>>> commenting out those lines of code). Otherwise I get the error I 
>>>> mentioned. 
>>>> My code is here: https://gist.github.com/prezaei85/372bde76012472865a94, 
>>>> which solves a simple one-variable quadratic equation. Any thoughts are 
>>>> very much appreciated.
>>>>
>>>> Thanks,
>>>> Pooya
>>>>
>>>

Reply via email to