How can I create an ordinary floating point function from a symbolic array

1 回表示 (過去 30 日間)
If I have some code that generates a symbolic array, I'd like to be able to turn it into a function that takes floating point inputs.
For example, if f and x are symbolic vectors and p a symbolic variable, and f is
>> disp(f)
[ x1, x2*x3, p + x4, x5, p + x3 + x2*x5 + 1]
I want to be able to use f to generate the MATLAB function
function f = f(x,p)
f(1) = x(1); f(2) = x(2)*x(3); f(3) = x(4)+p; f(4) = x(5); f(5) = x(5)*x(2)+p+1+x(3);
or its equivalent.
I'm very unclear how to do this.
Thanks for your help.

採用された回答

Walter Roberson
Walter Roberson 2017 年 9 月 15 日
syms x1 x2 x3 x4 x5 p
f = [ x1, x2*x3, p + x4, x5, p + x3 + x2*x5 + 1]
matlabFunction(f, 'vars', {[x1,x2,x3,x4,x5], p})
ans =
function_handle with value:
@(in1,p)[in1(:,1),in1(:,2).*in1(:,3),p+in1(:,4),in1(:,5),p+in1(:,3)+in1(:,2).*in1(:,5)+1.0]
Note the use of the 'vars' option to group variables into a single input
  1 件のコメント
John Billingham
John Billingham 2017 年 9 月 16 日
Great. Works fine, and without being any slower. Thanks v much.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeAssumptions についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by