0
$\begingroup$

I'm trying to optimize three parameters simultaneously using the following code:

(* Input  *)
NN = 4;
energy = 16;
α = 0;
β = 100;
z = 1;


(* Lambda values *)
λ1 = z;
λ2 = (2 energy + λ1^2)/6;
λ3 = (2 λ1*λ2 - α)/6;
λ4 = (4 λ2^2 + 6 λ1 *λ3 - 2 β)/
   20;


(* Find the optimized parameters *)
Clear[ψ, μ, f, Δ];
ψ[λ2_][λ3_][λ4_][r_] := 
  Exp[-r - λ2 r^2 - λ3  r^3 - λ4  r^4];

μ[λ2_][λ3_][λ4_][k_] := 
  NIntegrate[
   r^k ψ[λ2][λ3][λ4][r], {r, 
    0, ∞}];

f[λ2_][λ3_][λ4_][k_] := 
  k (k - 1) μ[λ2][λ3][λ4][k - 1] + 
   2 z μ[λ2][λ3][λ4][k] + 
   2 energy μ[λ2][λ3][λ4][k + 1] - 
   2 α μ[λ2][λ3][λ4][k + 2] - 
   2 β μ[λ2][λ3][λ4][k + 3];

Δ[λ2_][λ3_][λ4_] := 
  Sum[f[λ2][λ3][λ4][k]^2, {k, 1, NN}];

new = FindMinimum[Δ[λ2][λ3][λ4],\
 {λ2, λ3, λ4}] // Quiet;


λ2 = new[[2, 1]][[2]];
λ3 = new[[2, 2]][[2]];
λ4 = new[[2, 3]][[2]];

energy = λ2*3 - 1/2;
Print["New energy  ", energy];

but it returns errors like this

NIntegrate::inumr: The integrand E^(-r-(11 r^3)/6+(17 r^4)/5-r^2 λ2) has evaluated to non-numerical values for all sampling points in the region with boundaries {{∞,0.}}.

1- It is obvious that the integration procedure causes the errors, but I don't know how should I fix them?

2- Is my code to optimize three parameters true? (provided that I add a loop to my code) or does exist a more efficient way to do so?

$\endgroup$
5
  • $\begingroup$ Also related: (a135026), (a160052), (q201657) $\endgroup$
    – Michael E2
    Commented Nov 24, 2019 at 15:53
  • $\begingroup$ Thanks all, but if you note, the integration was not just my problem, I was looking for a method for optimizing three parameters too. $\endgroup$
    – Wisdom
    Commented Nov 24, 2019 at 15:55
  • $\begingroup$ So the first two related links in my comment are in fact duplicates, too? $\endgroup$
    – Michael E2
    Commented Nov 24, 2019 at 16:10
  • $\begingroup$ I don't know, maybe $\endgroup$
    – Wisdom
    Commented Nov 24, 2019 at 16:17
  • $\begingroup$ The parameters λ2, λ3, λ4 over which you're optimizing lack the NumericQ protection that lead to the NIntegrate errors. This is the same problem that the linked duplicate (q22210) has, except for optimizing over two vector parameters instead of three. (Is two instead of three significant? Are vectors?) The related two Q&A have similar problems to yours, but those Qs were challenged by the notion of a minimal working example, and I didn't think they were good examples, compared with (q22210). They could be added to the list of dupes. $\endgroup$
    – Michael E2
    Commented Nov 24, 2019 at 16:41

1 Answer 1

2
$\begingroup$

Not completelly understood your computations, but this is my try. First, let me simplify a bit.

NN = 4;
energy = 16;
α = 0;
β = 100;
z = 1;

λ1 = z
λ2 = (2 energy + λ1^2)/6
λ3 = (2 λ1*λ2 - α)/6
λ4 = -(4 λ2^2 + 6 λ1*λ3 - 2 β)/20 (* changed sign here, otherwise the integral does not converge *)

Clear[ψ, μ, f, Δ];
ψ[λ2_?NumericQ, λ3_?NumericQ, λ4_?NumericQ, r_] := 
  Exp[-r - λ2 r^2 - λ3 r^3 - λ4 r^4];

μ[λ2_?NumericQ, λ3_?NumericQ, λ4_?NumericQ, k_] := 
  NIntegrate[r^k ψ[λ2, λ3, λ4, r], {r, 0, ∞}];

f[λ2_?NumericQ, λ3_?NumericQ, λ4_?NumericQ, k_] :=
 k (k - 1) μ[λ2, λ3, λ4, k - 1] + 2 z μ[λ2, λ3, λ4, k] + 
   2 energy μ[λ2, λ3, λ4, k + 1] - 
   2 α μ[λ2, λ3, λ4, k + 2] - 
   2 β μ[λ2, λ3, λ4, k + 3];

Δ[λ2_?NumericQ, λ3_?NumericQ, λ4_?NumericQ] := 
  Sum[f[λ2, λ3, λ4, k]^2, {k, 1, NN}];

min = NMinimize[{Δ[x2, x3, x4], x2 > 0 && x3 > 0 && x4 > 0}, {x2, x3, x4}]

{0.0000396903, {x2 -> 6.71696, x3 -> 0.000223156, x4 -> 0.000143381}}

And new energy:

newEnergy = (3 x2 - 1/2) /. min[[2]]

19.6509

$\endgroup$
5
  • $\begingroup$ Thanks a lot. However the running time is so long (I waited for 10 min but it didn't finish), right? $\endgroup$
    – Wisdom
    Commented Nov 24, 2019 at 13:30
  • $\begingroup$ Yes, it takes several minutes on my machine too. $\endgroup$
    – Alx
    Commented Nov 24, 2019 at 14:06
  • $\begingroup$ OK. you're right, it finished finally in my laptop. 1- Please explain about the modifications you made to my code. specially what does ?NumericQcommand do and how it fixes the integration error? 2- I have to add a loop to my code, namely I have to use new values of λ2, λ3, λ4 in the next loop, How can I address this? $\endgroup$
    – Wisdom
    Commented Nov 24, 2019 at 14:17
  • $\begingroup$ ?NumericQ means that a function will work only with numerical argument, it is often used with numerical functions: NIntegrate, NMinimize require numerical imput. NINtegrate will not work if not provided with numerical coefficients except a variable of integration, see examples in help. Initial values of λ2, λ3, λ4 are not used in further computations, those λ in functions' definitions are dummy variables, and you see I used x in minimization to not confuse with initial λ. NMinimize finds global minimum for λ2, λ3, λ4. $\endgroup$
    – Alx
    Commented Nov 24, 2019 at 15:31
  • $\begingroup$ Thanks. In fact I have to use the new value of energy to get new lambdas and repeat the calculations and obtain new energy again and so on $\endgroup$
    – Wisdom
    Commented Nov 24, 2019 at 15:43

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