# Integrate gives wrong result

**URL:** <https://community.wolfram.com/t/integrate-gives-wrong-result/6265>\
**Category:** Ideas\
**Created:** [July 25, 2015, 9:16am UTC](https://community.wolfram.com/t/integrate-gives-wrong-result/6265 "2015-07-25T09:16:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![simontyran](https://avatars.discourse-cdn.com/v4/letter/s/57b2e6/32.png) [@simontyran](https://community.wolfram.com/u/simontyran)\
**Post date:** [July 25, 2015, 9:16am UTC](https://community.wolfram.com/t/integrate-gives-wrong-result/6265/1 "2015-07-25T09:16:29Z")

</div>

When I try to analytically solve the Integral

```
F = Integrate[Sqrt[(x + Cos[?])^2 + (y + Sin[?])^2], {?, 0,2 ?}]

```

Mathematica gives the solution

```
F = 0

```

(so does Maple and other programs).

Since Sin and Cos only give values between -1 and +1 one can clearly see that this can not be true if x or y are high or low numbers. I tried to find a fitting curve and found that

```
F = Power[(2 ?)^E + (2 ? Sqrt[x^2 + y^2])^E, (E)^-1]

```

I have no mathematical proof for that, but it seems elegant enough to be true. Of course I know that Integrate has some “possible issues” and I always recommend to test the result with NIntegrate. But anyway, since I seem to have found a better analytical solution than zero I thought it might help if I share it. Maybe this can be fixed in future versions of Mathematica.

---

<div class="post-metadata">

**Author:** ![gianlucagorni](https://avatars.discourse-cdn.com/v4/letter/g/ec9cab/32.png) [@gianlucagorni](https://community.wolfram.com/u/gianlucagorni)\
**Post date:** [July 25, 2015, 10:07am UTC](https://community.wolfram.com/t/integrate-gives-wrong-result/6265/2 "2015-07-25T10:07:39Z")

</div>

I tested your formula for F numerically, and it doesn’t seem true:

```
With[{x = 1, 
   y = 2}, {Integrate[
    Sqrt[(x + Cos[\[CurlyPhi]])^2 + (y + 
         Sin[\[CurlyPhi]])^2], {\[CurlyPhi], 0, 2 \[Pi]}],
   Power[(2 \[Pi])^E + (2 \[Pi] Sqrt[x^2 + y^2])^E, (E)^-1]}] // N
With[{y = 2}, 
 Plot[NIntegrate[
    Sqrt[(x + Cos[\[CurlyPhi]])^2 + (y + 
         Sin[\[CurlyPhi]])^2], {\[CurlyPhi], 0, 2 \[Pi]}] -
   Power[(2 \[Pi])^E + (2 \[Pi] Sqrt[x^2 + y^2])^E, (E)^-1], {x, -20, 
   20}]]

```

---

<div class="post-metadata">

**Author:** ![simontyran](https://avatars.discourse-cdn.com/v4/letter/s/57b2e6/32.png) [@simontyran](https://community.wolfram.com/u/simontyran)\
**Post date:** [July 25, 2015, 12:42pm UTC](https://community.wolfram.com/t/integrate-gives-wrong-result/6265/3 "2015-07-25T12:42:02Z")

</div>

On the Plot it fits:

 ![2pi2.png](https://global.discourse-cdn.com/wolfram/original/3X/6/0/604754d03ed0f341d0de25bc928ec7bc71210b95.png)

 ![2pi.png](https://global.discourse-cdn.com/wolfram/original/3X/0/4/04f5ef8c62422802ca1fee093e21516bfffc230d.png)

---

<div class="post-metadata">

**Author:** ![gianlucagorni](https://avatars.discourse-cdn.com/v4/letter/g/ec9cab/32.png) [@gianlucagorni](https://community.wolfram.com/u/gianlucagorni)\
**Post date:** [July 25, 2015, 1:19pm UTC](https://community.wolfram.com/t/integrate-gives-wrong-result/6265/4 "2015-07-25T13:19:58Z")

</div>

The difference between F and G is of the order of .2 or less. It is too small to perceive it in the two plots you have made. Try plotting the difference:

```
With[{n = E, y = 0}, 
 Plot[NIntegrate[
    Sqrt[(x + Cos[\[CurlyPhi]])^2 + (y + 
         Sin[\[CurlyPhi]])^2], {\[CurlyPhi], 0, 2 \[Pi]}] - 
   Power[(2 \[Pi])^E + (2 \[Pi] Sqrt[x^2 + y^2])^E, (E)^-1], {x, 0, 
   2 Pi}]]

```

---

<div class="post-metadata">

**Author:** ![sblinder](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/sblinder/32/17861_2.png) [@sblinder](https://community.wolfram.com/u/sblinder)\
**Post date:** [July 25, 2015, 5:41pm UTC](https://community.wolfram.com/t/integrate-gives-wrong-result/6265/5 "2015-07-25T17:41:14Z")

</div>

If you first expand and simplify the argument of the square root, the integration works:

```
In[1]:= 
Simplify[Expand[(x + Cos[\[CurlyPhi]])^2 + (y + Sin[\[CurlyPhi]])^2]]

Out[1]= 1 + x^2 + y^2 + 2 x Cos[\[CurlyPhi]] + 2 y Sin[\[CurlyPhi]]

In[2]:= Integrate[
 Sqrt[1 + x^2 + y^2 + 2 x Cos[\[CurlyPhi]] + 
   2 y Sin[\[CurlyPhi]]], {\[CurlyPhi], 0, 2 \[Pi]}]

Out[2]= ConditionalExpression[
 4 Sqrt[1 + x^2 + y^2 - 2 Sqrt[x^2 + y^2]]
   EllipticE[-((4 Sqrt[x^2 + y^2])/(
    1 + x^2 + y^2 - 2 Sqrt[x^2 + y^2]))], 
 2 Sqrt[x^2 + y^2] < 1 + x^2 + y^2 && (1 + x^2 + y^2)/Sqrt[
   x^2 + y^2] >= 2]

```

---

<div class="post-metadata">

**Author:** ![hdolhaine](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/hdolhaine/32/35924_2.png) [@hdolhaine](https://community.wolfram.com/u/hdolhaine)\
**Post date:** [July 25, 2015, 8:35pm UTC](https://community.wolfram.com/t/integrate-gives-wrong-result/6265/6 "2015-07-25T20:35:57Z")

</div>

Hello,

first of all, the Integrand as a (positive) square-root of a sum of squares is greater or equal Zero. As is pointed out later, I think it can be Zero at only one Point, and that means, you integrate over a positive Integrand, so the integral should be positive and non-Zero.

Look at it from a geometrical Point of view. Your Integrand Looks like the norm of a vector, in fact the norm of the sum of two vectors,

```
XX = {x, y};
VV = {Cos[phi], Sin[phi]};

```

And this is a circle of radius 1 centered a the Point { - x, - y }. So your Integrand is in fact the length of a vector from the origin to a Point of that circle. That length is always positive, only if the circle passes through the origin it can be Zero, at exactly one Point. So, the Integrand is (mostly) positive ant the integral non-Zero.

Let’s check it. Define the norm or length of a vector by

```
norm[a_] := Sqrt[a.a]

```

and we get

```
 In[11]:= norm[XX + VV]
    
    Out[11]= Sqrt[(x + Cos[phi])^2 + (y + Sin[phi])^2]

```

exactly your Integrand.

In other words you integrate over the norm[XX + YY].

The vector nearest to the origin is XX = { 0, 0 } and as norm[YY] is 1 the integral should then become 2 Pi.

Taking into account the inequality

```
norm[a + b] <= norm[a] + norm[b] 

```

and norm [a] = constant we arrive at (at least in my opinion)

```
2 \[Pi] <= integral <= 2 \[Pi] ( 1 + norm [XX] )

```

Now let us rotate the Frame of reference. Define a Rotation Matrix dd

```
dd = ( {
    {Cos[t], Sin[t]},
    {-Sin[t], Cos[t]}
   } );

```

and apply it to the vector in question. Note that this is an orthogonal Transformation and lengths are not changed

```
  In[21]:= vecnew = dd.(XX + VV) // Expand // TrigReduce
  
  Out[21]= {Cos[phi - t] + x Cos[t] + y Sin[t], 
   y Cos[t] + Sin[phi - t] - x Sin[t]}

```

We Chose the angle t in a way, that the Point { x, y } is rotated to the x-axes of the new System

```
In[22]:= vecnew1 = 
 vecnew /. Cos[t] -> x/Sqrt[x^2 + y^2] /. Sin[t] -> y/Sqrt[x^2 + y^2]

Out[22]= {x^2/Sqrt[x^2 + y^2] + y^2/Sqrt[x^2 + y^2] + Cos[phi - t], 
 Sin[phi - t]}

```

Note that this is the same Situation as before, only the Point in question is tranferred to the x-axes of a new coordinate - System. And now we have

```
   j1 = norm[vecnew1] // FullSimplify
    
    Sqrt[1 + x^2 + y^2 + 2 Sqrt[x^2 + y^2] Cos[phi - t]]

```

But this is of the form Sqrt [a + b Cos [ w] ], as the Phase shift doesn’t matter when we integrate over phi from 0 to 2 Pi.

Indeed we get

```
In[28]:= J1 = \[Integral]j1 \[DifferentialD]phi

Out[28]= (2 Sqrt[1 + x^2 + y^2 + 2 Sqrt[x^2 + y^2] Cos[phi - t]]
  EllipticE[(phi - t)/2, (4 Sqrt[x^2 + y^2])/(
  1 + x^2 + y^2 + 2 Sqrt[x^2 + y^2])])/Sqrt[(1 + x^2 + y^2 + 
 2 Sqrt[x^2 + y^2] Cos[phi - t])/(1 + x^2 + y^2 + 2 Sqrt[x^2 + y^2])]

```

(hmm, interstingly the rotation angle is maintained in the Elliptical thing…) and

```
In[30]:= T2 = J1 /. phi -> 2 Pi

Out[30]= (2 Sqrt[1 + x^2 + y^2 + 2 Sqrt[x^2 + y^2] Cos[t]]
  EllipticE[1/2 (2 \[Pi] - t), (4 Sqrt[x^2 + y^2])/(
  1 + x^2 + y^2 + 2 Sqrt[x^2 + y^2])])/Sqrt[(1 + x^2 + y^2 + 
 2 Sqrt[x^2 + y^2] Cos[t])/(1 + x^2 + y^2 + 2 Sqrt[x^2 + y^2])]

In[31]:= T1 = J1 /. phi -> 0

Out[31]= -((
 2 Sqrt[1 + x^2 + y^2 + 2 Sqrt[x^2 + y^2] Cos[t]]
   EllipticE[t/2, (4 Sqrt[x^2 + y^2])/(
   1 + x^2 + y^2 + 2 Sqrt[x^2 + y^2])])/Sqrt[((
 1 + x^2 + y^2 + 2 Sqrt[x^2 + y^2] Cos[t])/(
 1 + x^2 + y^2 + 2 Sqrt[x^2 + y^2]))])

```

and finally

```
In[34]:= res = T2 - T1 // FullSimplify

Out[34]= (2 Sqrt[
   1 + x^2 + y^2 + 
    2 Sqrt[x^2 + y^2]
      Cos[t]] (EllipticE[\[Pi] - t/2, (4 Sqrt[x^2 + y^2])/(
      1 + x^2 + y^2 + 2 Sqrt[x^2 + y^2])] + 
     EllipticE[t/2, (4 Sqrt[x^2 + y^2])/(
      1 + x^2 + y^2 + 2 Sqrt[x^2 + y^2])]))/(Sqrt[(
  1 + x^2 + y^2 + 2 Sqrt[x^2 + y^2] Cos[t])/(
  1 + x^2 + y^2 + 2 Sqrt[x^2 + y^2])])

```

This should be the result of the Integration. If Mathematica says 0 that is wrong indeed.

Two final checks

```
In[35]:= res /. x -> 0 /. y -> 0

Out[35]= 2 \[Pi]

```

ok, And

```
In[40]:= res /. t -> ArcSin[y/Sqrt[x^2 + y^2]] /. x -> 5. /. 
  y -> 3.8 // N

Out[40]= 39.7097

```

compared to

```
In[41]:= NIntegrate[((Sqrt[(x - Cos[a])^2 + (y - Sin[a])^2]) /. 
    x -> 5. /. y -> 3.8), {a, 0, 2 Pi}]

Out[41]= 39.7097

```

Regards  
Hans

---

<div class="post-metadata">

**Author:** ![hdolhaine](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/hdolhaine/32/35924_2.png) [@hdolhaine](https://community.wolfram.com/u/hdolhaine)\
**Post date:** [July 26, 2015, 3:53pm UTC](https://community.wolfram.com/t/integrate-gives-wrong-result/6265/7 "2015-07-26T15:53:12Z")

</div>

As pointed out above the phase in the integral is of no importance because a and b in the integrand Sqrt [a + b Cos [ w] ] are constant and the Integration is around the whole circle from 0 to 2 Pi. Therefore we can write

```
In[1]:= j1a = Sqrt[1 + x^2 + y^2 + 2 Sqrt[x^2 + y^2] Cos[phi - t]] /. t -> 0

Out[1]= Sqrt[1 + x^2 + y^2 + 2 Sqrt[x^2 + y^2] Cos[phi]]

```

Avoiding lots of Messages by

```
In[2]:= SetOptions[Integrate, GenerateConditions -> False]

Out[2]= {Assumptions :> $Assumptions, GenerateConditions -> False, 
 PrincipalValue -> False}

```

we get

```
In[3]:= res = Integrate[j1a, {phi, 0, 2 Pi}]

Out[3]= 4 Sqrt[1 + x^2 + y^2 + 2 Sqrt[x^2 + y^2]]
  EllipticE[(4 Sqrt[x^2 + y^2])/(1 + x^2 + y^2 + 2 Sqrt[x^2 + y^2])]

```

and as above

```
In[4]:= res /. x -> 0 /. y -> 0

Out[4]= 2 \[Pi]

In[5]:= res /. x -> 5. /. y -> 3.8

Out[5]= 39.7097

```
