Apply quad (or dblquad) on a series of integrals
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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!
0 Kommentare
Antworten (1)
Sarah Wait Zaranek
am 1 Apr. 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 Kommentar
Siehe auch
Kategorien
Mehr zu Numerical Integration and Differentiation finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!