matlabpolynomial-mathsymbolic-mathbinomial-coefficients# Get coefficients of 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 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?

Solution

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. :-)}

- how to reshape a cell array to matrix in desired size
- Using Mat_CreateVer for MATLAB versions higher than 7.3
- Making a numpy ndarray matrix symmetric
- Matlab 2021 and2023 cross compatability
- How to plot figures at the end of the for loop MATLAB?
- Variable transfer from App Designer to general .m script file
- Anomalous behavior of the Simulink signal - Simscape
- What is the fastest method to searsch for a part of a matrix inside of a large matrix?
- Can I Prevent Git from Using XTerm Escape Sequences?
- Use VPA to evaluate tiny probabilities
- Generate Array of Random Numbers with Specified Standard Deviation and Mean
- Matlab update plot with multiple data lines/curves
- Matlab vectorize for loop (grid depends on loop index)
- How to use a flag variable to break all the for-loops in spmd statement?
- How to produce the indexes from pdist2 function in Octave?
- Calculation of sensitivity and specificity from confusion matrix
- How do I read comma separated values from a .txt file in MATLAB using textscan()?
- How do you code Garson's algorithm in matlab to find the relative importance of parameters when training a neural network model?
- add data label to a grouped bar chart in matlab
- Using 1D convolutional with same shape so it will work with FFT?
- super fast median of matrix in OpenCV (as fast as MATLAB)
- How to create a struct or type from a dictionary in Julia?
- MATLAB Delay an Input Signal
- Is there an Octave equivalent of Matlab's `contains` function?
- Matlab define callback function for mouse click on a biograph
- getting a return value from a function in MATLAB
- How to convert nifti file to AFNI file?
- load .mat file, but not on the workspace
- Fast plane fitting to many points
- Cubic spline interpolation vs polynomial interpolation