From: Alexey Popkov on

"Ingolf Dahl" <ingolf.dahl(a)physics.gu.se> �������/�������� � ��������
���������: news:hqu8ai$rol$1(a)smc.vnet.net...
> Is there any serious Mathematica user in this forum, which has not
> stumbled
> on this problem one or several times, when NMinimize, or some other
> function, tries to evaluate a numeric function symbolically first, before
> inserting the numbers? Sometimes it would be nice to have an option,
> SymbolicEvaluation->False, which could be set for NMinimize in these
> cases.
> It is not always so convenient to have to define an extra function just to
> take care of this. I think there is a whole group of commands acting
> similarly to NMinimize.
>
> Ingolf Dahl
> Sweden

I totally agree with you. I do not see anything reasonable in that obviously
numerical function (for example, NMinimize, FindMinimum, etc.) is trying for
some reason to perform symbolic computation.



From: Joe Hays on
I agree with Ingolf.

On Sat, Apr 24, 2010 at 4:01 AM, Ingolf Dahl <ingolf.dahl(a)physics.gu.se>wrote:

> Is there any serious Mathematica user in this forum, which has not stumbled
> on this problem one or several times, when NMinimize, or some other
> function, tries to evaluate a numeric function symbolically first, before
> inserting the numbers? Sometimes it would be nice to have an option,
> SymbolicEvaluation->False, which could be set for NMinimize in these cases.
> It is not always so convenient to have to define an extra function just to
> take care of this. I think there is a whole group of commands acting
> similarly to NMinimize.
>
> Ingolf Dahl
> Sweden
>
>
>
> -----Original Message-----
> From: Bob Hanlon [mailto:hanlonr(a)cox.net]
> Sent: den 23 april 2010 09:51
> To: mathgroup(a)smc.vnet.net
> Subject: Re: problems with NMinimize
>
>
> Define a function that only evaluates for numeric arguments
>
> f[a_?NumericQ, b_?NumericQ] :=
> Abs[NIntegrate[Sin[x], {x, -a, b}]]^2
>
> NMinimize[f[a, b], {a, b}]
>
> {7.56317*10^-19,{a->0.131888,b->-0.131888}}
>
>
> Bob Hanlon
>
> ---- Neil Broderick <ngb(a)ecs.soton.ac.uk> wrote:
>
> =============
> Hi,
> I am trying to use NMinimize to find the solutions to various numerical
> equations and I keep getting error
> messages concerning non-numerical values. For example consider the
> following:
>
> In[2]:= NMinimize[Abs[NIntegrate[Sin[x], {x, -a, b}]]^2, {a, b}]
>
> During evaluation of In[2]:= NIntegrate::nlim: x = -1. a is not a valid
> limit of integration. >>
>
> During evaluation of In[2]:= NIntegrate::nlim: x = -1. a is not a valid
> limit of integration. >>
>
> During evaluation of In[2]:= NIntegrate::nlim: x = -1. a is not a valid
> limit of integration. >>
>
> During evaluation of In[2]:= General::stop: Further output of
> NIntegrate::nlim will be suppressed during this calculation. >>
>
> Out[2]= {5.08978*10^-24, {a -> 0.0994414, b -> 0.0994414}}
>
> My actual problem involves taking Fourier transforms of lists of numbers
> but
> you get the picture. Why is
> NMinimize putting variables into the function rather than just numbers and
> is the likely to cause a problem
> in some cases?
>
> regards,
> Neil=
>
>
From: Anh Ngoc LAI on
Hi,

One way to solve this problem (if i understand your problem) is to define your function so that only evaluates when its argument is numeric, as follow:

f(a_?NumericQ,b_?NumericQ, ...) == ...

LAI.

--- On Sun, 4/25/10, Alexey Popkov <lehin.p(a)gmail.com> wrote:

From: Alexey Popkov <lehin.p(a)gmail.com>
Subject: Re: problems with NMinimize
Date: Sunday, April 25, 2010, 6:25 AM


