syms Gr Ra Ri A B Gr1 Gr2 c d
Gr =  2*c*(Ra^2 - Ri^2) - d*(Ra^4 - Ri^4)/2;
Gr1 = subs(Gr, [c*(Ra^2 - Ri^2), -d*(Ra^4 - Ri^4)/2], [A, B])
Gr2 = subs(Gr, [c*(Ra^2 - Ri^2),  d*(Ra^4 - Ri^4)/2], [A, B])

退货
Gr1 =

2*A + B


Gr2 =

2*A - (d*(Ra^4 - Ri^4))/2

有没有办法说服MATLAB在第二种情况下返回Gr2 = 2*A - B而没有解决方法?我要替换的表达式要复杂得多,但是我无法处理此subs()行为。谢谢。

最佳答案

我认为问题出在subs的第二个参数的乘法/除法运算中,因为一旦我将代码重新排列如下,问题就消失了:

syms Gr Ra Ri A B Gr1 Gr2 c d
Gr =  2*c*(Ra^2 - Ri^2) - d*(Ra^4 - Ri^4)/2;
Gr1 = subs(Gr, [(Ra^2 - Ri^2),(Ra^4 - Ri^4)], [A/c,-2*B/d])
Gr2 = subs(Gr, [(Ra^2 - Ri^2),(Ra^4 - Ri^4)], [A/c,2*B/d])

输出为:
Gr1 =
2*A + B

Gr2 =
2*A - B

关于MATLAB subs(): Strange behaviour,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29533352/

10-12 00:49