Filter löschen
Filter löschen

How to use coeffs to a minimum power

2 Ansichten (letzte 30 Tage)
Connor LeClaire
Connor LeClaire am 4 Dez. 2021
Kommentiert: Star Strider am 5 Dez. 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?

Akzeptierte Antwort

Star Strider
Star Strider am 5 Dez. 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 Kommentare
Connor LeClaire
Connor LeClaire am 5 Dez. 2021
Awesome thanks!!!!
Star Strider
Star Strider am 5 Dez. 2021
As always, my pleasure!
.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by