# poly2sym

Create symbolic polynomial from vector of coefficients

## Syntax

``p = poly2sym(c)``
``p = poly2sym(c,var)``

## Description

example

````p = poly2sym(c)` creates the symbolic polynomial expression `p` from the vector of coefficients `c`. The polynomial variable is `x`. If `c = [c1,c2,...,cn]`, then ```p = poly2sym(c)``` returns ${c}_{1}{x}^{n-1}+{c}_{2}{x}^{n-2}+...+{c}_{n}$.This syntax does not create the symbolic variable `x` in the MATLAB® Workspace.```

example

````p = poly2sym(c,var)` uses `var` as a polynomial variable when creating the symbolic polynomial expression `p` from the vector of coefficients `c`.```

## Examples

collapse all

Create a polynomial expression from a symbolic vector of coefficients. If you do not specify a polynomial variable, `poly2sym` uses `x`.

```syms a b c d p = poly2sym([a,b,c,d])```
`p = $a {x}^{3}+b {x}^{2}+c x+d$`

Create a polynomial expression from a symbolic vector of rational coefficients.

`p = poly2sym(sym([1/2,-1/3,1/4]))`
```p =  $\frac{{x}^{2}}{2}-\frac{x}{3}+\frac{1}{4}$```

Create a polynomial expression from a numeric vector of floating-point coefficients. The toolbox converts floating-point coefficients to rational numbers before creating a polynomial expression.

`p = poly2sym([0.75,-0.5,0.25])`
```p =  $\frac{3 {x}^{2}}{4}-\frac{x}{2}+\frac{1}{4}$```

Create a polynomial expression from a symbolic vector of coefficients. Use `t` as a polynomial variable.

```syms a b c d t p = poly2sym([a,b,c,d],t)```
`p = $a {t}^{3}+b {t}^{2}+c t+d$`

To use a symbolic expression, such as `t^2 + 1` or `exp(t)`, instead of a polynomial variable, substitute the variable using `subs`.

`p1 = subs(p,t,t^2 + 1)`
`p1 = $d+a {\left({t}^{2}+1\right)}^{3}+b {\left({t}^{2}+1\right)}^{2}+c \left({t}^{2}+1\right)$`
`p2 = subs(p,t,exp(t))`
`p2 = $d+c {\mathrm{e}}^{t}+a {\mathrm{e}}^{3 t}+b {\mathrm{e}}^{2 t}$`

Create a polynomial expression from a numeric vector of integer coefficients.

```p_coeffs = [1 4 5 4 4]; p = poly2sym(p_coeffs)```
`p = ${x}^{4}+4 {x}^{3}+5 {x}^{2}+4 x+4$`

Because `poly2sym` does not create the symbolic variable `x` in the workspace, create this variable by using `syms`. Find the roots of the polynomial by using `solve`.

```syms x p_roots = solve(p,x)```
```p_roots =  $\left(\begin{array}{c}-2\\ -2\\ -\mathrm{i}\\ \mathrm{i}\end{array}\right)$```

The polynomial has 4 roots. To check if these roots are indeed the correct solution, you can reconstruct the original polynomial from the roots.

Find the factored form of the polynomial by subtracting each root from `x`.

`p_elem = x-p_roots`
```p_elem =  $\left(\begin{array}{c}x+2\\ x+2\\ x+\mathrm{i}\\ x-\mathrm{i}\end{array}\right)$```

Take the product of the factored form of the polynomial.

`p_new = prod(p_elem)`
`p_new = ${\left(x+2\right)}^{2} \left(x-\mathrm{i}\right) \left(x+\mathrm{i}\right)$`

Expand the polynomial and confirm that the result is the same as the original expression.

`p_new = expand(p_new)`
`p_new = ${x}^{4}+4 {x}^{3}+5 {x}^{2}+4 x+4$`

## Input Arguments

collapse all

Polynomial coefficients, specified as a numeric or symbolic vector. Argument `c` can be a column or row vector.

Polynomial variable, specified as a symbolic variable.

## Output Arguments

collapse all

Polynomial, returned as a symbolic expression.

## Tips

• When you call `poly2sym` for a numeric vector `c`, the toolbox converts the numeric vector to a vector of symbolic numbers using the default (rational) conversion mode of `sym`.

## Version History

Introduced before R2006a