# Difference in results between Integrate and NIntegrate

I was trying to ocmpute the line integral between the two zeroes of the function

$$f(x)=\sqrt{\frac{-x^2+x-1}{x^2(x-1)^2}}$$.

The zeroes are located at $$(-1)^{1/3}$$ and $$(-1)^{2/3}$$. I used the following code:

streb = (- x^2 + x - 1)/((x - 1)^2 x^2);
zeroes = x /. Solve[-1 + x - x^2 == 0];
path[t_] := zeroes[[2]]*t + zeroes[[1]] (1 - t);
test = Integrate[-I Sqrt[3] Sqrt[streb] /. x -> path[t], {t, 0, 1}] //
Simplify
test2 = NIntegrate[-I Sqrt[3] Sqrt[streb] /. x -> path[t], {t, 0, 1}]

This gives two very different results. The result of Integrate is $$\pi$$ which is the correct one( known due to the integral being a length computed by a certain strebel differential on the 3 punctured sphere which has to be $$\pi$$), while the NIntegrate returns $$-0.855465 - 7.35615*10^{-16} i$$. The imaginary part is obviously a rounding error, but the real part is very off.

Investigating this a bit further, ploting a Table of points on the path shows a completely harmless function:

ListPlot[Table[-I Sqrt[3] Sqrt[streb] /. x -> path[t], {t, 0, 1,
1/1000}]]

But trying to plot the same function results in a completly chaotic behavior:

Plot[-I Sqrt[3] Sqrt[streb] /. x -> path[t], {t, 0, 1}]

So I guess the origin of the numerical problems lie somewhere in this behaviour. As I have to perform similar computations with integrands which cannot be solved by Integrate, it would be nice to identify the origin. All computations have been performed in Mathematica 11.2. The NIntegrate does not produce any error messages.

It is a precision issue. Use arbitrary precision rather than machine precision by specifying a WorkingPrecision in both the NIntegrate and the Plot.

Clear["Global`*"]

streb = (-x^2 + x - 1)/((x - 1)^2 x^2);

zeroes = x /. Solve[-1 + x - x^2 == 0];

path[t_] := zeroes[[2]]*t + zeroes[[1]] (1 - t)

test = Integrate[-I Sqrt[3] Sqrt[streb] /. x -> path[t], {t, 0, 1}] //
Simplify

(* Pi *)

test2 = NIntegrate[-I Sqrt[3] Sqrt[streb] /. x -> path[t], {t, 0, 1},
WorkingPrecision -> 15]

(* 3.14159265358969 *)

test == test2

(* True *)

Plot[-I Sqrt[3] Sqrt[streb] /. x -> path[t], {t, 0, 1},
WorkingPrecision -> 15]

• It was indeed just a precision problem, thank you very much! Oct 11, 2021 at 14:21