problem with symbolic to function handle

조회 수: 3 (최근 30일)
lorenzo donadio
lorenzo donadio 2016년 11월 1일
댓글: Walter Roberson 2016년 11월 1일
Hi, i have a symbolic function of 3 variables and then I compute its gradient and convert each entry of the gradient to a function handle,but some of them become function handles of less than 3 variables, and that makes the evaluation of the vector field more difficult, any ideas on how to solve this?
syms x y z
f=x*y*z+x*2*y
G=gradient(f)
Dx=matlabFunction(G(1));
Dy=matlabFunction(G(2));
Dz=matlabFunction(G(3));
[X,Y,Z]=meshgrid(-3:1:3);
Fx=Dx(Y,Z);
Fy=Dy(X,Z);
Fz=Dz(X,Y);
this code works but i would like it to work when i write:
Fx=Dx( X,Y,Z);
Fy=Dy(X,Y,Z);
Fz=Dz(X,Y,Z);
and this is what my function handles look like:
Dx =
@(y, z) y .* z + 2 .* y
Dy =
@(x, z) x .* z + 2 .* x
Dz =
@(x, y) x .* y

채택된 답변

Steven Lord
Steven Lord 2016년 11월 1일
You can specify the list of input arguments the generated function or function handle should have using the 'Vars' parameter described in the documentation for the matlabFunction function. See the "Specify Input Arguments for Generated Function" example on that documentation page to see how to use this parameter.
  댓글 수: 3
lorenzo donadio
lorenzo donadio 2016년 11월 1일
I found it thank you :D
Walter Roberson
Walter Roberson 2016년 11월 1일
m = matlabFunction(G, 'Vars', {x, y, z});

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

추가 답변 (1개)

Mischa Kim
Mischa Kim 2016년 11월 1일
How about
syms x y z
f = x*y*z+x*2*y;
G = gradient(f);
m = matlabFunction(G);
[X,Y,Z] = meshgrid(-3:1:3);
m_eval = m(X,Y,Z);
  댓글 수: 1
lorenzo donadio
lorenzo donadio 2016년 11월 1일
this works but it doesnt solve the problem, because if i change the f function to lets say x^2+y^2+z now the gradient is only a function of x and y and doing
m = matlabFunction(G);
and then m_eval = m(X,Y,Z);
gives me and error,how can i fix it?

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

카테고리

Help CenterFile Exchange에서 Assumptions에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by