2

I wish to minimize following equation with respect to variables $h,p,q,t$:

$\qquad f(h,p,q,t)=\int_0^1 \int_0^1 \frac{dx\,dy}{\sqrt{h^2+x^2+y^2-2\,x\,y\space cos(p)+2\,h\,y\,cos(q)-2\,h\,x\,cos(t)}}$.

If I try to integrate this double integral using Mathematica it reduces it to a single integral:

$\qquad f(h,p,q,t)=- \int_o^1 \ln\big[-x \, cos(p)+h \, cos(q)+ \sqrt{h^2 +x^2-2\,h\,x \,cos(t)}\,\big] dx \\ \qquad + \int_0^1 \ln \big [1-x\, cos(p)+h\,cos(q)+ \sqrt{1+h^2+x^2-2\,x\,cos(q) - 2\,h\,x\,cos(t)} \big] dx $

Any further operation using Integrate does not work. So, I tried to use NIntegrate instead. NIntegrate doesn't work if you have have variables in the integrand that are not a variable of integration. So, the way out is this:

I created the integral as a function of the variables that don't participate in the integral.

 FUNC[p_, q_, h_, t_] := 
   NIntegrate[Log[-x Cos[p] + h Cos[q] + Sqrt[h^2 + x^2 - 2 h x Cos[t]]], {x, 0, 1}]

Now FUNC[Pi/3, Pi/2, 6, Pi/6] gives me 1.66971, which is great. But I really need the integral to run a minimization on the variables h,p,q, and t.

So, I tried this next:

NMinimize[
  {FUNC[p, q, h, t], {-Pi <= p <= Pi, -Pi <= q <= Pi, h >= 0, -Pi <= t <= Pi}}, 
  {p, q, h, t}]

And I get errors.

Is my entire approach wrong? Or it is partially wrong? Any feedback is most welcome.

Solution

FUNC[(p_)?NumericQ, (q_)?NumericQ, (h_)?NumericQ, (t_)?NumericQ] := 
   NIntegrate[
    If[(-x)*Cos[p] + h*Cos[q] + Sqrt[h^2 + x^2 - 2*h*x*Cos[t]] > 0, 
      Log[(-x)*Cos[p] + h*Cos[q] + Sqrt[h^2 + x^2 - 2*h*x*Cos[t]]]], 
    {x, 0, 1}];
NMinimize[
  {Re[FUNC[p, q, h, t]], {-Pi <= p <= Pi, -Pi <= q <= Pi, h >= 0, -Pi <= t <= Pi}}, 
  {p, q, h, t}]

Mathematica result:

{-10.5339, {p -> -0.0248342, q -> 2.54783, h -> 0.0265091, t -> -2.523}}

This NMinimize makes sense to me. I'm minimizing energy here and I didn't incorporated an negative sign and this the minimized result is negative.

  • how about this? ClearAll[FUNC]; FUNC[(p_)?NumericQ, (q_)?NumericQ, (h_)?NumericQ, (t_)?NumericQ] := NIntegrate[ If[(-x)*Cos[p] + h*Cos[q] + Sqrt[h^2 + x^2 - 2*h*x* Cos[t]] > 0, Log[(-x)*Cos[p] + h*Cos[q] + Sqrt[h^2 + x^2 - 2*h*x* Cos[t]]]], {x, 0, 1}]; NMinimize[{FUNC[p, q, h, t], {-Pi <= p <= Pi, -Pi <= q <= Pi, h >= 0, -Pi <= t <= Pi}}, {p, q, h, t}] Type Checking and Singularity Elimination. – Xminer Apr 07 '19 at 07:16
  • 1)Perhaps there is a typo in the original expression $f[h,p,q,t]$, check $-hc\cos {(t)}$ it should be $-hx\cos {(t)}$; 2)the integral is expressed as a ConditionalExpression with many constraints; 3)To avoid mistakes we need to minimize Re[FUNC]. Is it possible to immediately put a restriction on $f(h,p,q,t)$, so that Im[f]=0? – Alex Trounev Apr 07 '19 at 14:09
  • Xminer: Thanks for the ?Numeric and IF statement trick. This got some of the errors fixed. Alex: Thanks for catching the typo. I'll fix it. Also, thanks for the suggestion for minimizing real valued portion of the function. It works well now. Thanks to both of you for your help. :) – the_village_kid Apr 07 '19 at 23:46
  • Further discussion: https://mathematica.stackexchange.com/questions/18393/what-are-the-most-common-pitfalls-awaiting-new-users/26037#26037 – Michael E2 Apr 08 '19 at 00:56

0 Answers0