Generation of symbolic vector of functions

1 回表示 (過去 30 日間)
jf
jf 2021 年 1 月 26 日
コメント済み: Walter Roberson 2021 年 1 月 26 日
I need to define a lot of symbolic variables for DAE solver.
Instead of using
sym f1(t) f2(t) % up to arbitrary fN(t)
i would prefer something like this
sym('f_%d(t)',[1 N])
or similar. But it seems that use of parenthes is not allowed. Is possible to generate programatically such vectors?
  3 件のコメント
jf
jf 2021 年 1 月 26 日
I dont think it is that case. According to sym documentation can do
sym('dx_%d',[1 N])
What is the difference between this and variables f_i(t)?
Walter Roberson
Walter Roberson 2021 年 1 月 26 日
John is not correct, it can be done and I have done it.
The case with [] without () is handled by calling sym() to generate the names and then processing the returned list of names. That does not work with () because sym() does not handle (). Handling of () is done by syms() calling symfun() to create the appropriate objects.
There is no reason why syms could not be reprogrammed to handle arrays of functions: it just has not been done.

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

採用された回答

Walter Roberson
Walter Roberson 2021 年 1 月 26 日
syms("f_" + (1:N) + "(t)")
The output cannot be assigned to anything.
syms() must be used for this, not sym()
str2sym() will not work for this purpose, and instead will effectively give you back variable names that just happen to include '(' 't' ')' characters but which are not functions.
There is an alternative involving aarrayfun of a symfun call and 'uniform', 0
  1 件のコメント
jf
jf 2021 年 1 月 26 日
Thank you, syms is the key :)

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

その他の回答 (0 件)

タグ

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by