# NIntegrate::ncvb: NIntegrate failed to converge to prescribed accuracy

The integration is:

NIntegrate[-0.17116940940118283 + 1/(
9.736942322213501 + 7.789553857770802 Cos[q]) + (
0.02866566930866079 (0.5 + 1. Cos[q]) Sin[
q] (-3.0095696738628313 Sqrt[1.25 + 1. Cos[q]]
Cos[0. + ArcTan[(0.5 Sin[q])/(-1 - 0.5 Cos[q])]] +
1. Sin[q]))/(
0.9772727272727273 + 1. Cos[q] - 0.045454545454545456 Cos[2 q] -
0.09090909090909091 Cos[3 q]) + ((0.35586923225834494 +
0.5931153870972414 Cos[q] + 0.11862307741944829 Cos[2 q]) Sin[
0. + ArcTan[(0.5 Sin[q])/(-1 - 0.5 Cos[q])]])/((1.75 +
1. Cos[q] - 0.5 Cos[2 q])^(3/2) Sqrt[
1 - (1. Sin[q]^2)/(
1.75 + 1. Cos[q] - 0.5000000000000001 Cos[2 q])]), {q, -Pi,
Pi}]


. Error message is

NIntegrate::ncvb: NIntegrate failed to converge to prescribed accuracy after 9 recursive bisections in q near {q} = {-3.14159254089972008785892145083358745552559732061581598827615380287}. NIntegrate obtained -1.24910^-16 and 4.588053980254483^-13 for the integral and error estimates.**

How to get the real integration value?

To solve the problem of calculating the value of an integral that is actually $$0$$, you can set a lower AccuracyGoal than the default value (Infinity), as Andrew Moylan mentioned in this previous question here: "When the true value of the integral is zero, the default PrecisionGoal can never be satisfied. You need to set a finite AccuracyGoal in such cases".

With your integrand defined as expr for clarity, the following returns no errors:

NIntegrate[expr, {q, -Pi, Pi}, AccuracyGoal -> 10]

(* Out: 1.66533*10^-16 *)


Also NDSolve as a workaround evaluates to ~0

expr = -0.17116940940118283 +1/(9.736942322213501 +7.789553857770802 Cos[q]) + (0.02866566930866079 (0.5 + 1. Cos[q]) Sin[q] (-3.0095696738628313 Sqrt[1.25 + 1. Cos[q]] Cos[0. + ArcTan[(0.5 Sin[q])/(-1 - 0.5 Cos[q])]] +1. Sin[q]))/(0.9772727272727273 + 1. Cos[q] -0.045454545454545456 Cos[2 q] -0.09090909090909091 Cos[3 q]) + ((0.35586923225834494 +0.5931153870972414 Cos[q] +0.11862307741944829 Cos[2 q]) Sin[0. + ArcTan[(0.5 Sin[q])/(-1 - 0.5 Cos[q])]])/((1.75 +1. Cos[q] - 0.5 Cos[2 q])^(3/2) Sqrt[1 - (1. Sin[q]^2)/(1.75 + 1. Cos[q] -0.5000000000000001 Cos[2 q])])
// Rationalize;

