# How to perform double integration with vectors as integral limits

4 views (last 30 days)
jasongrig on 5 Sep 2018
Answered: John D'Errico on 5 Sep 2018
fxy = @(x,y) log10( 2.25 / ( (x.^2 + y.^2) ) ); % my function
v = integral2(fxy, ax, bx, ay, by,'method','iterated','AbsTol',1e-6,'RelTol',1e-6);
How can I execute the last line if "ax","bx","ay","by" are (1D) vectors (and not scalars)? Obviously, I want to avoid a "for" loop.

John D'Errico on 5 Sep 2018
So, you essentially have multiple, independent integrals over different domains. And you want integral2 to loop over the different domains, knowing what you want it to do. The result could only be achieved by a loop inside integral2. There needs to be a loop somewhere.
Is a for loop a real problem? No. Essentially, one extra line of code at each end for you.
Highly vectorized code can sometimes look impressive. LOOK MA! See what I did in only one line of code! Yeah, the code is impossible to read or understand, even if you find a way to do it.
In this case, integral2 is not designed to handle multiple, independent domains to integrate over, for good reason. It is an adaptive scheme, so each separate domain would need to be handled separately. That means, it would need to use an internal loop anyway. But the code is not written to do that.
So you would need to find an artifice, artificially wrapping the calls to integral2 into an implicit loop of some sort. While there are tools that can do these things, thus arrayfun & cellfun, they are not easily made to solve this particular problem without some seriously obtuse programming. It would take multiple lines of code to do anyway. The result would still be a loop. It would be no more efficient. And it would be impossible to follow what you did next year when you need to debug and maintain the code.
Just wrap it all in a loop. The result will be as efficient as it can be. It will be trivial to write.