I want to do a 1D integral of some 2D data

4 ビュー (過去 30 日間)
Matthew Hunt
Matthew Hunt 2021 年 8 月 16 日
コメント済み: Matthew Hunt 2021 年 8 月 16 日
Suppose I have a function which is defined as some numerical data which is equally spaced and I wish to compute:
How would I go about it? I thought I would start something akin to:
x=linspace(-3,3,200),y=linspace(-3,3,200);
[X,Y]=meshgrid(x,y);
Z=0.5*(X.^2+Y.^2);
%This creates a plottable surface, then I tried something like:
g=trapz(Z,y,2);
but it threw up an error. Any suggestions?

採用された回答

Wan Ji
Wan Ji 2021 年 8 月 16 日
trapz function has been wrongly used, trapz(x_array, F(x,y)) is with . So you should do following
x=linspace(-3,3,200);
y=linspace(-3,3,100);
[X,Y]=meshgrid(x,y);
Z=0.5*(X.^2+Y.^2);
%This creates a plottable surface, then I tried something like:
g=trapz(y,Z);
plot(x,g,'r*')
hold on
g2 = 0.5*(x.^2*6 + 1/3*(3^3+3^3));
plot(x,g2, 'b--')
legend('trapz','exact')
  1 件のコメント
Matthew Hunt
Matthew Hunt 2021 年 8 月 16 日
That's precisely what I was looking for, thank you.

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2021 年 8 月 16 日
g=trapz(y,Z,2);
... but be careful about which dimension is x and which dimension is y. In MATLAB, rows correspond to y and columns correspond to x, and rows is the first dimension...

カテゴリ

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