how to use integral3 without loops
Show older comments
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 Comments
Roger Stafford
on 3 Jan 2013
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
on 4 Jan 2013
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
on 4 Jan 2013
Answers (0)
Categories
Find more on Numerical Integration and Differentiation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!