feval only for a certain variables
5 ビュー (過去 30 日間)
古いコメントを表示
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?
0 件のコメント
採用された回答
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
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.
その他の回答 (1 件)
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
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.
参考
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!