0
$\begingroup$

I'm trying out the code below and it takes quite a long time to compute and throws eincr and slwcon warnings. Is there a way to speed this up?

phase[u_, m_, \[Theta]_, \[Phi]_] := 
  u^3 + 2 m \[Theta] (3/(4 m))^(2/3) Cos[\[Phi]] u^2 + 
   m \[Theta]^2 (3/(4 m))^(1/3) u - m \[Theta] Cos[\[Phi]];

integrand[u_, 
   m_, \[Theta]_, \[Phi]_] := (-2 (3/(4 m))^(1/3)
       u + \[Theta] ) Exp[-I phase[u, m, \[Theta], \[Phi]]];

integral[\[Theta]_, \[Phi]_, m_, {ulo_, uhi_}] := 
 NIntegrate[integrand[u, m, \[Theta], \[Phi]], {u, ulo, uhi}, 
   MaxRecursion -> 20, WorkingPrecision -> 20] // 
  Timing

integraltab = 
  Monitor[Table[{\[Theta], \[Phi], 
     integral[\[Theta], \[Phi], 100, {-Infinity, Infinity}]}, {\[Theta], 0, 
     Pi, Pi/32}, {\[Phi], 0, 2 Pi, 
     2 Pi/63}], {\[Theta], \[Phi]}];
$\endgroup$
2
  • 2
    $\begingroup$ Do all 2000+ integrals have the same problem? Or can you narrow down the problem to test case one could start with? $\endgroup$ – Michael E2 Aug 20 '20 at 13:13
  • $\begingroup$ @MichaelE2 For the first couple of values of theta, it's fairly fast. After that, it's really slow. $\endgroup$ – 123infinity Aug 20 '20 at 14:06
1
$\begingroup$

The integrand only depends on Cos[\[Phi], that's why you can decrease the integration range to {\[Phi], 0, Pi} (- 50% evaluation time!)!

Include the option Method -> {Automatic, "SymbolicProcessing" -> 0} inside NIntegrate:

integral[\[Theta]_, \[Phi]_, m_, {ulo_, uhi_}] := 
NIntegrate[integrand[u, m, \[Theta], \[Phi]], {u, ulo, uhi} ,MaxRecursion -> 20, WorkingPrecision -> 20 
,Method -> {Automatic, "SymbolicProcessing" -> 0}] 

the grid of 101x101 tablevalues is evaluated in nearly 75s!

integraltab = Monitor[Table[{\[Theta], \[Phi],integral[\[Theta], \[Phi],100, {-Infinity, Infinity}]}
, {\[Theta], 0, Pi,Pi/10}, {\[Phi], 0,  Pi, Pi/10}], {\[Theta], \[Phi]}]; // Timing
(*{74.7813, Null}*)

Without these modifictions the evaluation of 101x101 grid lasts around 1700s ( speedup factor 23 ) !

Hope it helps!

$\endgroup$
3
  • $\begingroup$ Thank you, but I actually need it over 2 Pi since I'll later integrate this over a sphere. $\endgroup$ – 123infinity Aug 21 '20 at 14:20
  • $\begingroup$ No problem, because Cos[2Pi-phi]==Cos[Phi]. $\endgroup$ – Ulrich Neumann Aug 21 '20 at 14:53
  • $\begingroup$ … Without this symmetrie evaluation time still would be 12times faster! $\endgroup$ – Ulrich Neumann Aug 21 '20 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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