Clear Filters
Clear Filters

How to sort solution of multiple terms by variables?

2 views (last 30 days)
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!

Accepted Answer

Nikolai Jakuba
Nikolai Jakuba on 20 Jun 2018
Look here: https://de.mathworks.com/help/symbolic/collect.html.
  1 Comment
Arno Kinnen
Arno Kinnen on 20 Jun 2018
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?

Sign in to comment.

More Answers (0)

Categories

Find more on Symbolic Math Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!