필터 지우기
필터 지우기

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 CenterFile Exchange에서 Number Theory에 대해 자세히 알아보기

태그

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by