"Ingolf Dahl" <ingolf.dahl(a)physics.gu.se> =D1=81=D0=BE=D0=BE=D0=B1=D1=89=D0=
=B8=D0=BB/=D1=81=D0=BE=D0=BE=D0=B1=D1=89=D0=B8=D0=BB=D0=B0 =D0=B2 =D0=BD=D0=
=BE=D0=B2=D0=BE=D1=81=D1=82=D1=8F=D1=85
=D1=81=D0=BB=D0=B5=D0=B4=D1=83=D1=8E=D1=89=D0=B5=D0=B5: news:hqu8ai$rol$1@s=
mc.vnet.net...
> Is there any serious Mathematica user in this forum, which has not
> stumbled
> on this problem one or several times, when NMinimize, or some other
> function, tries to evaluate a numeric function symbolically first, before
> inserting the numbers? Sometimes it would be nice to have an option,
> SymbolicEvaluation->False, which could be set for NMinimize in these
> cases.
> It is not always so convenient to have to define an extra function just to
> take care of this. I think there is a whole group of commands acting
> similarly to NMinimize.
>
> Ingolf Dahl
> Sweden

I totally agree with you. I do not see anything reasonable in that obviously
numerical function (for example, NMinimize, FindMinimum, etc.) is trying for
some reason to perform symbolic computation.
From: Ingolf Dahl on
Best LAI,
Yes, if you followed the thread back, you see that we also have understood
that we might introduce an extra function f(a_?NumericQ,b_?NumericQ, ...),
but we (I guess I can include Alexey Popkov and Joe Hays in "we") sometimes
find that way clumsy. If we ask Mathematica to evaluate NMinimize, this
function is described to "minimizes f numerically with respect to x." But it
does not only this: it also first tries to evaluate f symbolically. I find
it reasonable that there should be a way to opt-out from this symbolic
evaluation. It would be convenient to have an option to NMinimize for this
purpose.

Ingolf Dahl
Sweden

-----Original Message-----
From: Anh Ngoc LAI [mailto:laianhngoc(a)yahoo.com]
Sent: den 26 april 2010 10:50
Subject: Re: problems with NMinimize

Hi,

One way to solve this problem (if i understand your problem) is to define
your function so that only evaluates when its argument is numeric, as
follow:

f(a_?NumericQ,b_?NumericQ, ...) == ...

LAI.

--- On Sun, 4/25/10, Alexey Popkov <lehin.p(a)gmail.com> wrote:

From: Alexey Popkov <lehin.p(a)gmail.com>
Subject: Re: problems with NMinimize
Date: Sunday, April 25, 2010, 6:25 AM


"Ingolf Dahl" <ingolf.dahl(a)physics.gu.se> =D1=81=D0=BE=D0=BE=D0=B1=D1=89=D0=
=B8=D0=BB/=D1=81=D0=BE=D0=BE=D0=B1=D1=89=D0=B8=D0=BB=D0=B0 =D0=B2 =D0=BD=D0=
=BE=D0=B2=D0=BE=D1=81=D1=82=D1=8F=D1=85
=D1=81=D0=BB=D0=B5=D0=B4=D1=83=D1=8E=D1=89=D0=B5=D0=B5: news:hqu8ai$rol$1@s=
mc.vnet.net...
> Is there any serious Mathematica user in this forum, which has not
> stumbled
> on this problem one or several times, when NMinimize, or some other
> function, tries to evaluate a numeric function symbolically first, before
> inserting the numbers? Sometimes it would be nice to have an option,
> SymbolicEvaluation->False, which could be set for NMinimize in these
> cases.
> It is not always so convenient to have to define an extra function just to
> take care of this. I think there is a whole group of commands acting
> similarly to NMinimize.
>
> Ingolf Dahl
> Sweden

I totally agree with you. I do not see anything reasonable in that obviously
numerical function (for example, NMinimize, FindMinimum, etc.) is trying for
some reason to perform symbolic computation.


From: Ingolf Dahl on
Try with



