Re: Baffled by FindMinimum

• To: mathgroup at smc.vnet.net
• Subject: [mg124903] Re: Baffled by FindMinimum
• From: Bob Hanlon <hanlonr357 at gmail.com>
• Date: Sat, 11 Feb 2012 06:38:59 -0500 (EST)
• Delivered-to: l-mathgroup@mail-archive0.wolfram.com
• References: <201202101055.FAA00702@smc.vnet.net>

```>From the documentaion for FindMinimum: "FindMinimumfirst localizes the
values of all variables, then evaluates f with the variables being
symbolic, and then repeatedly evaluates the result numerically."

Since you don't want ObjectiveFunction to try evaluating with a
symbolic argument, restrict its definition to a vector of numeric
arguments.

In general, restrict the definition of any function to numeric
arguments when the function uses numeric techniques.

Clear[ObjectiveFunction]

ObjectiveFunction[p : {__?NumericQ}] :=
(p[[3]] - 1)^2 + Norm[p] /; Length[p] > 2

p0 = {1, 2, 2, 1, 2};

FindMinimum[ObjectiveFunction[xx], {xx, {1, 2, 3, 2, 1, 2}}]

{0.75, {xx -> {-5.48284*10^-9, -7.29576*10^-9,
0.5, -7.02789*10^-9, -5.48284*10^-9, -7.02789*10^-9}}}

Bob Hanlon

On Fri, Feb 10, 2012 at 5:55 AM, Sam Takoy <sam.takoy at yahoo.com> wrote:
> Hi,
>
> Please take a look at this simple code.
>
> ObjectiveFunction[p_] := Module[{},
>  Print["Why is this p: ", p];
>  (p[[3]] - 1)^2 + Norm[p]]
> p0 = {1, 2, 2, 1, 2};
> FindMinimum[ObjectiveFunction[xx], {xx, {1, 2, 3, 2, 1, 2}}]
>
> If you run it, you will find that ObjectiveFunction is called with the
> argument "xx" rather than the numerical vector. How come?
> Also, judging by the lack of the Print statement firing,
> ObjectiveFunction is not called again. Yet, it eventually (after
> complaining of understandable errors) produces the correct answer.
> What's going on here? I would expect if to be called repeated in the
> course of numerical minimization. I'm completely baffled by what's
> going on here.
>
> Here's the output:
>
> During evaluation of In[377]:= Why is this p: xx
>
> During evaluation of In[377]:= Part::partd: Part specification xx[[3]]
> is longer than depth of object. >>
>
> During evaluation of In[377]:= Part::partd: Part specification xx[[3]]
> is longer than depth of object. >>
>
> Out[380]= {0.75, {xx -> {-5.48284*10^-9, -7.29576*10^-9,
>    0.5, -7.02789*10^-9, -5.48284*10^-9, -7.02789*10^-9}}}
>