필터 지우기
필터 지우기

feval only for a certain variables

조회 수: 6 (최근 30일)
Geppo Batt
Geppo Batt 2015년 5월 14일
댓글: Geppo Batt 2015년 5월 17일
I computed a symbolic formula, for example fun=x^2*z^2+y^2*w^2 (in my case I have many symbolic variables) , after this I convert the symbolic formula in function handle (with "matlabFunction"), for example fun=@(x,z,y,w) x^2*z^2+y^2*w^2. I need to substitute in this function only certain variables, for example only z and w, and after other calculations substitute the other variables. Can I do this with "feval" or exist other functions?

채택된 답변

Walter Roberson
Walter Roberson 2015년 5월 14일
You can pass symbolic variables to anonymous functions.
fun=@(x,z,y,w) x^2*z^2+y^2*w^2;
syms x y
newexpression = fun(x, 5, y, 6);
You can matlabFunction the result if appropriate.
  댓글 수: 2
Walter Roberson
Walter Roberson 2015년 5월 17일
Change
feat_brf=@(phi,theta,psi,x_body_ned,y_body_ned,z_body_ned,...
x_feat_ned,y_feat_ned,z_feat_ned) (M_ned2brf * [x_feat_ned;y_feat_ned; z_feat_ned])...
-[x_body_ned;y_body_ned;z_body_ned];
to
feat_brf=@(phi,theta,psi,x_body_ned,y_body_ned,z_body_ned,...
x_feat_ned,y_feat_ned,z_feat_ned) (M_ned2brf(psi,theta,phi) * [x_feat_ned;y_feat_ned; z_feat_ned])...
-[x_body_ned;y_body_ned;z_body_ned];
When you name a variable that is a function handle, MATLAB keeps it as a function handle unless you specifically invoke it using () and passing in appropriate arguments. Naming a variable that is a function handle does not cause MATLAB to fish amongst the currently active variables to find the ones with the same names as the formal function arguments and use them.
When you define an anonymous function such as
f = @(x,y) y.^2+x
then you should interpret this as
* f is an anonymous function of two arguments
* the return value from the anonymous function is calculated as
plus(power(varargin{2},2),varargin{1})
* if the anonymous function ever needs to be displayed, then label varargin{1} as 'x' and label varargin{2} as 'y'
In other words, the function handle doesn't know anything about variable names except for display purposes: it just cases about what was passed in which position in the calling sequence. When you do no invoke the function handle by using () after it, it remains a function handle, not something that should search to resolve variable names and use them.
Geppo Batt
Geppo Batt 2015년 5월 17일
thank you very much I ignored this problem: that is the calling sequence. Just a last question: if I want for example compute an operation with components of feat_brf for example:
feat_brf(1)/feat_brf(2)
and after solve in numerical; what it's the correct way to do the operation above described (taking in account the variables that after we want substitute)?

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

추가 답변 (1개)

Guillaume
Guillaume 2015년 5월 14일
Note: I don't know anything about the symbolic toolbox, but your question seems to be only about function handles.
You only need to create a new function handle where you supply only parts of the arguments:
fun = @(x,z,y,w) x^2*z^2 + y^2*w^2;
knownz = 5; knownw = 6;
newfun = @(x,y) fun(x, knownz, y, knownw); %substitute part of the variables
%later on:
result = newfun(knownx, knowny);
Or you could create a generic function handle to perform the substitution:
substitutezw = @(z, w, fn) @(x, y) fn(x, z, y, w); %anonymous function whose output is an anonymous function
newfun = substitutezw(knownz, knownw, fun);
%later on
result = newfun(knownx, knowny)
  댓글 수: 5
Guillaume
Guillaume 2015년 5월 15일
I'm not sure I understand what component1 and component2 are. Are these functions of the input variable.
Can you also confirm that your last line of code is a typo? It should read:
feat_fin = feat_camera_sub(... ; %not feat_sub
Other than that typo, there is nothing wrong with the code you've posted, and testing it on my machine with some dummy variables work just fine.
Note that you have to define the anonymous function in the right order. That is component1 and component2 must be defined correctly before you declare the feat_camera anonymous function. And feat_camera_sub must be defined afterward. Once an anonymous function has been defined, changing the constants it refers to will not affect it.
Geppo Batt
Geppo Batt 2015년 5월 17일
Ok now I try to explain my problem well. At first of all I've a rotation matrix:
M_ned2brf=@(psi,theta,phi) [cos(theta).......]
After this I transform the coordinates of a point in NED to BRF with this command:
feat_brf=@(phi,theta,psi,x_body_ned,y_body_ned,z_body_ned,...
x_feat_ned,y_feat_ned,z_feat_ned) (M_ned2brf * [x_feat_ned;y_feat_ned; z_feat_ned])...
-[x_body_ned;y_body_ned;z_body_ned];
Now in theory the feat_brf has three components along x y and z. My problem is related to the evaluation of these components because when I try to do this (numbers are random):
feat_brf(10,10,1,121,13,131,212,13,131)
I've the following error:
Undefined function 'mtimes' for input arguments of type 'function_handle'.
Error in
@(phi,theta,psi,x_body_ned,y_body_ned,z_body_ned,x_feat_ned,y_feat_ned,z_feat_ned)(M_ned2brf*[x_feat_ned;y_feat_ned;z_feat_ned])-[x_body_ned;y_body_ned;z_body_ned]
Now I hope that the explanation is more clear.

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

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by