# root

Represent roots of polynomial

## Syntax

``r = root(p,x)``
``r = root(p,x,k)``

## Description

example

````r = root(p,x)` returns a column vector of numbered roots of symbolic polynomial `p` with respect to `x`. Symbolically solving a high-degree polynomial for its roots can be complex and not all polynomials can be solved analytically. In this case, the Symbolic Math Toolbox™ uses the `root` function to represent the roots of the polynomial.```

example

````r = root(p,x,k)` represents the `k`th root of symbolic polynomial `p` with respect to `x`.```

## Examples

collapse all

Represent the roots of the polynomial ${\mathit{x}}^{3}+1$ using `root`. The `root` function returns a column vector. The elements of this vector represent the three roots of the polynomial.

```syms x p = x^3 + 1; root(p,x)```
```ans =  $\left(\begin{array}{c}\mathrm{root}\left({x}^{3}+1,x,1\right)\\ \mathrm{root}\left({x}^{3}+1,x,2\right)\\ \mathrm{root}\left({x}^{3}+1,x,3\right)\end{array}\right)$```

$\mathrm{root}\left({\mathit{x}}^{3}+1,\mathit{x},1\right)$ represents the first root of `p`, while $\mathrm{root}\left({\mathit{x}}^{3}+1,\mathit{x},2\right)$ represents the second root, and so on. Use this syntax to represent roots of high-degree polynomials.

Find the roots of the quadratic polynomial ${x}^{2}-x-1$. You can use the `root` function to represent these roots.

```syms x p = x^2 - x - 1; r = root(p,x)```
```r =  $\left(\begin{array}{c}\mathrm{root}\left({x}^{2}-x-1,x,1\right)\\ \mathrm{root}\left({x}^{2}-x-1,x,2\right)\end{array}\right)$```

To convert these roots to high-precision floating point numbers, you can use `vpa`.

`rVpa = vpa(r)`
```rVpa =  $\left(\begin{array}{c}-0.61803398874989484820458683436564\\ 1.6180339887498948482045868343656\end{array}\right)$```

When solving a high-degree polynomial, `solve` represents the roots by using `root`. Alternatively, you can either return an explicit solution by using the `MaxDegree` option or return a numerical result by using `vpa`.

Find the roots of `x^3 + 3*x - 16`.

```syms x p = x^3 + 3*x - 16; R = solve(p,x)```
```R =  $\left(\begin{array}{c}\mathrm{root}\left({z}^{3}+3 z-16,z,1\right)\\ \mathrm{root}\left({z}^{3}+3 z-16,z,2\right)\\ \mathrm{root}\left({z}^{3}+3 z-16,z,3\right)\end{array}\right)$```

Find the roots explicitly by setting the `MaxDegree` option to the degree of the polynomial. Polynomials with a degree greater than `4` do not have explicit solutions.

`Rexplicit = solve(p,x,"MaxDegree",3)`
```Rexplicit =  ```

Calculate the roots numerically by using `vpa` to convert `R` to high-precision floating point.

`Rnumeric = vpa(R)`
```Rnumeric =  $\left(\begin{array}{c}2.1267693318103912337456401562601\\ -1.0633846659051956168728200781301-2.5283118563671914055545884653776 \mathrm{i}\\ -1.0633846659051956168728200781301+2.5283118563671914055545884653776 \mathrm{i}\end{array}\right)$```

If the call to `root` contains parameters, substitute the parameters with numbers by using `subs` before calling `vpa`.

You can use the `root` function as input to Symbolic Math Toolbox functions such as `simplify`, `subs`, and `diff`.

Simplify an expression containing `root` using the `simplify` function.

```syms x r = root(x^6 + x, x, 1); simplify(sin(r)^2 + cos(r)^2)```
`ans = $1$`

Substitute for parameters in `root` with numbers using `subs`.

```syms b subs(root(x^2 + b*x, x, 1), b, 5)```
`ans = $\mathrm{root}\left({x}^{2}+5 x,x,1\right)$`

Substituting for parameters using `subs` is necessary before converting `root` to numeric form using `vpa`.

Differentiate an expression containing `root` with respect to a parameter using `diff`.

`diff(root(x^2 + b*x, x, 1), b)`
```ans =  $\frac{\mathrm{root}\left({x}^{2}+b x,x,1\right)}{b}$```

Find the inverse Laplace transform of a ratio of two polynomials using `ilaplace`. The inverse Laplace transform is returned in terms of `root`.

```syms s G = (s^3 + 1)/(s^6 + s^5 + s^2); H = ilaplace(G)```
```H =  $t-\left({\sum }_{k=1}^{4}\frac{{\mathrm{e}}^{t \mathrm{root}\left({z}^{4}+{z}^{3}+1,z,k\right)}}{4 \mathrm{root}\left({z}^{4}+{z}^{3}+1,z,k\right)+3}\right)$```

When you get the `root` function in output, you can use the `root` function as input in subsequent symbolic calculations. However, if a numerical result is required, convert the `root` function to a high-precision numeric result using `vpa`.

Convert the inverse Laplace transform to numeric form using `vpa`.

`H_vpa = simplify(vpa(H))`
`H_vpa = $t+0.30881178580997278695808136329347 {\mathrm{e}}^{-1.0189127943851558447865795886366 t} \mathrm{cos}\left(0.60256541999859902604398442197193 t\right)-0.30881178580997278695808136329347 {\mathrm{e}}^{0.5189127943851558447865795886366 t} \mathrm{cos}\left(0.666609844932018579153758800733 t\right)-0.6919689479355443779463355813596 {\mathrm{e}}^{-1.0189127943851558447865795886366 t} \mathrm{sin}\left(0.60256541999859902604398442197193 t\right)-0.16223098826244593894459034019473 {\mathrm{e}}^{0.5189127943851558447865795886366 t} \mathrm{sin}\left(0.666609844932018579153758800733 t\right)$`

## Input Arguments

collapse all

Symbolic polynomial, specified as a symbolic expression.

Variable, specified as a symbolic variable.

Number of polynomial root, specified as a number, vector, matrix, multidimensional array, or a symbolic number, vector, matrix, or multidimensional array. When `k` is a nonscalar, `root` acts element-wise on `k`.

Example: `root(f,x,3)` represents the third root of `f`.

## Tips

• You can numerically approximate a symbolic expression involving the `root` function by using `vpa` to return variable-precision symbolic numbers. Starting in R2023a, you can convert the expression to a MATLAB® function that can be used without Symbolic Math Toolbox by using `matlabFunction`. The generated file uses the `roots` function that operates on the numeric `double` data type.

## Version History

Introduced in R2015b