# Function with numerical integrals, Plot3D works but NMaximize doesn't [duplicate]

I have the following problem: I have a function enter code hereH depending on two numerical integrals g and d, Mathematica is able to draw the 3D-Plot of the function H, but it isn't able to compute NMaximize.

I know that a common problem is the argument of a function, so I tried to be as precise as possible:

g[s_, eps_, tau_] :=
NIntegrate[
x^(s - 2) Exp[-x] (1 - Cos[x tau]) Coth[x/(2 eps)], {x, 0,
Infinity}]

d[s_, eps_, tau_] :=
NIntegrate[
x^(s - 1) Exp[-x]/
2 (1 - Cos[tau x])/(1 - (Cosh[x/(2 eps )])^2), {x, 0,
Infinity}]

H[s_, eps_, tau_, d_, g_] := (2*(d)^2)/(Exp[2*g] - 1)


Mathematica doen't give me any problem with the Plot3D of H

With[{s = 1},
Plot3D[H[s, eps, tau, d[s, eps,tau],
g[s, eps, tau]] , {eps, 1, 15}, {tau, 0.1, 1},
PlotRange -> All, AxesLabel -> Automatic ]]


But when I try to find which values of tau do maximize H, it says that NIntegrate has evaluated g to non-numerical values (NIntegrate::inumr message). Here's my code:

topt[eps_] :=
With[{s = 1},
NMaximize[
H[s, eps, tau, d[s, eps, tau],
g[s, eps, tau]] , {tau} ∈
Interval[{0.2, 1}] ] ]

topt


I don't get why in the Plot3D there is no problem with the numeric integration while using NMaximize Mathematica isn't able to calculate it. I looked for errors in the syntax, but I couldn't find any.

## marked as duplicate by Michael E2 numerical-integration StackExchange.ready(function() { if (StackExchange.options.isMobile) return; $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() { var$hover = $(this).addClass('hover-bound'),$msg = $hover.siblings('.dupe-hammer-message');$hover.hover( function() { $hover.showInfoMessage('', { messageElement:$msg.clone().show(), transient: false, position: { my: 'bottom left', at: 'top center', offsetTop: -7 }, dismissable: false, relativeToBody: true }); }, function() { StackExchange.helpers.removeMessages(); } ); }); }); May 21 '18 at 16:11

• Look up the Attributes of Plot3D and NMaximize. Plot3D is HoldAll and NMaximize is not. The difference means that H[..] is evaluated before it is passed to NMaximize, at which point tau is a symbol, not a number. For Plot3D, evaluation is "held" (not done) and the code is passed to Plot3D as is; it is not evaluated until eps and tau have been assigned numeric values. – Michael E2 May 21 '18 at 12:19
• For me, topt returns an answer (after a few minutes). The messages, strictly speaking, are just warnings, not errors. – Michael E2 May 21 '18 at 12:24
• In particular, this answer: mathematica.stackexchange.com/questions/18393/… – Michael E2 May 21 '18 at 16:12

Use _?NumericQ in function definiton.

g[s_?NumericQ, eps_?NumericQ, tau_?NumericQ] :=
NIntegrate[
x^(s - 2) Exp[-x] (1 - Cos[x tau]) Coth[x/(2 eps)], {x, 0,   Infinity}]

d[s_?NumericQ, eps_?NumericQ, tau_?NumericQ] :=
NIntegrate[
x^(s - 1) Exp[-x]/2 (1 - Cos[tau x])/(1 - (Cosh[x/(2 eps)])^2), {x,
0, Infinity}]

H[s_?NumericQ, eps_?NumericQ, tau_?NumericQ, d_?NumericQ,
g_?NumericQ] := (2*(d)^2)/(Exp[2*g] - 1)

topt[eps_] :=
With[{s = 1},
NMaximize[{H[s, eps, tau, d[s, eps, tau], g[s, eps, tau]], .2 <
tau < 1}, tau]]

topt

(*   {1.15071, {tau -> 0.64901}}   *)