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 CenterFile Exchange 中查找有关 Numerical Integration and Differentiation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by