Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MATLAB subs(): Strange behaviour

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])

returns

Gr1 =

2*A + B


Gr2 =

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

Is there a way to convince MATLAB to return Gr2 = 2*A - B in the second case without workarounds? I have a much more complex expression to substitute, but I can't work with this subs()-behaviour. Thanks.

like image 446
Thomas Avatar asked Nov 10 '22 16:11

Thomas


1 Answers

I think the problem is in the multiplication/division operations in the second argument of subs since the problem vanishes once I rearrange the code as follows:

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])

and the output is:

Gr1 =
2*A + B

Gr2 =  
2*A - B
like image 137
brainkz Avatar answered Nov 15 '22 07:11

brainkz