Apply simplify on individual coefficients after using the collect function

14 views (last 30 days)
Hello everyone,
I have a complicated symbolic expression that I want to rewrite in terms x and y. So I use: collect(expression,[x,y])
I obtain some result in the form of c1*x+c2*y. Now I want to simplify the c1 and c2 coefficients while still retaining the collected form. Is there any way to do this? When I use simplify, it cancels the collecting of the terms.
I saw that is was possible to write : collect(expression,[x,y],simplify) in MuPad, but it doesn't seem to work in Live scripts.
Thanks!

Accepted Answer

Walter Roberson
Walter Roberson on 23 Dec 2017
No, there is not.
However, if you are fairly sure about the form of your expression, you can use coeffs to pull out the c1 and c2, which you would then be able to simplify and then reconstruct the expression.
You could also consider using children(). Unfortunately at the MATLAB level, when you use children() you cannot find out what the connecting operator is between the children -- unlike MuPAD. Doing any substantial symbolic Mathematics programming requires the ability to examine the operators such as _plus or atan2 to figure out which situation you are facing, but unfortunately the MATLAB level does not currently have any ability to give you this information short of using evalin(symengine) or fevel(symengine) to have MuPAD break up the expression for you.
  1 Comment
Tom Teasdale
Tom Teasdale on 9 Aug 2022
Perhaps this function might help some people, it does what Fabrice asked. It just assumes that an equation is passed and that the children of the equation are summands.
function eqn = simplifyCollect(eqn, expr, steps)
%SIMPLIFYCOLLECT Collects expression and simplifies collected children.
arguments
eqn sym
expr sym
steps (1,1) {mustBePositive} = 1
end
eqn = collect(eqn, expr);
c = children(rhs(eqn));
for i = 1:numel(c)
exp = c{i};
exp = simplify(exp, 'Steps', steps);
c{i} = exp;
end
eqn = lhs(eqn) == collect(sum([c{:}]), expr);
end

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!