필터 지우기
필터 지우기

Symbolic Toolbox: Coefficients and Powers of a polynomial

조회 수: 3 (최근 30일)
Christoph
Christoph 2014년 1월 23일
답변: Mohamad Hejazi Dinan 2020년 7월 22일
Hey,
suppose I have a (maybe multidimensional) polynomial given in a symbolic way. What I want is all the coefficients of the different terms with knowledge about the power of the variable at this point. So a perfect solution would be something like this
Input: 3 * x^5 + 2 * x^3 + 23
Output: Coeffs: [3 2 23], Powers: [5 3 0]
Or something like the polynomial in matlab, i.e.
output: [3 0 2 0 0 23]
I actually wrote a small script than can already do what I want:
syms x
f = 3 * x^5 + 2 * x^3 + 23
[polyCoeffs, mononomials] = coeffs(f)
% output is
% polyCoeffs = [ 3, 2, 23]
% mononomials = [ x^5, x^3, 1]
polyPowers = zeros(1, length(mononomials))
for i=1:length(mononomials)
polyPowers(i) = length(sym2poly(polyPowers(i))) - 1;
end
It does the job, but it is very unelegant and - if there are more than one variable in the polynomial - in can get a little bit more complicated. So I'd like to know if anyone has a smarter idea, or if there is something in Matlab I just haven't found yet.
Thanks

답변 (2개)

Haroon
Haroon 2019년 1월 21일
The above code will not work well. I have modified it and now it is working fine.
syms x
f = 3 * x^5 + 2 * x^3 + 23
[polyCoeffs, mononomials] = coeffs(f);
poly=sym2poly(f);
polyPowers = zeros(1, length(mononomials));
j=0;
for i=1:length(poly)
if poly(i)~=0
j=j+1;
polyPowers(j)= length(poly)-i;
end
end
display(polyPowers)

Mohamad Hejazi Dinan
Mohamad Hejazi Dinan 2020년 7월 22일
Use the function polyPowers

카테고리

Help CenterFile Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by