Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #8480
| From | Bob Hanlon <hanlonr357@gmail.com> |
|---|---|
| Newsgroups | comp.soft-sys.math.mathematica |
| Subject | Re: Baffled by FindMinimum |
| Date | 2012-02-11 20:50 +0000 |
| Organization | Steven M. Christensen and Associates, Inc and MathTensor, Inc. |
| Message-ID | <jh6kbb$jsf$1@smc.vnet.net> (permalink) |
| 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@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}}}
>
> Many thanks in advance,
>
> Sam
Back to comp.soft-sys.math.mathematica | Previous | Next — Next in thread | Find similar | Unroll thread
Re: Baffled by FindMinimum Bob Hanlon <hanlonr357@gmail.com> - 2012-02-11 20:50 +0000 Re: Baffled by FindMinimum Sam Takoy <sam.takoy@yahoo.com> - 2012-02-12 10:00 +0000
csiph-web