How to sort solution of multiple terms by variables?

1 次查看(过去 30 天)
I have a solution which consists of multiple terms. Some of these terms have a common factor (a variable created with "syms", i.e. "k"). I want to sort these by that variable so that I get: k(term1 + term2) instead of term1*k + term2*k. I want to do this for multiple variables and terms (of the same solution). I tried simplify() but this didn't work. I use MATLAB R2018a.
What I have:
syms m s k X Y1 Y2 F1 F2
eqns = [m*s^2*Y1 == -k*(Y1-X)-k*(Y1-Y2)+F1 , m*s^2*Y2 == -k*(Y2-Y1)+F2];
vars = [Y1 Y2];
[Y1,Y2] = solve(eqns,vars)
The solution:
Y1 = (F1*k + F2*k + X*k^2 + F1*m*s^2 + X*k*m*s^2)/(k^2 + 3*k*m*s^2 + m^2*s^4)
Y2 = (F1*k + 2*F2*k + X*k^2 + F2*m*s^2)/(k^2 + 3*k*m*s^2 + m^2*s^4)
What I want: sort by X, F1 and F2 like below
Y1 = ((m*k*s^2+k^2)*X + (m*s^2+k)*F1 + k*F2)/(k^2 + 3*k*m*s^2 + m^2*s^4)
Y2 = (k^2*X + k*F1 + (m*s^2+2*k)*F2)/(k^2 + 3*k*m*s^2 + m^2*s^4)
Is there a way to do this?
Thanks in advance!

采纳的回答

Nikolai Jakuba
Nikolai Jakuba 2018-6-20
Look here: https://de.mathworks.com/help/symbolic/collect.html.
  1 个评论
Arno Kinnen
Arno Kinnen 2018-6-20
This worked, only now I have 3 collected terms with 3 times the same denominator. Is there a way to get these terms together on that denominator?

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Symbolic Math Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by