Stepsize in integral2 or quad2d

9 views (last 30 days)
Saeid
Saeid on 21 May 2020
Edited: John D'Errico on 21 May 2020
I would like to solve the double integral:
for i=1:length(z)
fz= @(P,Q) sin(P).*(sqrt(1+z(i).*cos(Q).*(sin(2*P)+z(i).*cos(Q).*sin(P).^2))).^(n-1)
Integral(i)=quad2d(fz,0,pi,0,pi/2)
end
where P & Q are the integral variables and z is a parameter that is given as a vector. If I run z in a loop going e.g. from 0 to 10 this integral is calculated relatively fast. However, in the original program z is not run in a pre-defined loop as the one above and it is first calculated in an fsolve routine and then fed into this integral calculator, and because of the nature of the fsolve it is unknown from the beginning how many times this integral has to be caculted. Usually when I run the program, for some reason it looks like it stalls at the point where this double integral has to be claculated. I followed some advice in this forum and changed from integral2 to quad2d but the problem persists althought the calculation becomes a bit faster.
Here my question: how can I determine the stepsize in either integral2 or quad2d? I could not find anything in the documentation. The current (probably automatically selected) number of steps is 150.

Accepted Answer

John D'Errico
John D'Errico on 21 May 2020
Edited: John D'Errico on 21 May 2020
integral (as with integral2) does not have a "stepsize". As an adaptive code, it looks at the local shape of the function, in the sense that there are methods that will see if the integral is converging locally. If there are convergence problems, then it takes more samples from the function in that local region. So on nasty functions that vary rapidly, or functions with singularities, the tool will sample more densely there. If everything seems totally boring, then it takes a minimal number of samples.
What you don't want it to do is to sample densely EVERYWHERE, if something is just complicated in one small area. But you do want code that will be able to adapt to what it sees, for each specific kernel. Therefore, you set a desird tolerance on the result, not a stepsize, and expect the integration tool to provide a solution that will be "close" by some measure to the true solution.
The same is true for essentially any such numerical integration tool, as long as they are adaptively able to work on any function as a black box. That does not include tools like trapz of course, since trapz merely works on the set of points you pass in.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!