How to use coeffs to a minimum power

1 view (last 30 days)
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?

Accepted Answer

Star Strider
Star Strider on 5 Dec 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 Comments

Sign in to comment.

More Answers (0)

Tags

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!