Symbolic substitution (subs) doesn't work with negative expressions

4 views (last 30 days)
Does the SUBS function have a bug when expressions have a negative sign in them? Why isn't the second output (a - c)?
clear all;
syms a b c
disp( subs(a + 2*b, 2*b, c) );
disp( subs(a - 2*b, 2*b, c) ); % The only difference is the negative sign
disp( subs(a - 2*b, -2*b, c) ); % Works if substituting -2*b instead of 2*b
Output:
a + c
a - 2*b
a + c

Answers (2)

Azzi Abdelmalek
Azzi Abdelmalek on 5 Aug 2013
Use
disp( subs(a + 2*b, {-2*b,2*b}, {-c c}) )

Walter Roberson
Walter Roberson on 5 Aug 2013
You should use MuPAD's op() and type() functions in order to explore the internal structure of each of the expressions. For example it is possible that the expression a - 2*b is stored internally as
_subtract(a, _mult(b, 2))
and that -2*b in the subs() call is being stored internally as
_negate(_mult(b, 2))
or
_mult(b, -2)
As _mult(b, -2) is not a subtree of _subtract(a, _mult(b, 2)) then the subs() would fail.
When I read through the documentation in the symbolic toolbox, I would expect that the representations would be _plus(a, _mult(b, -2)) and _mult(b, -2) in which case the subs() should succeed, but I do not have that toolbox to test with, and the alternate representations I showed are plausible ones that could potentially be triggered by the MATLAB to MuPAD interface.

Community Treasure Hunt

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

Start Hunting!