Why does the SUBS command change the size of the expression incorrectly?

1 vue (au cours des 30 derniers jours)
Fredrik Smith
Fredrik Smith le 9 Fév 2022
Commenté : Paul le 10 Fév 2022
Why does SUBS change the size of the expression even when the variable which would change the size of the expression is not present? For example
syms a b c d e
f = sym('f', [2,2])
f = 
g = a + b;
subs(g, {a,b,c}, {d,e,f})
ans = 
Clearly the result should be a 1x1 sym, not a 2x2?
  1 commentaire
Paul
Paul le 10 Fév 2022
At the risk of stating the obvious, it appears that the expansion of g is determined by the fact that f is in the set of replacement variables, regardless of whether or not f is actually substituted into g. The doc is silent as far as I can tell on what the behavior should be when a variable to be substituted, in this case e, does not appear in the expression.

Connectez-vous pour commenter.

Réponses (1)

Highphi
Highphi le 9 Fév 2022
Modifié(e) : Highphi le 9 Fév 2022
it's because of the way you're passing in your substitution variables
{ } ~= [ ]
syms a b c d e
f = sym('f', [2, 2])
f = 
g = a + b;
subs(g, [a, b], [d, e])
ans = 
  1 commentaire
Fredrik Smith
Fredrik Smith le 10 Fév 2022
Thank you for taking the time to answer! And yes I could just not pass f in as a variable as you have done, however that defeats the purpose as that was the whole point of my question.
When creating functions from symbolic expressions via 'matlabFunction' it is useful to have a standard set of variables using the 'vars' option to retain the correct order, which means that I often end up passing in many variables which are not used. It is useful to use a cell since sometimes I would like to substitute a scalar for a matrix.

Connectez-vous pour commenter.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by