Get coefficients of a symbolic polynomial in Matlab

I have a Matlab function that returns a polynomial of the form:

poly = ax^2 + bx*y + cy^2

where a, b and c are constants, and x and y are symbols (class sym).

I want to get polynomial coefficients in the form [a b c], but I ran into the following problem. If the function returns poly = y^2, then coeffs(poly) = 1. I do not want this - I want him to return [0 0 1].

How to create a function that will give me the coefficients of a symbolic polynomial in the form in which I want?

+3
source share
2 answers

You can use sym2polyif your polynomial is a function of one variable, for example, your example y^2:

syms y
p = 2*y^2+3*y+4;
c = sym2poly(p)

c =

     2     3     4

fliplr(c), . , , poly, , .

, MuPAD Matlab. MuPAD coeff, , (x y):

syms x y
p = 2*x^2+3*x*y+4*y;
v = symvar(p);
c = eval(feval(symengine,'coeff',p,v))

, poly2list :

syms x y
p = 2*x^2+3*x*y+4*y;
v = symvar(p);
m = eval(feval(symengine,'poly2list',p,v));
c = m(:,1); % Coefficients
degs = m(:,2:end); % Degree of each variable in each term

:

sum(c.*prod(repmat(v,[size(m,1) 1]).^degs,2))

, , .: -) > >

+6

. . :

 function [polynomial, coefficeint] = makePoly(degree)
 syms x y 
 previous  = 0 ;
 for i=0:degree
     current = expand((x+y)^i);
     previous= current +  previous  ;   
 end
  [~,poly] = coeffs(previous);
 for j= 1:length(poly)
     coefficeint(j) = sym(strcat('a', int2str(j)) );
 end
 polynomial = fliplr(coefficeint)* poly.' ;
 end

, .

0

All Articles