Does ArrayValued Affect the Efficiency of Integral()
1 回表示 (過去 30 日間)
古いコメントを表示
Suppose I want to numerically integrate a function of a scalar that returns a vector
f = @(x) [1 ; sin(1000*x)];
To integrate that function I use ArrayValued = true
y = integral(f,0,2,'ArrayValued',true)
Presumably, the integration of the second element of f requires a much smaller step size than does the first. So does integral() choose the smallest step size based on evaluations of all components of f and then apply that same step to integrate all of f, even though the first element of f doesn't need such a small step? Or does it magically integrate each compoent of f separately with different step sizes (though it's not clear to me how it could do so)?
0 件のコメント
採用された回答
Matt J
2022 年 5 月 7 日
編集済み: Matt J
2022 年 5 月 7 日
Based on the tests below, it appears that integral() does not develop a separate mesh for each element of f. Otherwise, I would expect the final test to have nearly double the time of the first. Also, I would expect it to be an option to specify waypoints for every element of f individually, but the documentation does not indicate such an option.
f = @(x) [1 ; sin(1000*x)];
tic;
y = integral(f,0,2,'ArrayValued',true);
toc
f = @(x) [1 ; 1];
tic;
y = integral(f,0,2,'ArrayValued',true);
toc
f = @(x) [sin(1000*x) ; sin(1000*x)];
tic;
y = integral(f,0,2,'ArrayValued',true);
toc
2 件のコメント
Matt J
2022 年 5 月 8 日
編集済み: Matt J
2022 年 5 月 8 日
Do the results below suggest it's always best to break up the vector function into indvidual scalar functions?
No, because you only have two functions. If you had 100 or 1000, I speculate that it wouldn't be so advantageous to split them up.
e=ones(100,1)*1000;
f1 = @(x) sin(1000*x);
f = @(x) sin(e*x(:).');
t1 = numel(e) * timeit(@() integral(f1,0,2))
t2 = timeit(@() integral(f,0,2,'ArrayValued',true))
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!