Converting symfun to function handle

8 ビュー (過去 30 日間)
Josef Lát
Josef Lát 2022 年 11 月 8 日
コメント済み: Walter Roberson 2022 年 11 月 9 日
Hi. In this answer I found a partial solution to my answer, but not quite. I have a symfun such as
syms a(t) b(t)
eq = a*b;
Now, I need to take matlabFunction() of the equation, such that the whole a(t) and b(t) get replaced. I tried:
matlabFunction(eq,"vars",[a b])
But fair enough, a and b are not symbolic variables, they are symfun. Is ther a workaround to this?
I also tried the following:
matlabFunction(eq,"vars",[formula(a) formula(b)])
But appearently, the elements of vars are no longer good enough variables.

回答 (1 件)

Walter Roberson
Walter Roberson 2022 年 11 月 8 日
If you want to "demote" a and b from functions to variables then you can subs() variables into the expression.
If you want have redefined a and b since the time you created a*b and you want to incorporate the new definitions then subs()
If you want to create a function handle that accepts function handles as its arguments and applies them, such as
c = a*b
intended to translate into
C = @(a, b) @(x)a(x).*b(x)
Then that is not something you can do through matlabFunction.
  2 件のコメント
Josef Lát
Josef Lát 2022 年 11 月 9 日
yes, the approach with subs would work like this:
syms a(t) b(t) a_placeholder b_placeholder
eq = a*b;
% ... more operations
eq = subs(eq, [a b], [a_placeholder b_placeholder]);
eq = formula(eq);
eq = matlabFunction(eq, "vars", {a_placeholder b_placeholder})
eq = function_handle with value:
@(a_placeholder,b_placeholder)a_placeholder.*b_placeholder
But I find this approach not very clean, with the must of defining 4 variables instead of 2. Have I made it more clear?
Walter Roberson
Walter Roberson 2022 年 11 月 9 日
eq = subs(eq, [a b], [sym('a'), sym('b') ])

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

カテゴリ

Help Center および File ExchangeConversion Between Symbolic and Numeric についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by