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

추가 답변 (0개)

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by