Message Boards Message Boards

?NumericQ for NMaximize NIntegrate ?

Posted 10 years ago
Hi,

Can you tell me what is wrong with it ?
Clear[Nb]
Nb[m_?NumericQ] :=
  Table[NIntegrate[i*m, {ebar, i, (i + 1)}], {i, 1., 10}];

NMaximize[{Nb[m], 800 <= m <= 1200}, {m}]

I have the following error :

NMaximize::nnum: The function value {-818.293,-1636.59,-2454.88,-3273.17,-4091.46,-4909.76,-5728.05,-6546.34,-7364.63,-8182.93}
is not a number at {m} = {818.293}. >>

I guess the problem comes from ?NumericQ, but I must put it (or the Integrand is non numerical for NMaximize)

Thank you,
Mich
POSTED BY: Thibault Michel
3 Replies
No, it looks like your objective "function" is actually a table of values.
POSTED BY: Daniel Lichtblau
Posted 10 years ago
 Clear[Nb]
 int[m_, i_] := NIntegrate[m*i, {x, i, i + 1}]
 Nb[m_?NumericQ] := Table[int[m, i], {i, 1., 10}];
 NMaximize[{Nb[m][[5]], 800 <= m <= 1200}, {m}]
 
 
 Part::partw: Part 5 of Nb[m] does not exist. >>
 
 Part::partw: Part 5 of Nb[m] does not exist. >>

Part::partw: Part 5 of Nb[m] does not exist. >>

General::stop: Further output of Part::partw will be suppressed during this calculation. >>
what about now ?
POSTED BY: Thibault Michel
This is still an order of evaluation issue. Nb[[5]] evaluates before Nb itself does.

To keep this simple, first write a function that takes in a number and outputs a single number.
Then feed this to NMaximize.

You could for example define Nb like this:
Nb[m_?NumericQ] := Table[NIntegrate[i*m, {ebar, i, (i + 1)}], {i, 1., 10}][[5]]

Or more concisely

Nb[m_?NumericQ] := NIntegrate[5*m, {ebar, 5, 6}]
POSTED BY: Sean Clarke
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract