3
$\begingroup$

I have four conditions over the two-dimensional domain $x,y\in[a,b]$. I want to use NIntegrate to compute the area of the union of the intersection of the two conditions con1 and con2 with the intersection of the two conditions con3 and con4, i.e. the region $(con1 \cap con2)\cup(con3 \cap con4)$. Does the following code give me the correct answer?

NIntegrate[
  Boole[{con1 && con2}, {con3 && con4}], 
  {x, a, b}, {y, a, b}
]
$\endgroup$
6
  • 1
    $\begingroup$ I'm confused.... Why can't you tell if it gives you the correct answer? $\endgroup$ Commented May 10, 2023 at 22:26
  • $\begingroup$ @DavidG.Stork Corrected :) $\endgroup$
    – Martha97
    Commented May 11, 2023 at 9:56
  • $\begingroup$ I still am confused. Why don't you simply try your code on an example and see if it gives the correct answer?? Be explicit about the conditions... that's all. I don't see how we can help you if you don't try your code. $\endgroup$ Commented May 11, 2023 at 10:40
  • $\begingroup$ @DavidG.Stork Since I do not know the area of my desired condition analytically, they can be computed only numerically, and NIntegrate was the only way I knew! If it is so clear, why cannot you answer if this code is correct in general (for any conditions and not only for an example) or not? My question is for a general situation not only for a particular example! $\endgroup$
    – Martha97
    Commented May 11, 2023 at 10:54
  • $\begingroup$ It is NOT so clear, and I never said that. Your accepted "answer" from CraigCarter shows a single example (the kind you could have and should have done on your own), but doesn't answer the full general problem. Are you now positive that CraigCarter's solution will work for every case you have in mind? $\endgroup$ Commented May 11, 2023 at 15:48

1 Answer 1

6
$\begingroup$

You could compare it in this fashion:

ir1 = ImplicitRegion[
  x + y < 1 && x - y <  1, {{x, -2, 2}, {y, -2, 2}}]

ir2 = ImplicitRegion[
  x + y > 0 && x - y >  0, {{x, -2, 2}, {y, -2, 2}}]

RegionPlot[RegionUnion[ir1, ir2]]

Integrate[1, {x, y} \[Element] RegionUnion[ir1, ir2] ]

or

NIntegrate[1, {x, y} \[Element] RegionUnion[ir1, ir2] ]
$\endgroup$
3
  • $\begingroup$ Thanks! Now, I see that my ode is fundamentally wrong; this part Boole[{con1 && con2}, {con3 && con4}] becomes red when I try to run it. So, my desired area can be computed in the way you mentioned. May I ask you to explain what $1$ stands in your Integrate or NIntegrate code? Sorry if it is a primary question; I am a beginner in Mathematica. $\endgroup$
    – Martha97
    Commented May 11, 2023 at 11:01
  • $\begingroup$ And if my conditions are very complicated, and if it is needed to increase the precision of the result, where I should add PlotPoints, ` Precision` , or MaxRecursion in your code? $\endgroup$
    – Martha97
    Commented May 11, 2023 at 11:09
  • 1
    $\begingroup$ {x, y} \[Element] RegionUnion[ir1, ir2] is analogous to the region you want. It is equivalent to "dA" for a two dimensional integral. So, integrating 1 over dA gives you the area you want. I left the NIntegrate as an option. If your region is a meshed region, you will need to do a numerical approximation. HTH. $\endgroup$ Commented May 11, 2023 at 14:38

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.