# Different results by Integrate and NIntegrate

While trying to compute winding number, i find that Integrate and NIntegrate are giving apparently different answers. Below is the code that i tried:

Clear[μ, λ1, λ2, θ1, θ2, Δ, ϵ, k];
$Assumptions = True;$Assumptions = {μ, λ1, λ2, θ, k} ∈ Reals;
μ = -2;
ϵ[k_] := -2 λ1*Cos[k] - 2 λ2*Cos[2 k];
Δ[k_] := λ1*Sin[k] + λ2*Sin[2 k];
θ1[k_] := ArcTan[(ϵ[k] - μ)/Δ[k]];
λ1 = 2.5;
λ2 = 0;
Integrate[θ1'[k], {k, -π, π}]
NIntegrate[θ1'[k], {k, -π, π}]
λ1 = 0;
λ2 = 1.5;
Integrate[θ1'[k], {k, -π, π}]
NIntegrate[θ1'[k], {k, -π, π}]


And it gives following output:

3.14159
6.28319
0.
12.5664


It would be extremly helpful if someone can explain as to what is wrong with it?

• Multiples of pi and arctan... It could be issues with branch cuts. Commented Jun 12, 2017 at 6:58
• Using approximate reals (e.g. 2.5, 1.5) with exact solvers (e.g. Integrate) sometimes fails. Try Rationalize[] as a remedy or enter parameters as fractions. Commented Jun 12, 2017 at 15:21
• Possible duplicate: Last paragraph of mathematica.stackexchange.com/questions/18393/… Commented Mar 3, 2020 at 21:27

This seems to be a numerical issue with Integrate since by rationalizing the input, same result is now obtained.

ClearAll[μ, λ1, λ2, θ1, Δ, \
ϵ, k];
μ = -2;
ϵ[k_] := -2*λ1*Cos[k] - 2*λ2*Cos[2*k]
Δ[k_] := λ1*Sin[k] + λ2*Sin[2*k]
θ1[k_] := ArcTan[(ϵ[k] - μ)/Δ[k]]
λ1 = 0;
λ2 = 1.5;


And

der = Derivative[1][θ1][k]
derR = Rationalize@der


Now

Integrate[der, {k, -Pi, Pi}]
Integrate[derR, {k, -Pi, Pi}]


The second answer above $$4 \pi$$ now matches the numerical result.

NIntegrate[der, {k,-Pi,Pi}]


One also notices that the integrand is continuous, but the anti-derivative as reported by Integrate is discontinuous, which indicates a problem with the result, since one would expect the anti-derivative to be continuous as well.

 Plot[der, {k, -Pi, Pi}]


 Plot[Evaluate@Integrate[der, k],{k,-Pi,Pi}]