# Working with Polynomials in MATLAB: Video Tutorial

URL: https://yarpiz.com/working-with-polynomials-video-tutorial-yptnm190520s16
Author: Mostapha Kalami Heris
Published: 2019-07-03
Updated: 2026-10-30
Topics: Courses

> Work with MATLAB coefficient vectors to evaluate, fit, multiply, divide, differentiate and integrate polynomials. Check roots and division remainders.

This video tutorial shows how to work with polynomials in MATLAB. It reviews the functions that evaluate polynomials, find and use their roots, fit them to data, multiply and divide them, and compute their derivatives and integrals. To watch the full course, see [Numerical Computations in MATLAB](https://yarpiz.com/numerical-computations-in-matlab-video-tutorial-572).

## How MATLAB represents a polynomial

MATLAB stores the polynomial

$$
p(x) = a_n x^n + a_{n-1} x^{n-1} + \dots + a_1 x + a_0
$$

as the row vector of its coefficients, from the highest power down to the constant term: `[an an-1 ... a1 a0]`. A missing power gets a zero. All the functions in this video use this form. The main example is

$$
p(x) = x^3 - 2x + 5
$$

which is the vector `[1 0 -2 5]`, because the $x^2$ term is absent.

## Watch online

The video is available on YouTube and is embedded below.

::: video {youtube="HPsRYJKnAMA" title="Working with Polynomials in MATLAB: Video Tutorial"}
:::

## What the video covers

Working with Polynomials ([YouTube](https://youtu.be/HPsRYJKnAMA)) introduces the functions one by one. Several of them come in pairs, where one function undoes the other. The times below mark where each topic starts.

### Evaluating a polynomial with `polyval` (from 2:43)

`polyval(p, x)` evaluates the polynomial at one point or at a whole vector of points. For the example, $p(0) = 5$ and $p(1) = 4$. Evaluated on many points, the plot shows one real root a little below $-2$.

```matlab {title="Evaluate a polynomial at several points"}
p = [1 0 -2 5];
polyval(p, [0 1 2])   % 5  4  9
```

### Roots and polynomials with `roots` and `poly` (from 5:22)

`roots(p)` returns the roots: one real root (about $-2.09$) and a pair of complex conjugate roots. A polynomial of odd degree with real coefficients always has at least one real root. `poly(r)` does the opposite: it builds the polynomial from a list of roots.

The two are not complete inverses. The polynomial $2p(x)$ has the same roots as $p(x)$, and `poly` always returns a polynomial whose first coefficient is 1.

### Fitting a polynomial with `polyfit` (from 8:01)

Four points of $p(x)$, at $x = -1, 0, 2, 3$, are passed to `polyfit` with a degree of 3, and it returns `[1 0 -2 5]`, the original polynomial. Lower degrees give the closest polynomial of that degree to the same data. The constant fit is $11.5$, the mean of the four values. A loop fits degrees 0 to 3 and plots the four approximations together, with a legend made from `DisplayName`. Fitting noisy data is covered in the [interpolation and curve fitting tutorial](https://yarpiz.com/interpolation-video-tutorial-yptnm190520s07).

### Multiplication and division with `conv` and `deconv` (from 13:31)

Multiplying two polynomials is the same as convolving their coefficient vectors. With $q(x) = x^2 + 2$:

$$
p(x)\,q(x) = (x^3 - 2x + 5)(x^2 + 2) = x^5 + 5x^2 - 4x + 10
$$

```matlab {title="Multiply and divide polynomials"}
q = [1 0 2];
r = conv(p, q)      % [1 0 0 5 -4 10]
deconv(r, p)        % returns q
```

`deconv` divides polynomials and can return both a quotient and a remainder. In this example the divisions are exact: `deconv(r, p)` returns $q$ and `deconv(r, q)` returns $p$. A general division need not have a zero remainder.

### Derivatives and integrals with `polyder` and `polyint` (from 16:17)

`polyder` returns the exact derivative. For the examples, $p'(x) = 3x^2 - 2$, which is `[3 0 -2]`, and $q'(x) = 2x$.

`polyint` returns the integral. By default the constant of integration is 0, so `polyint(p)` gives $\tfrac{1}{4}x^4 - x^2 + 5x$. A second input sets the constant: `polyint(q, -1)` gives $\tfrac{1}{3}x^3 + 2x - 1$. Taking the derivative of an integral returns the original polynomial, while integrating a derivative loses the constant term unless you pass it back.

## Summary of the functions

| Task | Function | Inverse |
| --- | --- | --- |
| Evaluate | `polyval` | |
| Roots | `roots` | `poly` |
| Fit to data | `polyfit` | |
| Multiply | `conv` | `deconv` |
| Differentiate | `polyder` | `polyint` |

## Check the calculation

Keep zero coefficients for missing powers. Dropping one changes the polynomial rather than shortening its representation. Evaluate a polynomial at its computed roots to inspect the residuals, and reconstruct a division from the divisor, quotient and remainder. High degree polynomials and repeated or closely spaced roots can be sensitive to rounding, so avoid treating every displayed digit as accurate.

## How to cite

Mostapha Kalami Heris, Working with Polynomials in MATLAB: Video Tutorial (YPTNM190520S16), Yarpiz, 2019. https://yarpiz.com/working-with-polynomials-video-tutorial-yptnm190520s16
