RE: Re: Is it possible to access internal variables?

*To*: mathgroup at smc.vnet.net*Subject*: [mg34749] RE: [mg34709] Re: [mg34705] Is it possible to access internal variables?*From*: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>*Date*: Tue, 4 Jun 2002 04:45:18 -0400 (EDT)*Sender*: owner-wri-mathgroup at wolfram.com

Hello Bob, there is something mysterious with your proposal that disturbs me somewhat. See my records: In[1]:= f[x_] := (x - 3)(x - 4) In[2]:= g[x_] := Module[{t = f[x]}, v = Append[v, {x, t}]; t]; In[3]:= v = {}; FindMinimum[g[x], {x, 2, 1, 3}] >From In[3]:= FindMinimum::"regex": "Reached the point \!\({3.5000000000000018`}\) which is \ outside the region \!\({\({1.`, 3.`}\)}\)." >From In[3]:= FindMinimum::"regex": "Reached the point \!\({3.5000000000000018`}\) which is \ outside the region \!\({\({1.`, 3.`}\)}\)." Out[3]= FindMinimum[g[x], {x, 2, 1, 3}] In[4]:= v Out[4]= {{2., 2.}, {2., 2.}, {2.03136, 1.9069}, {2.05074, 1.85035}, {2.18642, 1.4755}, {3., 0.}, {2., 2.}, {2., 2.}, {2.03136, 1.9069}, {2.05074, 1.85035}, {2.18642, 1.4755}, {3., 0.}} In[5]:= g[x] Out[5]= (-4 + x) (-3 + x) In[6]:= ?g Global`g g[x_] := Module[{t = f[x]}, v = Append[v, {x, t}]; t] Apart from the fact that FindMinimum is restarted and that the first value appears twice, this seems to be what you intended. It doesn't help Arny though, as the minimum value -- what he supposed to be that x occuring in the error message -- is not included in that list v. But there seems to be something special with g, if I try with h it doesn't work: In[7]:= h[x_] := Module[{t = f[x]}, Append[v, {x, t}]; t]; In[8]:= v = {}; FindMinimum[h[x], {x, 2, 1, 3}] >From In[8]:= FindMinimum::"regex": "Reached the point \!\({3.5000000000000018`}\) which is \ outside the region \!\({\({1.`, 3.`}\)}\)." >From In[8]:= FindMinimum::"regex": "Reached the point \!\({3.5000000000000018`}\) which is \ outside the region \!\({\({1.`, 3.`}\)}\)." Out[8]= FindMinimum[h[x], {x, 2, 1, 3}] In[9]:= v (* v is empty *) Out[9]= {} In[10]:= h[x] Out[10]= (-4 + x) (-3 + x) In[11]:= ?h Global`h h[x_] := Module[{t = f[x]}, Append[v, {x, t}]; t] Now observe: In[12]:= FindMinimum[Print[x]; f[x], {x, 2, 1, 3}] >From In[12]:= x >From In[12]:= 2. >From In[12]:= 2.03136 >From In[12]:= 2.05074 >From In[12]:= 2.18642 >From In[12]:= 3. >From In[12]:= FindMinimum::"regex": "Reached the point \!\({3.5000000000000018`}\) which is \ outside the region \!\({\({1.`, 3.`}\)}\)." Out[12]= FindMinimum[Print[x]; f[x], {x, 2, 1, 3}] The Argument of FindMinimum is evaluated first with symbolic x (to get at the derivative I suppose), later, at the second call, it and such also t = (-4 + x) (-3 + x) get a numeric value, and therefore the values are introduced into v when appending {x,t}, so the start value gets doubled. The restart of the minimization seems to be triggered by the compound expression v = {}; FindMinimum[...] in the main loop (not within Find Minimum) In[19]:= h5[x_] := Module[{t = f[x]}, Print[x]; t]; In[20]:= v = {}; FindMinimum[h5[x], {x, 2, 1, 3}] >From In[20]:= x >From In[20]:= 2. >From In[20]:= 2.03136 >From In[20]:= 2.05074 >From In[20]:= 2.18642 >From In[20]:= 3. >From In[20]:= FindMinimum::"regex": "Reached the point \!\({3.5000000000000018`}\) which is \ outside the region \!\({\({1.`, 3.`}\)}\)." >From In[20]:= x >From In[20]:= 2. >From In[20]:= 2.03136 >From In[20]:= 2.05074 >From In[20]:= 2.18642 >From In[20]:= 3. >From In[20]:= FindMinimum::"regex": "Reached the point \!\({3.5000000000000018`}\) which is \ outside the region \!\({\({1.`, 3.`}\)}\)." Out[20]= FindMinimum[h5[x], {x, 2, 1, 3}] Any expression e.g. (3; FindMinimum[...]) does that too; conversely if you initialize v in an extra line, there will be no restart. This may have something to do with peculiarities of compound expression, sometimes starting a re-evaluation. (This had been reported by Allan Hayes some years ago.) See: In[65]:= x = 5; In[66]:= x =.; Print[x]; {x, x = 3, x} >From In[66]:= x Out[66]= {3, 3, 3} In[67]:= x =. In[68]:= {x, x = 3, x} Out[68]= {x, 3, 3} Anyways, as shown above, Arny's desire to get at 'internal values' of FindMinimum seems to be hopeless. My advice would be to check for the error message and then possibly increase the search range (by a certain tolerance) and restart (only a finite number of times). -- Hartmut > -----Original Message----- > From: BobHanlon at aol.com [mailto:BobHanlon at aol.com] To: mathgroup at smc.vnet.net > Sent: Sunday, June 02, 2002 7:15 AM > Subject: [mg34749] [mg34709] Re: [mg34705] Is it possible to access internal > variables? > > > > In a message dated 6/1/02 6:18:58 AM, > someone at somewhere.sometime writes: > > >I am minimizing a function which only has real values > >between 1 and -1, > most > >of the time... occasionally its range is different and > unknown, (but anyway > >not far from 1 and -1). I could write a routine using Check > to catch errors > >and then expand the range over which FindMinimum is allowed > to search, > >but > >since FindMinimum seems to be getting the appropriate values > anyway - it > >tells me the values in its error message - I was wondering > if there weren't > >some way to get at those values and use them without > bothering to write > >said routine. I was thinking of analysing 'MessageList' or > '$MessageList', > >but was thinking there might be some easier way. > > > >Aren't variables within packages accessible via their long > names, e.g. > >`package`private`variablename or something like that? Does > anyone have > >any suggestions? > > > > f[x_] := (x-3)(x-4); > > g[x_] := Module[{t = f[x]}, v=Append[v, {x,t}]; t]; > > g is the same function as f except that calls to g are recorded in v > > v={}; FindMinimum[g[x], {x, 2}] > > {-0.25, {x -> 3.5}} > > FindMinimum called g at the following values of x: > > v[[All,1]] > > {2., 2., 2.0313609375, 2.0507430627940644, > > 2.1864179398525154, 3.136142079261673, 3.500000000000001, > > 3.838316500294484, 3.5, 3.3180710396308366, > > 3.4999904004703097, 3.5, 3.499999994722301} > > > Bob Hanlon > Chantilly, VA USA >