1
$\begingroup$

I want to integrate a function f[x,y] over a region like this.

enter image description here

Is there any way to define the region using the vertices of the purple polygon?

Then I can subsequently perform NIntegrate[f[x,y],{x, y} ∈ region]

$\endgroup$

1 Answer 1

3
$\begingroup$

Here is an example in 12.2.

poly = Polygon[{{0, 0}, {1/2, Sqrt[3]/2}, {1, 1/Sqrt[3]}, {1, 0}}];
NIntegrate[Log[x + y + 1], {x, y} \[Element] poly]

0.366623

Let us verify it by

Integrate[Log[x + y + 1], {x, y} \[Element] poly]

-((36 - 12 Sqrt[3] + 12 Log[2] + 228 Sqrt[3] Log[2] + 138 Log[3] + 54 Sqrt[3] Log[3] + 9 Log[4] - 3 Sqrt[3] Log[4] + 48 Sqrt[3] Log[6] - 2 Log[8] - 2 Sqrt[3] Log[8] + 2 Sqrt[3] Log[9] - 48 Sqrt[3] Log[2 - 2/Sqrt[3]] + 90 Log[2 - Sqrt[3]] + 54 Sqrt[3] Log[2 - Sqrt[3]] - 90 Log[3 - Sqrt[3]] - 54 Sqrt[3] Log[3 - Sqrt[3]] - 180 Log[-1 + Sqrt[3]] - 108 Sqrt[3] Log[-1 + Sqrt[3]] + 72 Log[1 + Sqrt[3]] - 48 Sqrt[3] Log[1 + Sqrt[3]] - 36 Log[2 + Sqrt[3]] + 24 Sqrt[3] Log[2 + Sqrt[3]] - 18 Log[3 + Sqrt[3]] - 90 Sqrt[3] Log[3 + Sqrt[3]] - 48 Log[6 + Sqrt[3]] - 52 Sqrt[3] Log[6 + Sqrt[3]] - 72 Log[3 + 2 Sqrt[3]] + 48 Sqrt[3] Log[3 + 2 Sqrt[3]] + 36 Log[9 + 5 Sqrt[3]] - 24 Sqrt[3] Log[9 + 5 Sqrt[3]])/(8 Sqrt[ 3] (19 + 11 Sqrt[3]) (-45 + 26 Sqrt[3])))

N[%]

0.366623

Addition. NIntegrate produces [a different] the same result if the vertices are taken couunter-clockwise as

poly1 = Polygon[{{1, 1/Sqrt[3]}, {1/2, Sqrt[3]/2}, {0, 0}, {1, 0}}];
NIntegrate[Log[x + y + 1], {x, y} \[Element] poly1]

[0.17812]0.366623

shows.

$\endgroup$
4
  • 1
    $\begingroup$ Does it matter whether I define the points in the function Polygon[] in clockwise or counter-clockwise manner? $\endgroup$ Commented Apr 1, 2021 at 5:18
  • $\begingroup$ Now I am confused about the different results for the clockwise and counterclockwise. Why is it different? Which one is the actual value of the integral over the region? $\endgroup$ Commented Apr 1, 2021 at 7:06
  • 1
    $\begingroup$ I get the same result (0.366623) when I integrate over poly1 and poly $\endgroup$ Commented Apr 1, 2021 at 7:16
  • $\begingroup$ @ArishmanPanigrahi: Yes, you are right. Executing that on a fresh kernel, I obtain the same result. $\endgroup$
    – user64494
    Commented Apr 1, 2021 at 8:54

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.