# Strange results from evaluating a simple integral

I am trying to do a seemingly simple integral in Mathematica 10 and am getting strange results:

g[x1_, y1_, x2_, y2_] = -Log[Sqrt[(x2 - x1)^2 + (y2 - y1)^2]]
Integrate[g[Cos[θ], Sin[θ], 0.1, 0.1], {θ, 0, 2 π}]


I am getting back

3.0725 - 1.16032 I


Clearly this is nonsense, since the integrand is real everywhere, and the integration doesn't go anywhere near to $g$'s singularity. What gives?

• – george2079 Apr 22 '15 at 21:18
• Making those .1's into 1/10 results in 0 ( very fast.. ) (v9.) – george2079 Apr 22 '15 at 21:22
• Yes, I get that too, and it agrees with some very simple Riemann sum code I hand-wrote. I'm still curious why both Integrate and NIntegrate choke on this simple integral (and how to avoid this bug in the future). – user168715 Apr 22 '15 at 21:28
• (1) It's not strictly true that the integrand is real everywhere. E.g., g[Cos, Sin, 1. I, 1. I]. Clearly M is using complex methods to evaluate the integral, and the approximate coefficients can cause numerical error in the symbolic analysis. It could also be a bug. (2) In V10.1, I get -9.58041*10^-8 + 1.88738*10^-15 I, which is effectively a real result and numerically close-ish to the true answer. (3) Adding the assumption that θ is real produces 12.29 - 4.44089*10^-16 I, closer to real, farther from the true answer. A similar answer if 0.116 is used instead of 0.1. – Michael E2 Apr 22 '15 at 22:17
• @MichaelE2 (1) True but 0.1 is hard-coded as the third and fourth argument; h[t] = g[Cos[t], Sin[t], 0.1, 0.1] is real for all real t. – user168715 Apr 22 '15 at 22:49

It seems to me that Integrate can do some strange things with your function g. From plotting g, we can see the integral should clearly be zero.

g[x1_, y1_, x2_, y2_] = -Log[Sqrt[(x2 - x1)^2 + (y2 - y1)^2]]
Plot[g[Cos[θ], Sin[θ], 1/10, 1/10], {θ, 0, 2 π}] Further,

Integrate[g[Cos[θ], Sin[θ], 1/10, 1/10], {θ, 0, 2 π}]


gives zero as expected, but

Integrate[g[Cos[θ], Sin[θ], 0.1, 0.1], {θ, 0, 2 π}]


gives

-9.58041*10^-8 + 1.88738*10^-15 I


which may be due to errors resulting from the use of inexact arithmetic. However, I can not explain the following:

Integrate[g[Cos[θ], Sin[θ], 0.1, 0.1], {θ, 0, 2 π}, Assumptions -> θ ∈ Reals]


Reduce::ratnz: Reduce was unable to solve the system with inexact coefficients. The answer was obtained by solving a corresponding exact system and numericizing the result. >>

12.29 - 4.44089*10^-16 I


I carried out these calculations in V10.1.0.

### Update

I reported the strange result from evaluating

Integrate[g[Cos[θ], Sin[θ], 0.1, 0.1], {θ, 0, 2 π}, Assumptions -> θ ∈ Reals]


to Wolfram tech support. The response was

It does appear that Integrate is not behaving properly in this case and I have forwarded an incident report to our developers with the information you provided.

Therefore, I marking this question with the tag.

• Thanks; I am using 10.0 which explains the difference in our answers. I'm confused why the Assumptions makes any difference, though -- shouldn't Mathematica conclude that $\theta$ is real from the fact that it is sampled from the interval $[0,2\pi]$? – user168715 Apr 23 '15 at 19:09
• @user168715. I don't blame you for being puzzled by the result returned when the Assumptions option is given; I am puzzled by it too. I am going to report it to Wolfram Research tech support. If I get a useful reply, I will update this answer to convey to the Mathematica.SE community. – m_goldberg Apr 24 '15 at 16:09

It is not a good idea to feed approximate numbers like 0.1 to symbolic methods.

Have a look at the indefinite integral Integrate[g[Cos[\[Theta]], Sin[\[Theta]], x2, y2], \[Theta]] to see what's going on. Mathematica has to go very far into complex analysis to solve this integral symbolically. I suspect the several terms with branch points at \[Theta] == \[Pi]` are particularly treacherous. I'm not surprised that Mathematica cannot find its way back onto the real line when you also ask it to deal with approximate rather than exact numbers.