How to use coeffs to a minimum power

1 vue (au cours des 30 derniers jours)
Connor LeClaire
Connor LeClaire le 4 Déc 2021
Commenté : Star Strider le 5 Déc 2021
Hello,
Stemming from a recent post here I wanted to ask a question about using coeffs. As the title says, is it possible to set coeffs to find all coefficients of an equation up to a power of the variable (or a minimum power if a larger power exists) for instance:
syms x
y = x + 3;
[c p] = coeffs(y,x,'all')
c = 
p = 
However in my application I need to collect all terms of x^2 from a series of equations, so for the above I would want something like:
c = (0, 1, 3)
p = (x^2, x, 1)
to make it easy. Is it possible to force coeffs to go upto a minimum power of x without altering the equation itself?

Réponse acceptée

Star Strider
Star Strider le 5 Déc 2021
One approach —
syms x
eqn(1,:) = 5*x^2 + 2*x + 8;
eqn(2,:) = 42*x^3 + 3*x^2 + 3;
eqn
eqn = 
for k = 1:numel(eqn)
[cfs,px] = coeffs(eqn(k),'All');
xsq = find(ismember(px, x^2)); % Index Oof 'x^2' Terms
xsqcf{k} = cfs(xsq); % Coefficient Of 'x^2' Term
end
xsqcf{1}
ans = 
5
xsqcf{2}
ans = 
3
I have not tested this for robustness to other conditions. It works here.
.
  6 commentaires
Connor LeClaire
Connor LeClaire le 5 Déc 2021
Awesome thanks!!!!
Star Strider
Star Strider le 5 Déc 2021
As always, my pleasure!
.

Connectez-vous pour commenter.

Plus de réponses (0)

Tags

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by