Direct calculation of a simple converging integral gives the correct result:

NIntegrate[1/(1 - I x) Exp[-I x], {x, -∞, ∞}]

(* 2.31145 - 1.11022*10^-16 I *)

We can check it:

2. π/E

(* 2.31145 *)

However if I calculate the same integral using a function it fails:

i[y_?NumericQ] = 1/(1 - I y) Exp[-I y];
NIntegrate[i[x], {x, -∞, ∞}]

(* NIntegrate::ncvb: NIntegrate failed to converge to prescribed accuracy after 9
recursive bisections in x near {x} = {6.3887*10^56}. NIntegrate obtained
3.96859 -328.962 I and 245.98686501304076` for the integral and error estimates.

3.96859 - 328.962 I *)

I have tried various methods and options, but was unable to get the correct result.

What is the problem, and how to solve it in the general case?

Update: in real life i[y] represents a black box, which can be evaluated only numerically. Here is a plot of my "real" function for numerical integration: enter image description here

It has the same asymptotic behavior as the simple function in my oversimplified example.

  • 2
    $\begingroup$ The second function cannot be symbolically analyzed by NIntegrate[], so it uses its default algorithm, which is not too good for oscillatory integrals like yours. $\endgroup$ Aug 17, 2017 at 8:11
  • $\begingroup$ And what method should I use? I have already tried all of them... $\endgroup$
    – Dmitry
    Aug 17, 2017 at 8:13
  • 1
    $\begingroup$ In at least this simple case, you could try deforming the contour, but what might work for this toy problem might not work for your actual one. $\endgroup$ Aug 17, 2017 at 12:06
  • $\begingroup$ Do you mean using i[x + 0.001 I] instead of i[x] ? Unfortunately it does not help... $\endgroup$
    – Dmitry
    Aug 17, 2017 at 12:31
  • $\begingroup$ Wow, that's... a lot of wiggles. I'm sure a good contour choice can at least ease the numerics. Let me think about it... $\endgroup$ Aug 19, 2017 at 4:41

2 Answers 2


Yes, my actual black box looks very similar to the example. It also contains oscillating exponent, but the prefactor depends on y in much more complicated way.

-- OP's comment

Taking the comment literally, assuming the exponential factor is known, and assuming the product of the exponential and prefactor is computed numerically, one can use the following approach:

Let i[y] be the numerical integrand. Then divide and multiply by the exponential factor as follows:

i[y_?NumericQ] = 1/(1 - I y) Exp[-I y];  (* OP's numerical integrand *)
i2[y_?NumericQ] := i[y] / Exp[-I y];     (* OP's divided by exponential *)
NIntegrate[i2[x] Exp[-I x], {x, -∞, ∞}]  (* multiply back by exponential *)
(*  2.31145 + 0. I  *)

The symbolic product i2[x] Exp[-I x] can be analyzed symbolically (using the "LevinRule"). The ?NumericQ on i2 prevents the exponential factors from canceling symbolically.

  • $\begingroup$ Thank you! This works perfectly! $\endgroup$
    – Dmitry
    Aug 19, 2017 at 12:20

Try this:

i[y_] := 1/(1 - I y) Exp[-I y];
NIntegrate[i[x], {x, -Infinity, Infinity}]

(*  2.31  *)

Have fun!

  • $\begingroup$ Thank you! Definitely it works, because it is just equivalent to my first example. However, I want to be able to work with numerical function, because in my initial code i[y] is intended to represent some numerical sum. Do you have any other idea? $\endgroup$
    – Dmitry
    Aug 17, 2017 at 9:59
  • 2
    $\begingroup$ @Alexei, he used i[y_?NumericQ] = 1/(1 - I y) Exp[-I y]; as a simple example of a black box (note the _?NumericQ pattern) that NIntegrate[] won't be able to analyze. Basically, pretend you can't actually see the definition, and you can only evaluate the integrand at numerical values. $\endgroup$ Aug 17, 2017 at 13:44
  • 2
    $\begingroup$ @AlexeiBoulbitch The one in the OP might be a minimal working example. For more complex functions that can only be evaluated numerically, the problem would persist. $\endgroup$
    – MarcoB
    Aug 17, 2017 at 16:03
  • 1
    $\begingroup$ Indeed originally I had much more complicated function i[y], which is given by infinite sum with the parameter y. This sum in general case can be evaluated only numerically. Then I want to take some rather complicated integral with this function. I have found, that I can not calculate it despite it is definitely converging. Therefore I oversimplified my problem in order to give a minimal working example. If you help me to solve this one, I am pretty sure, that it will work in my original problem. $\endgroup$
    – Dmitry
    Aug 17, 2017 at 19:54
  • 1
    $\begingroup$ @J.M. Yes, my actual black box looks very similar to the example. It also contains oscillating exponent, but the prefactor depends on y in much more complicated way. Nevertheless it has the same asymptote for large y. $\endgroup$
    – Dmitry
    Aug 18, 2017 at 8:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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