From: Neil Broderick on
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: Bill Rowe on
On 4/22/10 at 6:43 AM, ngb(a)ecs.soton.ac.uk (Neil Broderick) 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}]

<error messages snipped>

Even if this did work as you expected with no error messages,
this would be a very inefficient way to solve the problem.
NMinimize works by repeatedly evaluating the expression to be
minimized. That means the numerical integration problem is
repeatedly solved. In this case, there is a symbolic result for
the integral. So doing:

In[4]:= int = Integrate[Sin[x], {x, -a, b}]

Out[4]= cos(a)-cos(b)


And this clearly is zero for Abs[a]==Abs[b]. So, any pair of
numbers satisfying this last will be a minimum for the square of
the absolute value for the integral.

But if you didn't want to find the minimum by inspection, then doing

In[5]:= NMinimize[Abs[int], {a, b}]

Out[5]= {4.80727*10^-14,{a->0.0961182,b->0.0961182}}

returns one of many possible solutions. Note, I didn't bother
with squaring the absolute value since minimizing the absolute
value is the same as minimizing the square of the absolute value.


From: Bob Hanlon on

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: Ingolf Dahl on
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
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: Bob Hanlon on

\[Beta][coeffs : {_?NumericQ ..}, z_?NumericQ] :=
coeffs[[1]] + Sum[coeffs[[i]] ChebyshevT[i, z],
{i, 2, Length[coeffs]}];


Bob Hanlon

---- Neil Broderick <ngb(a)ecs.soton.ac.uk> wrote:

=============
Bob,
thanks for that. It is neater than my work around was that I defined the function recursively if the arguements weren't
numeric and then set the recursion limit to 2 to get it to halt. Do you know how I would handle a variable
number of arguments? Currently I have a function

\[Beta][coeffs_, z_] := coeffs[[1]] + Sum[coeffs[[i]] ChebyshevT[i, z], {i, 2, Length[coeffs]}];

and would like to be able minimise a function involving beta for an arbitrary number of arguments. So how
does the NumericQ trick work on a list of variables?

cheers,
Neil

On 22 Apr 2010, at 13:26, Bob Hanlon wrote:

>
> 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=