# How do I integrate a function defined over an arbitrary area?

Let's say, I have a compact area $$S$$ (for example a circle, a square or some arbitrary polygon) and a function $$f: S \rightarrow \mathbb{R}$$. I want to numerically calculate the Integral $$\int_S f(\vec{x})\ \mathrm{d}x \approx \sum_i f(x_i)\Delta x_i$$ of the function over $$S$$.

My question is, how to choose the sapling points $$x_i$$ for "complicated" areas such as circles or shapes irregularly shaped borders. Can I simply overlay the aread with a equidistant lattice of sampling points and remove the ones not inside the area?

• Super hard problem. Most general approach is to integrate over a square that contains your domain as a subset and then multiply the integrand by an indicator function. This generally makes your integrand discontinuous so convergence is slow. If you need speed, you have to work out the Gaussian quadratures for your domain; see doi.org/10.1016/S0885-064X(03)00011-6 for details. – user14717 Nov 12 '18 at 23:19
• You could discretize your domain in triangles/tetrahedrals and compute the integral over each domain. – nicoguaro Nov 13 '18 at 0:21
• Do you know anything about your function, $f$? The optimal quadrature is going to depend on that, along which quantites you're most interesting in keeping well behaved (e.g., is it important that you get back the exact solution for a constant function). – origimbo Nov 13 '18 at 13:45

The divergence theorem in three dimensions for a vector function $$F$$ can be written
$$\int_V \nabla \cdot {\bf F} dv = \int_S {\bf F} \cdot ds$$ That is, an integral over the volume of the region can be replaced by an integral over the bounding surface. For a scalar function, $$f$$, in two dimensions this can be simplified to $$\int_A \frac{\partial f}{\partial x} dx dy = \int_C f ds$$ relating an integral over the area to a line integral over the curve bounding the area.