4

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 symbolic (class sym).

I want to get the coefficients of the polynomial in the form [a b c], but I'm running into the following problem. If the function returns poly = y^2, then coeffs(poly) = 1. I don't want this – I want it to return [0 0 1].

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

horchler
  • 18,384
  • 4
  • 37
  • 73
Michelle
  • 663
  • 1
  • 8
  • 17

2 Answers2

7

You can use sym2poly if your polynomial is a function of a single variable like your example y^2:

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

which returns

c =

     2     3     4

Use fliplr(c) if you really want the coefficients in the other order. If you're going to be working with polynomials it would probably also be a good idea not to create a variable called poly, which is the name of a function you might want to use.

If you actually need to handle polynomials in multiple variables, you can use MuPAD functions from within Matlab. Here is how you can use MuPAD's coeff to get the coefficients in terms of the order of variable they precede (x or y):

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

If you want to extract all of the information from the polynomial, the poly2list function is quite helpful:

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

The polynomial can then be reconstructed via:

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

By the way, good choice on where you go to school. :-)

horchler
  • 18,384
  • 4
  • 37
  • 73
  • Hi @horchler, using Matlab R2019a gives the following error: `Error using mupadengine/feval (line 195) Invalid number of arguments.` The documentation for R2019a indicates that this should work, however (see the link you posted). I'm only able to get it to run the function with `m = eval(feval(symengine,'poly2list',p));` but I am working with a large number of variables and I want a particular order of coefficients. Any thoughts? – MattG Apr 04 '19 at 14:30
  • 1
    @MattG: looks like the problem may be that `poly2list` is not automatically converting the symbolic array `v` to a string anymore. As a workaround you can manually convert to a string: `vc=char(v);` `m=eval(feval(symengine,'poly2list',p,vc(9:end-2)));`. This may be a bug because MuPAD's `coeff` does not appear to have this issue. – horchler Apr 05 '19 at 16:06
  • 1
    Thanks for the tip! I ended up finding a solution in this Matlab Central question: https://www.mathworks.com/matlabcentral/answers/370283-how-to-use-poly2list-for-specific-variables – MattG Apr 06 '19 at 23:58
1

There is also an alternative to this problem. For a given degree, this function returns a polynomial of that degree and its coefficients altogether.

 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

Hope this helps.

Pouyan
  • 363
  • 5
  • 22