0
$\begingroup$

This question already has an answer here:

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[2]

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.

$\endgroup$

marked as duplicate by Michael E2 numerical-integration May 21 '18 at 16:11

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ 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. $\endgroup$ – Michael E2 May 21 '18 at 12:19
  • $\begingroup$ For me, topt[2] returns an answer (after a few minutes). The messages, strictly speaking, are just warnings, not errors. $\endgroup$ – Michael E2 May 21 '18 at 12:24
  • $\begingroup$ In particular, this answer: mathematica.stackexchange.com/questions/18393/… $\endgroup$ – Michael E2 May 21 '18 at 16:12
3
$\begingroup$

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[2]

(*   {1.15071, {tau -> 0.64901}}   *)
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.