NDSolveValue[{int'[q] == expr, int[-Pi] == 0}, int[Pi], {q, -Pi, Pi},AccuracyGoal -> 15]
(*-3.79922*10^-8*)


indicates a zero integral value!

• Could you please kindly explain, why NDSolve is better than NIntegrate? Dec 14, 2020 at 11:54
• @AlexeiBoulbitch Not better, perhaps the integration routines inside NDSolve are a little bit more robust? In any case NDSolve evaluates without error message. Dec 14, 2020 at 12:22
• Rationalize without the second argument does not fully rationalize the expression. Use expr // Rationalize[#, 0]& // Simplify then NDSolveValue[{int'[q] == expr, int[-Pi] == 0}, int[Pi], {q, -Pi, Pi}, WorkingPrecision -> 40] Dec 14, 2020 at 13:59
• NDSolve seems more "robust" here because the (local) PrecisionGoal and AccuracyGoal are around 8, whereas for NIntegrate, the (global) goals are about 8 and Infinity respectively. (That's why the NDSolve result has an accuracy of only ~7.4). Dec 14, 2020 at 16:26

NIntegrate may have problems if the true value of the integral is zero. The error message quotes this as possible reason for the convergence problem. As a workaround you could add 1 to the integrand and subtract 2Pi in the end like

NIntegrate[-0.17116940940118283 +
1/(9.736942322213501 + 7.789553857770802*Cos[q]) +
(0.02866566930866079*(0.5 + 1.*Cos[q])*
Sin[q]*(-3.0095696738628313*Sqrt[1.25 + 1.*Cos[q]]*
Cos[0. + ArcTan[(0.5*Sin[q])/(-1 - 0.5*Cos[q])]] +
1.*Sin[q]))/(0.9772727272727273 + 1.*Cos[q] -
0.045454545454545456*Cos[2*q] -
0.09090909090909091*Cos[3*q]) +
((0.35586923225834494 + 0.5931153870972414*Cos[q] +
0.11862307741944829*Cos[2*q])*
Sin[0. + ArcTan[(0.5*Sin[q])/(-1 - 0.5*Cos[q])]])/
((1.75 + 1.*Cos[q] - 0.5*Cos[2*q])^(3/2)*
Sqrt[1 - (1.*Sin[q]^2)/(1.75 + 1.*Cos[q] -
0.5000000000000001*Cos[2*q])]) + 1, {q, -Pi, Pi}] - 2*Pi


Edit: I rationalized all numbers and now WorkingPrecision can be set to a higher value:

NIntegrate[-(17116940940118283/100000000000000000) +
1/(9736942322213501/
1000000000000000 + (7789553857770802/1000000000000000)*Cos[q]) +
((2866566930866079/100000000000000000)*(1/2 + Cos[q])*Sin[q]*
((-(30095696738628313/10000000000000000))*Sqrt[5/4 + Cos[q]]*
Cos[ArcTan[((1/2)*Sin[q])/(-1 - (1/2)*Cos[q])]] +
Sin[q]))/
(43/44 + Cos[q] - (1/22)*Cos[2*q] - (1/11)*Cos[3*q]) +
((35586923225834494/
100000000000000000 + (5931153870972414/10000000000000000)*
Cos[q] + (11862307741944829/100000000000000000)*
Cos[2*q])*
Sin[ArcTan[((1/2)*Sin[q])/(-1 - (1/2)*Cos[q])]])/
((7/4 + Cos[q] - (1/2)*Cos[2*q])^(3/2)*
Sqrt[1 - Sin[q]^2/(7/4 + Cos[q] - (1/2)*Cos[2*q])]), {q, -Pi, Pi},
WorkingPrecision -> 30]


Now the precision of the calculation should be higher (15 digits ?) and it completes without complaint. So the real value of the integral as given by the OP is

3.28296365*10^-16


I should probably change what I say up front:

The full error message, perhaps surprisingly, is saying there is nothing to worry about, and no fix is needed.

This is the opposite of most people's reactions to error messages, including mine, especially when the message leads with the word "failed." The point is to think about this part of the error message:

NIntegrate obtained -1.24910*^-16 and 4.588053980254483*^-13 for the integral and error estimates.

That means NIntegrate calculated the integral to be in the interval

{-4.586804880254483*^-13, 4.589303080254483*^-13}


Now, is that a good enough answer?

Probably, it is, unless you believe (for other reasons, say, based on what the value is supposed to represent) that the integral is nonzero and smaller than 10^-13. Probably you don't need to do anything; just accept the answer. On the other hand, if an uncertainty of 4.6*10^-13 is unacceptable, then none of the methods discussed below fix that; they just hide the problem.

The OP's method is better than the NDSolve method, which lies well outside this interval and corresponds to its AccuracyGoal of about 8.

The OP's method is better than the NIntegrate answer obtained by integrating 1 + integrand and subtracting 2 Pi for somewhat technical reasons: The default precision goal is about 6, which means that the error in the value the integral is bounded by 2 Pi 10*^-6, which is much greater than 4.6*10^-13. Further, while the value of the integral in this method (minus 2 Pi) lies inside the interval, it is much larger than the value of the OP's integral.

The OP's method is better than lowering AccuracyGoal. The setting AccuracyGoal -> a means roughly that if the absolute error is less than 10^-a, NIntegrate will accept the result. By lowering AccuracyGoal, you are actually telling NIntegrate to accept a worse result. A good reason to do this is given in one of the answers @MarcoB linked: A lower setting speeds up NIntegrate when the integral is (nearly) zero, because it is easier to compute a less accurate result.

There is nothing very wrong in these other methods. Unless you need more than 8 decimal places of accuracy, which almost certainly is not the case here, they are fine but no better than the OP's method. The error message in this case in fact indicates how good the answer is. In other cases, it might indicate how bad the answer could be.

• the result I get for integrand +1is -6.838973831690964*^-14, well within the error interval of (+-) 4*10^-13 Dec 14, 2020 at 20:42
• @Andreas That is what said: "while the value of the integral (minus 2 Pi) lies inside the interval," Dec 14, 2020 at 20:50
• It's somewhat irrelevant since the error-bound interval estimated by NIntegrate in the 1 + integrand approach is {-6.620418321663166*^-8, 6.620404643715502*^-8}, which is much larger. Dec 14, 2020 at 20:56
• where do you get +-6*10^-8 from Dec 14, 2020 at 21:19
• @Andreas The PrecisionGoal for a 1D NIntegrate is normally 6 (which I misremembered as 8). So the condition that must be met is that the error estimate needs to be lower than 2 Pi * 10^-6 = ±6*10^-6, not 2 Pi * 10^-8` as I was saying. Dec 14, 2020 at 21:58