# 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. – Simon Jun 12 '17 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. – Michael E2 Jun 12 '17 at 15:21

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

ClearAll[\[Mu], \[Lambda]1, \[Lambda]2, \[Theta]1, \[CapitalDelta], \
\[Epsilon], k];
\[Mu] = -2;
\[Epsilon][k_] := -2*\[Lambda]1*Cos[k] - 2*\[Lambda]2*Cos[2*k]
\[CapitalDelta][k_] := \[Lambda]1*Sin[k] + \[Lambda]2*Sin[2*k]
\[Theta]1[k_] := ArcTan[(\[Epsilon][k] - \[Mu])/\[CapitalDelta][k]]
\[Lambda]1 = 0;
\[Lambda]2 = 1.5;


And

der = Derivative[1][\[Theta]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}]