how to use integral3 without loops
이전 댓글 표시
I am trying to run a code that uses integral3 but this seems to be taking so long. Is there a way to use vectors in integral3 rather than loops?
L = 1:1:5;
num = numel(L);
eff = zeros(1, num);
extcoef = [3 5 600 4 3 699 200 22 22 55 33];
alp = numel(extcoef);
for i = 1:num
pl = L(i);
Q = 0;
for ii = 1:alp
alpha = extcoef(ii);
% define the function
fun = @(x,y,z) (2*pi*pl)^-1*sin(x).*(exp(-alpha.*...
(pl-y)./(sin(x).*sin(z)))+ exp(-alpha.*...
(pl+y)./(sin(x).*sin(z))));
% integrate using triple integral
q = integral3(fun,0.73,pi/2,0,pl,0,pi);
Q = Q + q;
end
eff(i) = Q;
end
plot(L, eff);
thank you for your help regarding this..
댓글 수: 3
Roger Stafford
2013년 1월 3일
You can easily obtain the definite integral of your function with respect to y from y = 0 to y = pl as an explicit function of just x and z. The resulting function will not be very much more complicated than your present function. I suggest you do that and then your problem becomes that of numerically solving a double integral of the new function with respect to x and z which should be much, much faster. For this latter you can use either 'quad2d', 'dblquad', or 'integral2', whichever proves to be faster.
Roger Stafford
2013년 1월 4일
Just to encourage you to avoid time-consuming triple integration, here is the function, g(x,z), you would use as integrand in a double integration with respect to x and z:
g = @(x,z) sin(x).^2.*sin(z)/(2*alpha*pi*pl).*...
(1-exp(-2*alpha*pl./(sin(x).*sin(z))));
This is obtained by integrating your 'fun' with respect to y from 0 to pl. As you see, it is even simpler than 'fun'. You should always use explicit solutions like this from calculus when available in order to minimize numerical processing.
Ping
2013년 1월 4일
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!