Symbolic substitution (subs) doesn't work with negative expressions
2 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(2 个)
Walter Roberson
2013-8-5
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Special Values 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!