Integral inside integral is taking too much time

22 views (last 30 days)
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)
  5 Comments
Walter Roberson
Walter Roberson on 20 Jul 2021
Sometimes it helps to reverse the order of integration.
Waseem Akhtar
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!

Sign in to comment.

Accepted Answer

Walter Roberson
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 .
  14 Comments
Walter Roberson
Walter Roberson on 14 Aug 2021
The outer integral() passes a vector of values into the inner interval. When the inner integral is set to ArrayValued, the inner integral only passes in one trial omega value at a time, and for that omega does the value calculation that is vectorized with respect to the vector of values from the outer integral, l . The inner integral continues to pass in one value at a time until it is satisfied that it has met integration tolerances for all elements corresponding to the positions in l .
When the outer integral is marked as array valued instead, but the inner integral is not, then the outer integral would pass a single value at a time to the inner integral. The inner integral would then construct a vector of values to pass to be calculated with the scalar l from the outer integral.
What is the difference, since either way the inner calculation is going to be evaluate with a vector? Well, there is not necessarily an inherent advantage to one or the other. However... depending on the calculations for each of the variables, it might happen to be the case that numerically one of the two versions happens to have its integral converge faster than the other way.
Walter Roberson
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.

Sign in to comment.

More Answers (0)

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!