using quad2d

22 ビュー (過去 30 日間)
Aziz
Aziz 2011 年 12 月 18 日
Hi, i am trying to use quad2d and running into the following problem: I've a functions defined , e.g.
f1 = @(x,y) x+y
f2 = @(x,y) x-y
f3 = @(x,y) x
f4 = @(x,y) y
I need to integrate dot([f1 f2], [f3 f4]). if I try quad2d(@(x,y) dot([f1 f2], [f3 f4]),a,b,c,d ) it isn't working. Any suggestions as how fix this? thanks

採用された回答

Mike Hosea
Mike Hosea 2011 年 12 月 19 日
DOT doesn't work on function handles, and as Walter says, QUAD2D requires that the integrand be able to handle matrix input. You don't need all that reshaping, however:
quad2d(@(x,y)f1(x,y).*f3(x,y)+f2(x,y).*f4(x,y),a,b,c,d)

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2011 年 12 月 18 日
quad2d(@(x,y) dot([f1(x,y) f2(x,y)], [f3(x,y) f4(x,y)]),a,b,c,d )
  2 件のコメント
Aziz
Aziz 2011 年 12 月 19 日
Thanks, I am getting
??? Error using ==> quad2d>tensor at 350
Integrand output size does not match the input size.
Walter Roberson
Walter Roberson 2011 年 12 月 19 日
The quad2d() reference says,
All input functions must be vectorized. The function Z=fun(X,Y) must accept 2-D matrices X and Y of the same size and return a matrix Z of corresponding values
However, when you supply matrix arguments, although each of f1, f2, f3, and f4 would return matrices, [f1(x,y) f2(x,y)] is going to be a 2D array with the pieces concatenated along the second dimension (horzcat), and likewise [f3(x,y) f4(x,y)] would be a 2D array with the pieces concatenated along the second dimension, and dot() applied to a pair of 2D arrays will apply the dot product along the first non-singular dimension (which will be the first dimension in this case), giving you a row vector of results rather than a 2D array.
Alternative code:
quad2d(@(x,y) reshape(f1(x(:),y(:)) .* f3(x(:), y(:)) + f2(x(:),y(:)) .* f4(x(:), y(:)), size(x)) )

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

カテゴリ

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