Apply quad (or dblquad) on a series of integrals

조회 수: 3 (최근 30일)
Rob
Rob 2011년 4월 1일
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!

답변 (1개)

Sarah Wait Zaranek
Sarah Wait Zaranek 2011년 4월 1일
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
Sarah Wait Zaranek
Sarah Wait Zaranek 2011년 4월 1일
Note, that trapz can take multiple inputs.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by