James. You clearly have not thought about what I said. I never said you need to use an analytical integration. All I showed was how to simply transform the integration into polar coordinates, with the origin translated to the center of the ellipse. You will still use integral2!!
The point I made is that when you integrate in polar coordinates, then you will need an extra factor of r in the kernel.
Do I really need to explain this in great detail? Argh. Since I apparently need to show you how to do this in detail, I'll write it as an answer.
For example, integrate the function x*y, over an elliptical domain, centered at (x,y) = [1 2]. The equation of the ellipse will be a simple one:
Thus the ellipse has a major axis that is twice the length of the minor axis.
xycenter = [1 2];
fxy = @(x,y) x.*y;
fthetar = @(theta,r) fxy(xycenter(1) + r.*cos(theta),xycenter(2) + r.*sin(theta));
So nothing of any significance required there. All you need to supply is the basic function fxy, and the center of the ellipse.
I already gave you the formula for the ellipse boundary in polar form, at least for an unrotated ellipse, of the same form, but since I've already dealt with the translation, we can ignore that part. If the ellipse was rotated, we would have an xy term in the ellipse equation.
a = 1;
b = 2;
rbound = @(theta) sqrt(1./(cos(theta).^2/a^2 + sin(theta).^2/b^2));
Does it work? TRY IT!
rbound(0)
ans =
1
rbound(pi/2)
ans =
2
So at 0 degrees, we get a boundary in r of 1. The ellipse is elongated in the y direction, so at 90 degrees, it goes out to 2.
Now, integrate. USE INTEGRAL2. See that I carefully multiply by r in there.
I = integral2(@(theta,r) fthetar(theta,r).*r,0,2*pi,0,rbound)
I =
12.5663706004049
Is that correct? Perhaps simplest is to use an alternative method. Here I'll show an even simpler solution, a tool called tessquad, that does a gaussian integration over a triangulated domain. Apparently it is not posted on the file exchange.
I'll create a set of points around the perimeter of the ellipse, then build a triangulated domain from that polygon.
nt = 100;
t = linspace(0,2*pi,nt)';
xyellipse = [xycenter(1) + rbound(t).*cos(t),xycenter(2) + rbound(t).*sin(t)];
plot(xyellipse(:,1),xyellipse(:,2),'-')
axis equal
grid on
Now, build a triangulation over that domain
vert = [xyellipse;xycenter];
tri = [(1:nt)',[(2:nt)';1],repmat(nt+1,nt,1)];
Plot. AGAIN.
trisurf(tri,vert(:,1),vert(:,2),fxy(vert(:,1),vert(:,2)))
That is a crude plot of the surface. Note that the actual integration will be exact, because it will employ a Gaussian integration of sufficient order, and this is a simple polynomial function on that domain. Since the function is such low order, the gaussian integration is a very basic one, but tessquad worries about that.
It = tessquad(@(xy) fxy(xy(:,1),xy(:,2)),1,vert,tri)
It =
12.550841380856
Or, given the triangulation, I can do it using an adaptive 2-dimensional quadrature tool I've recently written to work on a triangulation.
options.tol = 1e-12
options =
struct with fields:
tol: 1e-12
I = simplxint2(fxy,tri,vert,options)
I =
12.550841380856
These two solutions have generated the exact solution to an integral over a triangulated domain that approximates the ellipse, because the boundary is represented as a polygon, not the true ellipse. Both of the above solutions are only approximations, because they miss a tiny bit of the volume. Had I made nt larger, they would be now much closer. I deliberately set nt at only 100 before so the surface plot would be best seen. Otherwise the edges in the triangulation would have dominated the surface.
(having redone the triangulation to reflect the larger nt)
It = tessquad(@(xy) fxy(xy(:,1),xy(:,2)),1,vert,tri)
It =
12.5662178639005
Now, exactly where in here do you think I've done any analytical integration? All you needed to supply was the function fxy, the center of the ellipse, and the lengths of the ellipse axes. In this case, I used a basic, unrotated ellipse. But that is trivially changed.
I should repost tessquad and simplxint2 on the FEX I guess, but they were used here only to verify the result from integral2.