Behavior of subs() function in Symbolic Math Toolbox

15 visualizzazioni (ultimi 30 giorni)
Dear MATLAB Central members,
I would like to ask a behavior of subs() in Symbolic Math Toolbox (R2016b, Windows 64 bit, OS: Windows 7).
Here is an example.
syms a b aM bM
aM=sym([2 3;4 5]);
bM=sym([6 7;8 9]);
s_temp=a*b;
>> s_temp=subs(s_temp,a,'aM')
s_temp =
aM*b
>> s_temp=subs(s_temp,b,'bM')
s_temp =
aM*bM
>> subs(s_temp)
ans =
[ 12, 21]
[ 32, 45]
which is same as
>> aM.*bM
ans =
[ 12, 21]
[ 32, 45]
but what I like to get the result from subs(s_temp) is a result of
>> aM*bM
ans =
[ 36, 41]
[ 64, 73]
Can I get this result using subs function, or should I do something else?
Thank you in advance for your help.
Regards,
Kosuke

Risposta accettata

Nicolas Schmit
Nicolas Schmit il 19 Set 2017
You have to define a and b as 2x2 symbolic matrices. Also, do not put the variables aM and bM into quotes when using subs()
a = sym('a', [2 2]);
b = sym('b', [2 2]);
aM=sym([2 3;4 5]);
bM=sym([6 7;8 9]);
s_temp=a*b;
s_temp=subs(s_temp,a,aM)
s_temp=subs(s_temp,b,bM)
subs(s_temp)
  1 Commento
Kosuke
Kosuke il 19 Set 2017
Dear Nicolas,
Thank you for your reply.
I learned a new thing of subs() function.
What I wanted was to handle a and b more likely strings,
and the actual calculation was done by substitution of a to aM and b to bM,
then evaluate aM*bM.
e.g.,
user types s_temp=a*b;
Then if the user wants to calculate a matrix representation of a*b,
use a function (say st2mat()) including subs() function,
M = st2mat(s_temp);
Thank you again,
Kosuke

Accedi per commentare.

Più risposte (1)

Walter Roberson
Walter Roberson il 19 Set 2017
No. The symbolic toolbox treats all unassigned variables as scalar and that is not possible to change (it is the behavior of the symbolic engine itself)
  1 Commento
Kosuke
Kosuke il 19 Set 2017
Dear Walter,
Thank you for your reply.
It seems that the behavior of symbolic toolbox is not simple as I expected.
Although, It is good to know it is not possible so that I can change my coding or put a caution that a*b as an input is not converted to aM*bM correctly.
Thank you again, Kosuke

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!