# Integral inside integral is taking too much time

20 visualizzazioni (ultimi 30 giorni)
Waseem Akhtar il 20 Lug 2021
Commentato: Walter Roberson il 14 Ago 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)
##### 5 CommentiMostra 3 commenti meno recentiNascondi 3 commenti meno recenti
Walter Roberson il 20 Lug 2021
Sometimes it helps to reverse the order of integration.
Waseem Akhtar il 23 Lug 2021
Modificato: Waseem Akhtar il 23 Lug 2021
@Torsten Thank you for your reply. I tried symbolic integration but it is even taking longer than the numerical integration

Accedi per commentare.

### Risposta accettata

Walter Roberson il 20 Lug 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 CommentiMostra 12 commenti meno recentiNascondi 12 commenti meno recenti
Walter Roberson il 14 Ago 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 il 14 Ago 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.

Accedi per commentare.

### Categorie

Scopri di più su Numbers and Precision in Help Center e File Exchange

R2020a

### Community Treasure Hunt

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

Start Hunting!

Translated by