Use meshgrid to evaluate anonymous function which accepts vector inputs

조회 수: 7 (최근 30일)
Hello,
I want to numerically evaluate the average value of an function over a domain using meshgrid. For this simple example, we can use many other methods but I would like to stick with meshgrid.
% Define my domain, a rectangle
[X1,X2] = ndgrid([1,2],[3,4]);
% Define myfunc1 with individual input
myfunc1 = @(x1,x2) x1+x2;
% Define myfunc2 with a two-dimensional vector
myfunc2 = @(x) x(1)+x(2);
% Evaluate the mess grid using following expression
f01 = myfunc1(X1,X2);
% what about f02? I cannot do the following
f02 = myfunc2([X1,X2]);
% After got f01 or f02, I can use sum() to get average value.
So if a function accepts a vector input, how to use it to evaluate a meshgrid? I don't want to convert it back to multiple individual inputs. Thanks, Liang
  댓글 수: 1
Liang Chen
Liang Chen 2018년 2월 16일
I think one way to solve this problem is: if I have a function, for example: myfunc = @(x) x(1) + x(2) + x(3) then I can create another one which only accepts individual inputs: myfunc2 = @(x1,x2,x3) myfunc([x1,x2,x3]) Therefore, I can apply meshgrid on myfunc2

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2018년 2월 16일
arrayfun(@(x1,x2) myfunc2([x1,x2]), X1, X2)

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by