Effacer les filtres
Effacer les filtres

Apply quad (or dblquad) on a series of integrals

3 vues (au cours des 30 derniers jours)
Rob
Rob le 1 Avr 2011
Is it possible to apply a numerical integration technique, for example quad or dblquad on a series of integrals, without using a for loop?
So, if I have a system of elements, and I want to ingrate each element, I could use a for loop to split up the system and calculate the integral on each element seperately. However, since a loop takes a lot of computation time, I would rather put all elements in a matrix and let quad integrate all the elements in the matrix at once. However, quad gives an error that it needs scalars as integration limits, whereas in my case these limits are 2 vectors, rather than 2 scalars. Does anyone know a trick to deal with this?
Thanks a lot in advance!

Réponses (1)

Sarah Wait Zaranek
Sarah Wait Zaranek le 1 Avr 2011
The only thing I could think of was to use arrayfun. I don't think it will be much faster you for (at least in my tests) but it is cleaner code.
N = 1000;
F = @(x)1./(x.^3-2*x-5);
xlow = rand(1,N);
xhigh = xlow + 1;
Q = zeros(1,N);
% Using a loop
tic
for ii = 1:N
Q(ii) = quad(F,xlow(ii),xhigh(ii));
end
toc
% Using arrayfun
tic
Q2 = arrayfun(@(x,y) quad(F,x,y),xlow,xhigh);
toc
isequal(Q,Q2)
  1 commentaire
Sarah Wait Zaranek
Sarah Wait Zaranek le 1 Avr 2011
Note, that trapz can take multiple inputs.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Numerical Integration and Differentiation dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by