フィルターのクリア

How to use coeffs to a minimum power

2 ビュー (過去 30 日間)
Connor LeClaire
Connor LeClaire 2021 年 12 月 4 日
コメント済み: Star Strider 2021 年 12 月 5 日
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?

採用された回答

Star Strider
Star Strider 2021 年 12 月 5 日
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 件のコメント
Connor LeClaire
Connor LeClaire 2021 年 12 月 5 日
Awesome thanks!!!!
Star Strider
Star Strider 2021 年 12 月 5 日
As always, my pleasure!
.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNumber Theory についてさらに検索

タグ

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by