2
$\begingroup$

I want to perform a double integration over a line segment in 2D and I am wondering if can it be done in Mathematica. An added difficulty is that the integral is singular.

$$I = \int_{(4,4)}^{(2,8)}\int_{(4,4)}^{(2,8 )} \log|x-y| \,\mathrm{d}y \, \mathrm{d}x.$$

I am not that concerned with efficiency here, I would just like to have a nice way to integrate these types of integrals compared to how I have to do it in Python which involves analytically regularizing them first to deal with the singular integrands and then transformation to reference intervals before finally coding the actual integration.

So can this be done 'nicely' in Mathematica?

Edit My Mathematica code that gives the incorrect answer:

  NIntegrate[
     Log[Norm[{x1, x2} - {y1, y2}]], 
     {x1, x2} ∈ Line[{{4,4},{2,8}}], 
     {y1, y2} ∈ Line[{{4,4},{2,8}}]
     ]
$\endgroup$
4
  • $\begingroup$ What do the integral limits mean? $\endgroup$ Commented Feb 12, 2019 at 12:51
  • $\begingroup$ @HenrikSchumacher The limits mean integrate over the straight line segment from the point $[3,3]$ to the point $[2,5]$. $\endgroup$ Commented Feb 12, 2019 at 13:36
  • $\begingroup$ @HenrikSchumacher Actually I just noticed it can be done using the method you used in a previous question I asked here - mathematica.stackexchange.com/questions/190728/… $\endgroup$ Commented Feb 12, 2019 at 13:46
  • $\begingroup$ However it gives me the result: -0.0435146 whereas I get -0.042677 in Python! I get alot of warnings from Mathematica too before it gives me that result and the 'error estimate': 0.008498. $\endgroup$ Commented Feb 12, 2019 at 13:47

1 Answer 1

3
$\begingroup$

Putting the singularities on the boundary of the integration domain makes NIntegrate take care of them. Plus, this utilizes the fact that the integrant is symmetric, so that the work can be reduced by half.

p = {2, 8};
q = {4, 4};
2 Norm[p - q]^2 NIntegrate[
  Log[Norm[(p (1 - t) + t q) - (p (1 - s) + s q)]], 
  {t, 0, 1}, {s, t, 1}]

-0.0426773

If you are in need of integrating this integrant over more general and more complicated domains and if speed is an issue, the fast multipole method and related techniques might be your friends.

$\endgroup$
9
  • $\begingroup$ Ok cool, I got the same result in Python! I will edit my original post to show the Mathematica code I used that gave the wrong result. It would be great if you could identify why it is wrong as I would like to able to compute integrals in this form. $\endgroup$ Commented Feb 12, 2019 at 13:53
  • $\begingroup$ I plan to implement the FMM eventually but for the moment I'm just trying to make sure all my singular integral evaluations are correct! $\endgroup$ Commented Feb 12, 2019 at 13:56
  • $\begingroup$ Do you think the edited code I posted in the OP can be 'fixed' to give the correct answer? It would be great if I could evaluate integrals using expressions in Mathematica like that rather than having to transform them first like I have to do with Python. $\endgroup$ Commented Feb 12, 2019 at 13:57
  • 2
    $\begingroup$ I'm not sure if this warrants an answer or not but NIntegrate[Log[Norm[{x1, x2} - {y1, y2}]] /. Abs -> Identity, {x1, x2} ∈ Line[{{4, 4}, {2, 8}}], {y1, y2} ∈ Line[{{4, 4}, {2, 8}}]] gets preprocessed to NIntegrate[20 Log[2 Sqrt[5] Sqrt[(x1 - x2)^2]], {x1, 0, 1}, {x2, 0, 1}] internally and this is where it chokes. $\endgroup$
    – Greg Hurst
    Commented Feb 12, 2019 at 14:33
  • 1
    $\begingroup$ @ChipHurst Hm. Interesting. From there one could go by NIntegrate[20 Log[2 Sqrt[5] Sqrt[(x1 - x2)^2]], {x1, 0, 1}, {x2, 0, 1}, Exclusions -> {x1 == x2} ]. Unfortunately, NIntegrate with regions refused to accept a setting for Exclusions. $\endgroup$ Commented Feb 12, 2019 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.