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

• To: mathgroup at smc.vnet.net
• Subject: [mg34725] RE: [mg34709] Re: [mg34705] Is it possible to access internal variables?
• From: "DrBob" <majort at cox-internet.com>
• Date: Mon, 3 Jun 2002 01:46:47 -0400 (EDT)
• Sender: owner-wri-mathgroup at wolfram.com

```For a bit more insight and flexibility, try this:

ClearAll[f, mem]
f[x_] := (x - 3)(x - 4);
mem[func_] := mem[func] = {};
mem[x_, func_] := Module[{t = func[x]},
AppendTo[mem[func], {x, t, func'[x]}];
t];
FindMinimum[mem[x, f], {x, 2}]
NumberForm[
TableForm[mem[f], TableHeadings -> {Automatic, {"x", "f[x]",
"f'[x]"}}], 12]

ODDLY ENOUGH, when I tried to use NumberForm within TableForm, the
result didn't display as a table, so I don't know how to show different
columns of the table at different display precisions.

QUESTION: Does anybody know how to do that?

Partly as a consequence of this, I can't tell on what iteration the
final answer was achieved!!  The table above seems to say the optimum
was visited three times, but the derivative is displayed as zero at only
two of them.

The following yields more insight:

ClearAll[f, mem]
f[x_] := (x - 3)(x - 4);
mem[func_] := mem[func] = {};
mem[x_, func_] := Module[{t = func[x]},
AppendTo[mem[func], {x - 7/2, func[x] - func[7/2], func'[x]}];
func[x]];
FindMinimum[mem[x, f], {x, 2}]
TableForm[mem[f],
TableHeadings -> {Automatic, {"x-7/2", "f[x]-f[7/2]", "f'[x]"}}]

Look how slow the search is for the first five evaluations (despite the
regular behavior of this function), and notice that f is evaluated twice
at the initial value:

Subtract @@ mem[f][[{1, 2}]]

{0., 0., 0.}

Also, if I give two initial values, the result is no longer exact, and f
is never evaluated at the second initial value, contrary to the
documentation:

ClearAll[f, mem]
f[x_] := (x - 3)(x - 4);
mem[func_] := mem[func] = {};
mem[x_, func_] := Module[{t = func[x]},
AppendTo[mem[func], {x, t, func'[x]}];
t];
FindMinimum[mem[x, f], {x, {3.49, 3.51}}]
x - 7/2 /. %[[-1]]
NumberForm[
TableForm[mem[f], TableHeadings -> {Automatic, {"x", "f[x]",
"f'[x]"}}], 12]

I bracketed the optimum closely, and it didn't speed things up at all!

However, Method->Newton or Method->QuasiNewton improves things greatly:

ClearAll[f, mem]
f[x_] := (x - 3)(x - 4);
mem[func_] := mem[func] = {};
mem[x_, func_] := Module[{t = func[x]},
AppendTo[mem[func], {x, t, func'[x]}];
t];
FindMinimum[mem[x, f], {x, 2}, Method -> Newton]
x - 7/2 /. %[[-1]]
NumberForm[
TableForm[mem[f], TableHeadings -> {Automatic, {"x", "f[x]",
"f'[x]"}}], 12]

With this Method, the function is evaluated symbolically once, then once
at the initial value (unnecessary!), and once at the zero of the

I wonder what the Automatic method is.

Bobby Treat

-----Original Message-----
From: BobHanlon at aol.com [mailto:BobHanlon at aol.com]
To: mathgroup at smc.vnet.net
Subject: [mg34725] [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

```

• Prev by Date: RE: Package problem
• Next by Date: How can solue the inequation system in mathematica
• Previous by thread: Re: Is it possible to access internal variables?
• Next by thread: Re: RE: Re: Is it possible to access internal variables?