Skip to content

MATLAB - Polynomials

Polynomials are a fundamental tool in engineering and science for modeling data, approximating complex functions, and designing systems. MATLAB provides a suite of simple yet powerful functions for polynomial manipulation, fitting, and evaluation.

In MATLAB, a polynomial is represented by a row vector containing its coefficients in order of descending powers. For example, the polynomial P(x) = x⁴ + 7x³ - 5x + 9 is represented as:

% Note the '0' coefficient for the missing x^2 term.
% This is crucial for the representation to be correct.
p = [1 7 0 -5 9];

The polyval function evaluates a polynomial at a given value or set of values. This is useful for plotting or for calculating the polynomial’s output for specific inputs.

p = [1 7 0 -5 9];
% Evaluate at a single point x = 4
y_single = polyval(p, 4)
% Evaluate at multiple points to generate data for plotting
x_vector = -8:0.1:2;
y_vector = polyval(p, x_vector);
% Plot the polynomial
figure;
plot(x_vector, y_vector);
title('Plot of P(x) = x^4 + 7x^3 - 5x + 9');
xlabel('x');
ylabel('P(x)');
grid on;

MATLAB also provides polyvalm for evaluating a polynomial with a matrix argument, which is a common operation in linear algebra and control systems theory.

The roots function calculates the roots of a polynomial (i.e., the values of x for which P(x) = 0).

p = [1 7 0 -5 9];
r = roots(p)

MATLAB will return the four roots of the polynomial, which can be real or complex:

r =
-6.8661 + 0.0000i
-1.4247 + 0.0000i
0.6454 + 0.7095i
0.6454 - 0.7095i

Conversely, the poly function can reconstruct the polynomial coefficient vector from its roots.

% Reconstruct the polynomial from its roots
p2 = poly(r)
% Note: Due to floating-point arithmetic, the result might have tiny imaginary parts.
% You can use real() to discard them if you know the coefficients should be real.
p2_real = real(p2)

Perhaps the most common use of polynomials is fitting a curve to a set of data points. The polyfit function finds the coefficients of a polynomial that best fits the data in a least-squares sense.

Imagine you have the following measurements from a sensor over time. We want to find a smooth curve that models this data.

% Measured data points
x_data = [1 2 3 4 5 6];
y_data = [5.5 43.1 128 290.7 498.4 978.7];
% Fit a 4th-degree polynomial to the data
fit_degree = 4;
p_fit = polyfit(x_data, y_data, fit_degree);
% To plot the fitted curve, evaluate the polynomial over a finer grid
x_fine = 1:0.1:6;
y_fit = polyval(p_fit, x_fine);
% --- Visualization ---
figure;
% Plot original data as circles
plot(x_data, y_data, 'o', 'MarkerSize', 8, 'DisplayName', 'Measured Data');
hold on; % Hold the plot to add the fitted curve
% Plot the fitted polynomial curve
plot(x_fine, y_fit, 'r-', 'LineWidth', 2, 'DisplayName', '4th-Degree Fit');
hold off;
% Add labels and a legend for clarity
grid on;
xlabel('Time (s)');
ylabel('Sensor Output');
title('Polynomial Fit to Sensor Data');
legend('Location', 'northwest');
  • Choosing the Right Degree: Be careful with polyfit. A high-degree polynomial can overfit your data, perfectly matching the sample points but behaving erratically between them. This leads to poor predictions. Always start with a low degree (e.g., 1 or 2) and increase it only if necessary. Visualizing the fit is essential!
  • Interpolation vs. Fitting: polyfit finds an approximating function (it doesn’t have to pass through all points). If you need a function that passes exactly through every data point, you are looking for interpolation (e.g., interp1).
  • Numerical Stability: For very high-degree polynomials or poorly scaled data, polyfit can issue warnings about numerical instability. In such cases, consider normalizing your data (e.g., scaling x to be in the range [-1, 1]) before fitting.
  • Advanced Fitting: For more complex modeling beyond simple polynomials, explore the Curve Fitting Toolbox™, which provides an interactive app (cftool) and the fit function for a wide range of models (exponentials, Fourier series, custom equations).