feval only for a certain variables
古いコメントを表示
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?
採用された回答
その他の回答 (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 件のコメント
Geppo Batt
2015 年 5 月 14 日
編集済み: Geppo Batt
2015 年 5 月 14 日
Walter Roberson
2015 年 5 月 14 日
Is it possible that one of the things you are passing is a function rather than a variable?
Geppo Batt
2015 年 5 月 15 日
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
2015 年 5 月 17 日
カテゴリ
ヘルプ センター および File Exchange で Functional Programming についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!