Symbolic Toolbox: Coefficients and Powers of a polynomial

5 ビュー (過去 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 Center および File ExchangeSymbolic Math Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by