# Numerically integrating a highly oscillatory function

I'm trying to evaluate

NIntegrate[Sin[x] Exp[I m (x + Cos[x] + Sin[x])], {x, 1, 100}]

with m=1000. The problem is that it's highly oscillatory. What would be the best method to do this integral?

• Add this:Method -> "LocalAdaptive", MaxRecursion -> 100 to NIntegrate. – Mariusz Iwaniuk May 23 at 14:51
• @MariuszIwaniuk This is still giving me errors for some reason. – 123infinity May 23 at 14:59
• I use Mathematica 12.1 and for me works fine. – Mariusz Iwaniuk May 23 at 15:02

This works fast and no warnings

ClearAll[x, m];
m = 100;
NIntegrate[Sin[x] Exp[I m (x + Cos[x] + Sin[x])], {x, 1, 100}, MinRecursion -> 3] (actually MinRecursion -> 2 works also, as well as 1. but 3 looks more cool since it is odd number.)

• This worked! Thank you so much! – 123infinity May 23 at 14:59

If m is an integer, then the integrand is periodic and the trapezoidal rule is a good way to go, except for the leftover bit when the length of the interval is not an exact multiple of the period.

i1 = Block[{m = 1000},
NIntegrate[Sin[x] Exp[I m (x + Cos[x] + Sin[x])],
{x, 1, 1 + 2 Pi},   (* any period will do *)
Method -> "Trapezoidal",
MaxRecursion -> Round[1 + 1.31 Log[m]]] (* theoretically O[Log[m]] *)
];

periods = Round[(100 - 1)/(2 Pi)];

i2 = Block[{m = 1000},
NIntegrate[
Sin[x] Exp[I m (x + Cos[x] + Sin[x])],
{x, 1 + periods*2 Pi, 100},
Method -> {
"GaussKronrodRule",
"Points" -> 41}] (* more recursion or more points for larger m *)
];

periods*i1 + i2
(*  1.24527 + 0.239249 I  *)