# Find minimum of a function defined via NIntegrate [closed]

I need to find the minimum of a function f(t) = int g(t,x) dx over [0,1]. I used

g[t_, x_] := *huge expression*
f[t_?NumericQ] := NIntegrate[g[t, x], {x, -1, 1}]


as explained in an old thread here, but when I run

FindMinimum[f[t], t \[Element] Interval[{0,1}]]


{0., {t -> 1.}}


which is not the minimum, and indeed this is followed by the message

FindMinimum::fmgz: Encountered a gradient that is effectively zero. The result returned may not be a minimum; it may be a maximum or a saddle point.


Can someone help me? What am I doing wrong? Thanks!

EDIT: I was trying to keep it short (and similar in notation to the old thread), but the full expressions look like

g[t_,q_, u_, OptionsPattern[]] := Module[{k=OptionValue[k],*other parameters*}, *function of t,q,u,parameters*]
Options[g] = {k -> 3,*other default parameters*};
f[t_?NumericQ] := NIntegrate[g[t, q, u], {u, 0, \[Infinity]}, {q, 0, \[Infinity]}, Method -> "QuasiMonteCarlo", MaxPoints -> 10^5, AccuracyGoal -> \[Infinity]];

• 1) Please provide (some version of) the huge expression for NIntegrate. 2) Try using NMinimize (with different methods) and see do you get an answer you expect. – Anton Antonov Dec 25 '20 at 13:31
• Another idea is to use the principal axis method, which does not use derivatives/gradients. E.g. FindMinimum[Abs[2 x - 1] + Abs[x^2 - 1], {x, -2, 2}, Method -> "PrincipalAxis"] – Michael E2 Dec 26 '20 at 16:38

I will use the definition of g given in the answer to question you link to.

g[t_, x_] := t^3 - t + x^2
f[t_?NumericQ] := NIntegrate[g[t, x], {x, -1, 1}]
minPt = With[{min = FindMinimum[f[t], t ∈ Interval[{0, 1}]]}, {min[[2, 1, 2]], min[[1]]}]

{0.57735, -0.103134}

Plot[f[t], {t, 0, 1}, Epilog -> {Red, AbsolutePointSize[8], Point @ minPt}]


This demonstrates that your problem arises from the ill-behavior of your definition of g, and since you won't tell what that is, there is nothing further we can say.

• I was trying to keep it short, the expression is long and I'm having trouble to copy paste it because of formatting issues. The only relevant differences wrt yours are: - I'm using Module[] for default values - I'm using "QuasiMonteCarlo" to integrate it in f[t_] – Davide Venturelli Dec 25 '20 at 18:20
• @DavideVenturelli Monte Carlo methods are not very accurate and that may be part of the problem. You better off estimating the minimum from a plot. Or changing the integration method. – Michael E2 Dec 25 '20 at 18:32
• @MichaelE2 thanks, I will try with another integration method. Actually it turned out much faster than others for producing plots... btw, how can one have Mathematica read off the minimum from a plot automatically? I will have to do so varying some of the other parameters involved, and this would already be a solution:) – Davide Venturelli Dec 25 '20 at 18:35
• @MichaelE2 nope, I tried to change method but unfortunately the problem remained (thanks anyway!) – Davide Venturelli Dec 25 '20 at 18:40
• Max@Cases[ Plot[Sin[20 x]/2 - Cos[x/2], {x, 0, 10}, PlotRange -> All], Line[pts_] :> pts[[All, 2]], Infinity]. It should be accurate to a few digits, but you don't have much control over it. – Michael E2 Dec 25 '20 at 19:04