Substituting many symbolic variables with one vector to create function handle

Hello,
I am generating a symbolic function (which is very long) through multiple steps. To make my question easy, let's say i have:
syms B C
sym A(B,C)
A = B + C
for further computation I need to transform the symbolic function into a function handle of the form:
f = @(x) x(1)+x(2)
But I generally fail to do this. I tried different methods and I got as far as f = @(x1,x2) x1+x2
which is of course different.
How may I accomplish this task?
Thank you

 採用された回答

Andrei Bobrov
Andrei Bobrov 2013 年 4 月 13 日
syms B C
f = matlabFunction(B + C,'vars',{[B C]});

2 件のコメント

David
David 2013 年 4 月 13 日
This works great!
Thana Chut
Thana Chut 2019 年 10 月 12 日
Thanks for sharing this

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2013 年 4 月 13 日
Use an auxiliary function
function r = spreadargs(x, fhandle)
t = num2cell(x);
r = fhandle(t{:});
end
then when
f = matlabFunction(EXPRESSION, 'vars', {ListOfVariables});
invoke (e.g.)
fmincon( @(x) spreadargs(x, f), ....)

カテゴリ

質問済み:

2013 年 4 月 13 日

コメント済み:

2019 年 10 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by