Is there a way to vectorize trapezoidal numerical integration?

4 ビュー (過去 30 日間)
Jon B.
Jon B. 2022 年 4 月 12 日
コメント済み: Jon B. 2022 年 4 月 12 日
I was wondering whether there might be a way to (numerically) integrate several functions over different integration domains in a parallel manner. Using Matlab's trapz function, I can specify an "intergation domain"
X = [1 2.5 7 10];
and what essentially amount to the image of this vector under a set of, say, 3 different function
Y = [5.2 7.7 9.6 13.2;
4.8 7.0 10.5 14.5;
4.9 6.5 10.2 13.8];
Integration via trapz is then carried out as
trapz(X, Y, 2)
My problem is that I'd like to integrate each function over a different range. So essentially I'd like to have
X = [1 2.5 7 10;
3 4.1 8 11;
1 2.3 6 19];
Just for clarity: I can generate the desired result via a loop
for j = 1:3
out(j) = trapz(X(j,:), Y(j,:));
end
but since I am working with 1,000nds of function with 10,000nds of nodes in the integration domain this is not particularly expedient.
I'd very much appreciate it if anyone might have an idea how to go about parallelizing this task.
Many thanks.

採用された回答

Matt J
Matt J 2022 年 4 月 12 日
ab=conv2(Y,[1,1]/2,'valid');
h=diff(X,1,2);
out=sum(ab.*h,2);
  1 件のコメント
Jon B.
Jon B. 2022 年 4 月 12 日
Works like a charm. Thank you so much!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNumerical Integration and Differentiation についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by