Message Boards Message Boards

Symbolic integration has Problems

In[1]:= NIntegrate[
  Sqrt[(Cos[\[Theta]1] - Cos[\[Theta]2])^2 + (Sin[\[Theta]1] - 
      Sin[\[Theta]2])^2], {\[Theta]1, 0, 2 \[Pi]}, {\[Theta]2, 0, 
   2 \[Pi]}]/(2 \[Pi])^2

Out[1]= 1.27324

In[2]:= Integrate[
  Sqrt[(Cos[\[Theta]1] - Cos[\[Theta]2])^2 + (Sin[\[Theta]1] - 
      Sin[\[Theta]2])^2], {\[Theta]1, 0, 2 \[Pi]}, {\[Theta]2, 0, 
   2 \[Pi]}]/(2 \[Pi])^2

Out[2]= 8/\[Pi]

In[3]:= N[%]

Out[3]= 2.54648

 (* check which one is is correct using random sampling*)

n = Length[\[Theta]s = Partition[RandomReal[{0, 2 \[Pi]}, 10^6], 2]]

Out[11]= 500000

In[12]:= Sum[
  Sqrt[(Cos[\[Theta]s[[i, 1]]] - 
      Cos[\[Theta]s[[i, 2]]])^2 + (Sin[\[Theta]s[[i, 1]]] - 
      Sin[\[Theta]s[[i, 2]]])^2], {i, n}]/n

Out[12]= 1.27184
POSTED BY: Frank Kampas
6 Replies

Maple does it correctly

int(sqrt((cos(t1) - cos(t2))^2 + (sin(t1) - sin(t2))^2), t1 = 0 .. 2*Pi, t2 = 0 .. 2*Pi)/(2*Pi)^2;
                           4 
                           --
                           Pi
POSTED BY: Frank Kampas

Specifying an integration path sometimes helps. Here locating the singularity on the θ2 path makes it faster and accurate.

Integrate[
  Sqrt[(Cos[θ1] - Cos[θ2])^2 + (Sin[θ1] - Sin[θ2])^2], 
 {θ1, 0, 2 π},
 {θ2, 0, θ1, 2 π}  (* Ν.Β. *)
 ] / (2 π)^2
% // N
(*
  4/π
  1.27324
*)
POSTED BY: Michael Rogers

The FullSimplify expression has the same shape but gives the correct result

In[5]:= FullSimplify @ 
Sqrt[(Cos[\[Theta]1] - Cos[\[Theta]2])^2 + (Sin[\[Theta]1] - 
Sin[\[Theta]2])^2]

Out[5]= Sqrt[2 - 2 Cos[\[Theta]1 - \[Theta]2]]

In[6]:= Plot3D[Sqrt[
2 - 2 Cos[\[Theta]1 - \[Theta]2]], {\[Theta]1, 0, 
2 \[Pi]}, {\[Theta]2, 0, 2 \[Pi]}]

enter image description here

POSTED BY: Frank Kampas

Most likely the code that looks for path singularities is simply falling short on the original integrand.

POSTED BY: Daniel Lichtblau

You are right, the result is wrong. Here are three ways of calculating the integral, two give the right answer and one does not

f = Sqrt[(Cos[\[Theta]1] - Cos[\[Theta]2])^2 + (Sin[\[Theta]1] - 
       Sin[\[Theta]2])^2];
Integrate[
 FullSimplify[f], {\[Theta]1, 0, 2 \[Pi]}, {\[Theta]2, 0, 2 \[Pi]}]
Integrate[f, {\[Theta]1, \[Theta]2} \[Element] 
  Rectangle[{0, 0}, {2 Pi, 2 Pi}]]
Integrate[f, {\[Theta]1, 0, 2 \[Pi]}, {\[Theta]2, 0, 2 \[Pi]}]

However, it is a sad fact that symbolic integration is prone to erros due to branch cuts. The function we are integrating is non-analytical on the diagonal, and I suppose this is the root of the problem. The primitive with respect to one variable is discontinuous on the diagonal:

Integrate[f, \[Theta]1]
Plot3D[%, {\[Theta]1, 0, 2 \[Pi]}, {\[Theta]2, 0, 2 \[Pi]}]
POSTED BY: Gianluca Gorni
Posted 2 years ago

Here is a is another way:

2 Integrate[f, {\[Theta]1, 0, 2 \[Pi]}, {\[Theta]2, 0, \[Theta]1}]

And another:

Integrate[Integrate[FullSimplify[f], {\[Theta]2, 0, 2 \[Pi]}, 
  Assumptions -> 0 < \[Theta]1 < 2 \[Pi]], {\[Theta]1, 0, 2 \[Pi]}]

I assume the problem is because of shape of the function (meaning not differentiable at $\theta_1=\theta_2$):

Plot3D[f, {\[Theta]1, 0, 2 \[Pi]}, {\[Theta]2, 0, 2 \[Pi]}, PlotPoints -> 200]

3D plot of function

POSTED BY: Jim Baldwin
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract