Apply simplify on individual coefficients after using the collect function
13 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Fabrice Demière
le 23 Déc 2017
Commenté : Tom Teasdale
le 9 Août 2022
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!
0 commentaires
Réponse acceptée
Walter Roberson
le 23 Déc 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 commentaire
Tom Teasdale
le 9 Août 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
Plus de réponses (0)
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!