using quad2d
조회 수: 19 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
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)
댓글 수: 0
추가 답변 (1개)
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
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)) )
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!