Numerical integration using Trapz

24 ビュー (過去 30 日間)
Waseem Akhtar
Waseem Akhtar 2021 年 5 月 6 日
コメント済み: Star Strider 2021 年 5 月 7 日
Hi all,
I am trying to calculate a double integral over a rectangular grid using trapz.
The grid is defined as:
[x,z] = meshgrid(xp,zp);
xp and zp are the 1d arrays of x and z coordinates of the grid.
I trying to calculate the function values (F) at the grid nodes as:
F(i,j)=trapz(zp,trapz(xp,g.*f,2));
here,
g is a function of x and z i.e. g(x,z) whereas, f values are already calculated values at each node of the grid i.e. not a function of x and z.
Can someone suggest how may I do it, please.

採用された回答

Star Strider
Star Strider 2021 年 5 月 6 日
That should produce one scalar value as ‘F’, so the subscripts are not necessary.
I would do something like this —
F = trapz(zp,trapz(xp,g.*f));
The result of the first trapz call will be a vector, and of the second will be a scalar. So long as ‘g’ and ‘f’ have compatible dimensions, and they match ‘xp’ and ‘zp’ appropriately, that should work.
  6 件のコメント
Waseem Akhtar
Waseem Akhtar 2021 年 5 月 7 日
Thank you Star Strider, your first answer was correct.
Star Strider
Star Strider 2021 年 5 月 7 日
As always, my pleasure!

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by