# Integral inside integral is taking too much time

45 views (last 30 days)
Waseem Akhtar on 20 Jul 2021
Commented: Walter Roberson on 14 Aug 2021
Hi,
I am trying to evaluate integral inside integral using the following script but it is taking too much time, is there anyway I can reduce the time of evaluation:
f =@(x,y) x+y; % "x+y is just a simple example, the actual function is way more complicated"
integral(@(y) exp(-integral(@(x) f(x,y),a,b,'RelTol',1e-8,'AbsTol',1e-13),aa,bb,'RelTol',1e-8,'AbsTol',1e-13,'ArrayValued',true)
Waseem Akhtar on 23 Jul 2021
Edited: Waseem Akhtar on 23 Jul 2021
@Torsten Thank you for your reply. I tried symbolic integration but it is even taking longer than the numerical integration
Thank you for your help!

Walter Roberson on 20 Jul 2021
Use worse tolerances to reduce the computation time, at the expense of decreased accuracy.
In some cases, switching to integral2 might help. You are using arrayvalued so that would require multiple calls to integral2.
The fastest approach can depend on what your function is sensitive to. Numeric integration is adaptive, so if part of the range has rapid changes then it can slow down. In some cases, the rapid change only applies for one part of a range, and if you are using arrayvalued you can end up having to do the detailed integration for everything according to the worst part of the range, so sometimes arrayfun of non-arrayvalued can be faster. Sometimes .
Walter Roberson on 14 Aug 2021
value = int(exp(-(T-L.*(0.97)).*(2e+05)).*(int((exp(-omega-(((2e+05).*sqrt((T-L.*(0.97)).^2+(U+L.*(0.26)).^2)).^2./(4*omega))))./(2*omega),omega,AA,BB)),L,A,B);
valF = matlabFunction(value, 'vars', [AA,BB,A,B,U,T]);
That is going to fail because matlabFunction does not generate integral2() automatically when it sees nested integral(), and matlabFunction() does not know it should generate 'arrayvalued' at any point.

### Categories

Find more on Numerical Integration and Differentiation in Help Center and File Exchange

R2020a

### Community Treasure Hunt

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

Start Hunting!

Translated by