f[myVec_?(VectorQ[#,NumericQ]&), myParam_, myModel_] := < function
definition>



instead of your condition test at the end. The parenthesis around
VectorQ[#,NumericQ]& is essential. I have tested with



func[v_?(VectorQ[#, NumericQ] &)] := Mean[v]



and that seems to work.

Still I would like to be able to say to NMininize directly: "I understand
that you do symbolic evaluation of f behind my back with the very best
intentions, but this time I prefer to say NO to this - you should not even
try."



Best regards



Ingolf Dahl



From: Joe Hays [mailto:hays.joe(a)gmail.com]
Sent: den 26 april 2010 15:34
To: Ingolf Dahl
Cc: mathgroup(a)smc.vnet.net
Subject: Re: Re: problems with NMinimize



So, on this topic I'd like to ask the group for some pointers...



First Observation:

I generally have a vector function that I need to pass to NMinimize and/or
FindMinimum. I have not had success with either of the following
definitions,



f(myVec_?NumericQ):= Module[{}, <body of function definition>]



nor,



f(myVec_?VectorQ):= Module[{}, <body of function definition>]



the only thing I have found to work is,



f(myVec_):= Module[{}, <body of function
definition>]/;VectorQ[myVec,NumericQ]



This was very difficult to find, and as I recall, I didn't find it in the
documentation but in a forum posting. I hope the Wolfram folks consider
adding an example when dealing with a vector valued function in their
documentation.



Now, a question:

Not only do I have a vector valued function to be minimized but I also have
additional arguments that I need to pass to the function for proper
evaluation. So, if I make the following definition,



f(myVec_, myParam_, myModel_):= Module[{}, <body of function
definition>]/;VectorQ[myVec,NumericQ]



where myParam is just a constant and myModel is a sybolic set of equations.
If I then pass this function definition to NMinimize/FindMinimum by,



myParam = <some Real constant>;

myModel = <some symbolic equation>;

NMinimize[ f[myVec, myParam, myModel], myVec ]



will Mathematica try to symbolically evaluate myModel?



Sorry, this is likely a Rookie question but I've had really poor performance
with both NMinimize and FindMinimum with multiple problems I've tried with
it. I'm trying to find reasons as to why this performance is so poor. It's
likely due to my poor Mathematica code (in the body), or my formulation,
however, I have been curious about this specific question for some time now.
So, if anyone has more insight to this mechanism of bypassing symbolic
evaluations (or not) your comments.



Joe



On Mon, Apr 26, 2010 at 7:32 AM, Ingolf Dahl <
<mailto:ingolf.dahl(a)telia.com> ingolf.dahl(a)telia.com> wrote:

Best LAI,
Yes, if you followed the thread back, you see that we also have understood
that we might introduce an extra function f(a_?NumericQ,b_?NumericQ, ...),
but we (I guess I can include Alexey Popkov and Joe Hays in "we") sometimes
find that way clumsy. If we ask Mathematica to evaluate NMinimize, this
function is described to "minimizes f numerically with respect to x." But it
does not only this: it also first tries to evaluate f symbolically. I find
it reasonable that there should be a way to opt-out from this symbolic
evaluation. It would be convenient to have an option to NMinimize for this
purpose.


Ingolf Dahl
Sweden

-----Original Message-----

From: Anh Ngoc LAI [mailto: <mailto:laianhngoc(a)yahoo.com>
laianhngoc(a)yahoo.com]
Sent: den 26 april 2010 10:50
To: <mailto:mathgroup(a)smc.vnet.net> mathgroup(a)smc.vnet.net

Subject: Re: problems with NMinimize

Hi,

One way to solve this problem (if i understand your problem) is to define
your function so that only evaluates when its argument is numeric, as
follow:

f(a_?NumericQ,b_?NumericQ, ...) == ...

LAI.

--- On Sun, 4/25/10, Alexey Popkov < <mailto:lehin.p(a)gmail.com>
lehin.p(a)gmail.com> wrote:

From: Alexey Popkov < <mailto:lehin.p(a)gmail.com> lehin.p(a)gmail.com>

Subject: Re: problems with NMinimize

To: <mailto:mathgroup(a)smc.vnet.net> mathgroup(a)smc.vnet.net
Date: Sunday, April 25, 2010, 6:25 AM


"Ingolf Dahl" < <mailto:ingolf.dahl(a)physics.gu.se>
ingolf.dahl(a)physics.gu.se>

> Is there any serious Mathematica user in this forum, which has not
> stumbled
> on this problem one or several times, when NMinimize, or some other
> function, tries to evaluate a numeric function symbolically first, before
> inserting the numbers? Sometimes it would be nice to have an option,
> SymbolicEvaluation->False, which could be set for NMinimize in these
> cases.
> It is not always so convenient to have to define an extra function just to
> take care of this. I think there is a whole group of commands acting
> similarly to NMinimize.
>
> Ingolf Dahl
> Sweden

I totally agree with you. I do not see anything reasonable in that obviously
numerical function (for example, NMinimize, FindMinimum, etc.) is trying for
some reason to perform symbolic computation.