'ArrayValued' Integral Extremely Slow: Slower than For-Loop Through Each Element of Array
2 views (last 30 days)
Show older comments
The title says it all. I first wrote out some code for creating a vector of simulated data through integrating a function for many different values of Q:
%%For-loop implementation
S_ani_int_func = @(Q,r)(r.^3 .* gam(r) .* diff_g_r_func(r) .* sphbesselj(2,Q*r));
S_ani = @(Q)(C * integral(@(r)S_ani_tanh_int_func(Q,r),0,Inf));
my_S_ani_Q = zeros(Q_len,1);
for Q_i = 1:Q_len
my_S_ani_Q(Q_i) = S_ani(Q_vec(Q_i));
end
I then realized the integral function had a built-in array-valued flag, which I then tried to use to my advantage:
%%'ArrayValued' implementation
S_ani_int_func = @(r)(r.^3 .* gam(r) .* diff_g_r_func(r) .* sphbesselj(2,r.*Q_vec));
my_S_ani_Q = C * integral(S_ani_int_func,0,Inf,'ArrayValued',true);
The two implementations return the exact same vectors, which is good, but the stock array-valued way is for some reason A LOT slower. I run this piece of code in a larger loop several hundred times, and it becomes very obvious: the latter takes at least three times as long. Why is this the case?
1 Comment
Chad O'Brien
on 9 Oct 2015
I observe the same thing for small length vectors. However if you increase the number of points in the array you'll see it switches. I'd like to know why as well. There must be some non-scalable function integral calls when it is in arrayvalued mode.
Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!