TExcelTAD
Home › MATLAB / Octave › Polynomials

Polynomials

MATLAB represents a polynomial as a row vector of coefficients, highest power first. The polynomial

p(x) = 2x³ − 3x² + 0x + 5

is the vector [2 -3 0 5]. Don't forget the zeros for missing powers: every position matters.

Evaluating: polyval

polyval(p, x) evaluates the polynomial at x, which can be a scalar or an array:

matlab · try it yourself
p = [2 -3 0 5];
disp(polyval(p, 2))              % 2*8 - 3*4 + 0 + 5
disp(polyval(p, [-1 0 1 2 3]))
9
    0    5    4    9   32

Roots

roots(p) finds all values of x where p(x) = 0. A polynomial of degree n has n roots, which may be complex:

matlab · try it yourself
r = roots([1 -5 6])              % x^2 - 5x + 6 = (x - 2)(x - 3)
r2 = roots([1 0 4]);             % x^2 + 4 has complex roots
fprintf('%g%+gi\n', [real(r2) + 0, imag(r2)]');   % + 0 turns a -0 into 0
r =

   3
   2

0+2i
0-2i

poly(r) goes the other way: it builds the coefficient vector from a list of roots.

matlab · try it yourself
p = poly([1 -2 4])
fprintf('check: p(4) = %g\n', polyval(p, 4));
p =

   1  -3  -6   8

check: p(4) = 0

Arithmetic on polynomials

Operation Function
Add / subtract + / - (pad the shorter vector with leading zeros)
Multiply conv(p, q)
Divide [q, r] = deconv(p, d) (quotient and remainder)
Derivative polyder(p)
Integral polyint(p) (constant of integration 0)
matlab · try it yourself
a = [1 2];          % x + 2
b = [1 -3];         % x - 3
prodAB = conv(a, b)                 % x^2 - x - 6
[q, r] = deconv([1 0 0 -8], [1 -2]);    % (x^3 - 8) / (x - 2)
fprintf('quotient %s, remainder %s\n', mat2str(q), mat2str(r));
disp(polyder([1 0 -3 2]))           % derivative of x^3 - 3x + 2
disp(polyint([3 2 1]))              % integral of 3x^2 + 2x + 1
prodAB =

   1  -1  -6

quotient [1 2 4], remainder [0 0 0 0]
   3   0  -3
   1   1   1   0

Curve fitting: polyfit

polyfit(x, y, n) finds the polynomial of degree n that best fits the data points in the least-squares sense. Degree 1 gives a straight line (linear regression):

matlab · try it yourself
temp = [10 15 20 25 30];          % degrees C
sold = [110 145 205 240 290];     % ice creams sold
p = polyfit(temp, sold, 1);
fprintf('sold = %.2f * temp + %.2f\n', p(1), p(2));
fprintf('prediction at 28 C: %.0f\n', polyval(p, 28));
residuals = sold - polyval(p, temp);
fprintf('residuals: %s\n', mat2str(round(residuals * 10) / 10));
sold = 9.10 * temp + 16.00
prediction at 28 C: 271
residuals: [3 -7.5 7 -3.5 1]

A higher degree follows the data more closely but can overfit: with as many coefficients as data points, the curve passes through every point and wiggles wildly in between. A quadratic fit to slightly curved data:

matlab · try it yourself
t = 0:5;
h = [2.0 6.9 10.1 11.2 10.3 7.1];      % height of a thrown ball (m)
p2 = polyfit(t, h, 2);
fprintf('h(t) = %.3f t^2 + %.3f t + %.3f\n', p2);
tPeak = -p2(2) / (2 * p2(1));          % vertex of the parabola
fprintf('peak at t = %.2f s, height %.2f m\n', tPeak, polyval(p2, tPeak));
h(t) = -1.016 t^2 + 6.132 t + 1.918
peak at t = 3.02 s, height 11.17 m

To see the fit, plot the data as markers and the polynomial on a fine grid:

matlab
tt = linspace(0, 5, 100);
plot(t, h, 'o', tt, polyval(p2, tt), '-')
legend('data', 'quadratic fit')

Test yourself

Which vector represents x³ + 4?

  1. [1 4]
  2. [1 0 0 4]
  3. [4 0 0 1]
  4. [1 4 0 0]

Which function multiplies two polynomials?

  1. *
  2. .*
  3. conv
  4. polymul

What does polyfit(x, y, 1) return?

  1. The correlation of x and y
  2. Slope and intercept of the best-fit line
  3. The roots of y
  4. A plot