How to Minimize an NIntegral

My problem is to minimize in Mathematica a numerical integral. The command


does calculate the mimimum value of Nintegral but it returns a message about non-numerical values for sampling points. Possibly a transfer of ‘a’ into NItegrate is not correct here.
Similarly, the command

fun[r1_?NumberQ, R_?NumberQ, a_?NumberQ] := (R*r1*a)*Sin[r1*a]

does not work.
I would be grateful for your advice how to couple Minimize and NIntegrate commands.




Also note NMinimize[fun2[2.5, a], a] will give a better result, albeit more slowly.
– Daniel Lichtblau
Feb 9 at 0:05



Additionally, your integral is 0 at a=0 — that is why you get messages.
– Anton Antonov
Feb 9 at 0:06



You don’t need NIntegrate at all, Integrate provides an analytic result
– george2079
Feb 9 at 14:35



Thank you all very very much, the solution by Daniel probably helps. I will check it soon for my real problem where instead of this illustrative function under the integral I have a pretty large expression, i.e. a scalar product for 3-dim quantum well. Thank God that the stackexchange exists.
– Marek
Feb 9 at 18:03



I made a community wiki response since this is a common issue and was slightly subtle.
– Daniel Lichtblau
Feb 9 at 18:50


1 Answer


This is a fairly common problem to encounter and in this case was a bit subtle due to having an outer and inner function both in need of being defined only for explicitly numeric input. So I’ll repost my comment as an answer and make it a Community wiki.

The FindMinumum objective itself needs to be defined only for numeric input. Which can be done as follows for example.

fun1[r1_, bigR_, a_?NumberQ] := (bigR*r1*a)*Sin[r1*a]
fun2[bigR_?NumberQ, a_?NumberQ] :=
NIntegrate[fun1[r1, bigR, a], {r1, 1, bigR}]

FindMinimum[fun2[2.5, a], a]

(* Out[49]= {6.74896886521*10^-16, {a -> -7.44151562462*10^-9}} *)

One can do better with NMinimize although it will be slower to run The above was a split-second).

Timing[Minimize[fun2[2.5, a], a]]

(* Out[50]= {3.329455, {-8.91131111427, {a -> -2.46070084014}}} *)



Probably best to edit the extended problem into the original question.
– Daniel Lichtblau
Feb 9 at 21:48



Great. The application to my problem have revealed one problem more. I need to apply this minimization in loop, like Do[Minimize[…],{bigR,bigRmin,bigRmax}]. The following cases go wrong. Why? fun1[r1_, bigR_?NumberQ, a_?NumberQ] := (bigRr1*a)*Sin[r1*a] fun2[bigR_?NumberQ, a_?NumberQ] :=NIntegrate[fun1[r1,bigR,a], {r1,1,bigR}] (*1) With[{R = 1}, Minimize[fun2[R, a], a]] (2) Minimize[(fun2[R, a]) /. {R -> 1}, a] (3) abc = 0; Do[{R = i, abc = abc + Minimize[fun2[R, a], a], Print[abc]}, {i, 1}]
– Marek
Feb 9 at 22:03



(1) That’s a comment, not the original question. (2) It’s not formatted very well either; the code cannot be cut-and-pasted. (3) You are using a different minimization function, one that cannot handle these functions. Check documentation for NMinimize vs. Minimize. Very different animals, those.
– Daniel Lichtblau
Feb 9 at 22:09



Daniel, allow me to leave the recent extension of the problem as it is because my question is about the generalization of your solution to the cases when bigR is taken from any external procedure.
– Marek
Feb 9 at 22:12



Ok, I will work on it. Thank you.
– Marek
Feb 9 at 22:13