# Calculating Residue with NIntegrate fails [closed]

Trying to answer this interesting question Principal value from two different axis I observed a problem using Nintegrate:

The function func[p_] := 1/(Sinh[p/2] Sqrt[Cosh[p]]) has a pol at p==0.

The residue of this point evaluates to

Residue[func[z], {z, 0}] (*2*)
Limit[func[z] z, z -> 0] (*2*)


The result might be confirmed by integrating along a path in the complex plane which contains the pol. For example integrating along a square path

NIntegrate[func[z], {z, 1, I, -1, -I, 1}]/(2 Pi I) (*2*)


evaluates correct value , whereas integrating along a circle

NIntegrate[func[ Exp[I \[CurlyPhi] ]]/(2 Pi I), {\[CurlyPhi], 0, 2 Pi}]  (*~0*)


gives a message NIntegrate failed to converge...  and a wrong result 0!

What's wrong with this last integration?

How to modify to get the correct result?

Thanks!

• Alternate way (equivalent to Method -> "Trapezoidal": With[{n = 64}, First@Fourier[ Table[ func[Exp[I \[CurlyPhi]]]*Exp[I \[CurlyPhi]], {\[CurlyPhi], Most@Subdivide[0., 2 Pi, n]}] ]/Sqrt[n] ] Jul 25 at 17:20
• @MichaelE2 Thank you for this nice answer. Amazingly this "simple" method evaluates the expected result. Jul 25 at 17:53

NIntegrate[func[Exp[I \[CurlyPhi]]]/(2 Pi I)*I*Exp[I \[CurlyPhi]], {\[CurlyPhi], 0, 2 Pi}]

1.99993 - 1.11022*10^-16 I
• When you change variable, you need to include a derivative term to account for the fact that dz!=d[\CurlyPhi] Jul 25 at 18:12
• @UlrichNeumann user64494 means the last formula in the wiki section. The derivative is what you get when you substitute $z=e^{i\phi}$ into the $dz$ of the contour integral $\int_\gamma f(z)\,dz$. BTW, Method -> "Trapezoidal" numerical convergence properties than "GlobalAdaptive" on this sort of integral (see Trefethen & Weideman). Jul 25 at 18:12