I have a problem with a correct transfer of values of parameters when NIntegrate (and some subsequent functions) are coupled with minimizing procedures in Mathematica. Namely, let's define an operator:
V[r_,R_] := If[r < R, 0., 1.]
H[r_, R_] := m* D[#, r] + V[r, R]*# &;
which acts on a function (the function below is only an example but their nested form and 'ifs', which are important to me, would be possibly crucial)
X[R_, b_?NumberQ] := -b / Sin[b*R];
F1[r_, R_, b_?NumberQ] := If[r<=R, Sin[b*r] / r, Exp[-X[R, b]*r] / r]
F[r_, R_, b_?NumericQ] := F1[r, R, b]
m = 1.0;
I try to minimize this numerical integral (the best method seems to be 'FindMinValue' because initial value of minimizing parameter can be specified by hand). The two commands below do calculate it but non-numerical values are indicated in messages; how to avoid them?
(*1*) FindMinValue[NIntegrate[F[r,2.5,b]*H[r, 2.5]F[r,2.5,b]],{r,1,2.5}], {b,1.0}] (* R=2.5 is set here by hand *)
(*2*) With[{R=2.5},FindMinValue[NIntegrate[F[r,R,b]*H[r, R][F[r,R,b]],{r,1,R}],{b, 1.0}]]
But the following does not return any messages, surprisingly. What is the difference in transfer of 'R' into 'FindMinValue' here and in those previous cases? I cannot see a general rule.
(*3*) Do[{R = 2.5*i,abc = FindMinValue[NIntegrate[F[r,R,b]*H[r, R][F[r,R,b]],{r,1,R}],{b,1.0}],Print[abc]},{i,2}]
I would be grateful for help.
Ffunction is useless here, sinceF1is already defined to evaluate only on numerical input; indeed if you swapF1forFin your code you obtain the exact same results. 2) You had a missing opening bracket in (1):H[r, 2.5] F[r, 2.5, b]]should beH[r, 2.5][F[r, 2.5, b]], i.e.Fis the argument toH`.