0
|
5069 Views
|
3 Replies
|
0 Total Likes
View groups...
Share
GROUPS:

# ?NumericQ for NMaximize NIntegrate ?

Posted 11 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
3 Replies
Sort By:
Posted 11 years ago
 No, it looks like your objective "function" is actually a table of values.
Posted 11 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 11 years ago
 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 conciselyNb[m_?NumericQ] := NIntegrate[5*m, {ebar, 5, 6}]