How to integrate a function over an implicitly defined domains

5 views (last 30 days)
Hello everyone,
I am attempting to write code for integrating a function f(x, y) over a rectangular domain with a cutout inside. The concept involves defining the cut-out domain implicitly within the overall integral domain, similar to what was done in the referenced paper. http://dx.doi.org/10.1137/140966290
Thank you for your support and your ideas..
Best regards

Answers (2)

Torsten
Torsten on 16 Jan 2024
Edited: Torsten on 16 Jan 2024
I'd define the function to be integrated as f .* (g>0) and integrate over the rectangular domain. Here, g is the function that implicitly defines your cutout and is > 0 outside the cutout.
Example:
f = @(x,y)x.^2+y.^2;
g = @(x,y) 1-f(x,y);
i1 = integral2(@(x,y)f(x,y).*(g(x,y)>0),-2,2,-2,2)
Warning: Reached the maximum number of function evaluations (10000). The result fails the global error test.
i1 = 1.5707
i2 = integral2(@(x,y)f(x,y).*(g(x,y)<=0),-2,2,-2,2)
Warning: Reached the maximum number of function evaluations (10000). The result fails the global error test.
i2 = 41.0959
i1+i2
ans = 42.6667
i = integral2(f,-2,2,-2,2)
i = 42.6667
  1 Comment
John D'Errico
John D'Errico on 16 Jan 2024
Edited: John D'Errico on 16 Jan 2024
By far the best approach. Be careful, if the interior of the cutout is something that generates nonsense, perhaps inf or NaN. In that case, you will want to test to see if a point lies inside the cut-out, and then just avoid the computation in that region. Even so, this is trivial to write. Just return 0 for those points.

Sign in to comment.


Hassaan
Hassaan on 16 Jan 2024
Edited: Hassaan on 16 Jan 2024
Monte Carlo Integration
Monte Carlo methods are useful for high-dimensional integrals or when the domain of integration has an irregular shape. The basic idea is to randomly sample points in the rectangular domain and then check whether they fall inside the cut-out. If they do, they are discarded; otherwise, they contribute to the integral estimate.
Here's a conceptual algorithm for Monte Carlo Integration:
1. Define the domain R and the cut-out D.
2. Generate N random points within R.
3. For each point, check if it lies outside of D.
4. If a point is outside of D, evaluate f(x, y) at that point.
5. Sum up all the function evaluations and normalize by the ratio of the area of R to the number of points outside of D.
6. This sum approximates the integral of f over R \ D.
Adaptive Quadrature Methods
Adaptive quadrature methods adjust the number of function evaluations based on the function's behavior. These methods can be used with a function that checks if a point is within the cut-out area and only evaluates the integral function if the point is outside the cut-out.
Here's a conceptual algorithm for Adaptive Quadrature Integration:
1. Define the domain R and the cut-out D.
2. Start with a coarse partition of R into subdomains.
3. On each subdomain, check if it intersects with D.
4. If a subdomain is entirely outside D, use quadrature rules to estimate the integral over it.
5. If a subdomain intersects with D, partition it further and repeat the process.
6. Continue refining the partition until the desired accuracy is reached or until a maximum level of refinement is achieved.
7. Sum up all the subdomain integrals to approximate the integral over R \ D.
Level Set Methods
The level set method represents the cut-out as a zero level set of a higher-dimensional function. This method is quite advanced and might be similar to what's described in the paper you referenced. It allows for the implicit definition of domains and is powerful in handling evolving interfaces and shapes.
Conceptual steps for Level Set Integration:
1. Define a level set function Φ(x, y) that is negative inside D and positive outside.
2. Divide the rectangular domain R into a grid.
3. For each grid point, check the sign of Φ to determine if it's inside or outside D.
4. Use quadrature rules on grid points outside of D, possibly with adjustments near the boundary where Φ = 0.
5. Aggregate the integral estimates, accounting for the cut-out boundary using the properties of the level set function.
Discretization Methods
If the cut-out domain is regular enough, you can discretize the overall domain and then explicitly subtract the cut-out portion.Conceptual steps for Discretization Integration:
1. Discretize the domain R into a grid or mesh.
2. Identify which elements of the grid or mesh fall within the cut-out domain D.
3. Remove or ignore the grid elements that fall within D.
4. Apply numerical integration (like the midpoint rule, trapezoidal rule, or Simpson's rule) to the remaining elements.
5. Sum the results to obtain the integral over the domain R without D.
Finite Element Method (FEM)
For complex domains, the Finite Element Method can be used where the domain is divided into elements, and the integral is approximated over these elements. The FEM can handle domains with holes by not meshing the hole region.
Conceptual steps for FEM:
1. Mesh the domain R, excluding the cut-out domain D.
2. Define a basis set of functions over the mesh.
3. Express the function f(x, y) in terms of the basis functions.
4. Integrate each basis function over the mesh, excluding the area corresponding to D.
5. Sum the results, weighted by the coefficients of the expansion of f in the basis set.
When implementing these methods, care must be taken to accurately represent the boundary of the cut-out domain and to ensure that the numerical integration is sufficiently precise near the edges of the cut-out. If the cut-out's boundary is defined by a function g(x, y) = 0, you can use this function to test whether a point is inside or outside the cut-out.
In practice, the choice of method will depend on the specific characteristics of the function f(x, y) and the domain you are integrating over. If the function is smooth and the boundary of the cut-out is not too complex, simpler quadrature rules might suffice. For more complex situations, Monte Carlo or adaptive methods may be more appropriate.
Remember that all these methods will approximate the integral, and the precision will depend on the number of samples or the fineness of the discretization. In the context of the methods discussed, error estimation and convergence checks are crucial to ensure that the integral's approximation is within an acceptable tolerance.
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!