Generation of symbolic vector of functions

1 visualizzazione (ultimi 30 giorni)
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 Commenti
jf
jf il 26 Gen 2021
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 il 26 Gen 2021
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.

Accedi per commentare.

Risposta accettata

Walter Roberson
Walter Roberson il 26 Gen 2021
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

Più risposte (0)

Tag

Prodotti


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by