How to perform double integration with vectors as integral limits
3 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(1 个)
John D'Errico
2018-9-5
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.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!