Skip to content
Tutorials Center
My learning

Topics

Lesson 28 of 45 ยท Numerical Methods

Polynomials and Curve Fitting

Work with polynomials in MATLAB and Octave: polyfit, polyval, roots, conv, least squares with the backslash operator, residuals and interp1, with Python stand-ins.

A polynomial such as 3x^2 - 2x + 5 is stored in MATLAB and Octave as a plain vector of coefficients, highest power first: [3 -2 5]. That simple convention gives you a small toolbox: evaluate, multiply, find roots, and, most importantly, fit a polynomial through measured data.

Fitting means choosing the curve that stays closest to noisy points. The standard criterion is least squares: minimize the sum of the squared vertical distances. MATLAB and Octave are not available in your browser here, so the MATLAB code is read-only; every example has a runnable Python/NumPy stand-in that follows the same steps and prints the same kind of results.

Polynomials are vectors

Coefficients [3 -2 5] means 3xยฒ โˆ’ 2x + 5.
polyval Evaluate at any x.
roots Where the polynomial is zero.
conv Multiply two polynomials.
1 / 2

Evaluating and multiplying

polyval(p, x) evaluates the polynomial with coefficients p at every element of x. conv(p, q) multiplies two polynomials; convolution of coefficient vectors is exactly polynomial multiplication. polyder and polyint differentiate and integrate.

MATLAB
p = [3 -2 5];
polyval(p, 2)             % 3*4 - 4 + 5 = 13
q = [1 1];                % x + 1
conv(p, q)                % [3 1 3 5]  ->  3x^3 + x^2 + 3x + 5
polyder(p)                % [6 -2]
polyint(p)                % [1 -1 5 0]

A missing power needs an explicit zero: x^3 - 1 is [1 0 0 -1], not [1 -1]. Forgetting zero coefficients is the most common mistake with these functions.

Finding roots

roots(p) returns all roots, real and complex, as a column vector. Internally it computes the eigenvalues of the companion matrix (see Linear Algebra Essentials). poly(r) goes the other way, building coefficients from roots.

MATLAB
roots([1 -3 2])           % x^2 - 3x + 2 = (x-1)(x-2):  [2; 1]
roots([1 0 1])            % x^2 + 1:  [0+1i; 0-1i]
poly([2 1])               % [1 -3 2]

The order of the returned roots is not something to rely on, and roots of high-degree polynomials with widely different scales are sensitive to rounding. For degree beyond about 10 treat the answers with suspicion.

Python Evaluate, multiply, differentiate and find roots

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Fitting with polyfit

p = polyfit(x, y, n) returns the coefficients of the degree-n polynomial that minimizes the sum of squared errors. Degree 1 is a straight line, degree 2 a parabola. Evaluate the fit with polyval.

MATLAB
x = 0:10;
y = [1.9 4.2 5.8 8.1 10.2 11.8 14.1 16.2 17.9 20.1 22.0];   % about 2x + 2, with noise
p = polyfit(x, y, 1)              % about [2.0 2.0]: slope and intercept
yfit = polyval(p, x);
plot(x, y, 'o', x, yfit, '-')
legend('data', 'linear fit')

The data must be vectors of equal length, and the number of points must exceed the degree. Plotting the data together with the fit (see 2-D Plots) is the quickest sanity check.

Python Fit a straight line and plot it

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Least squares by hand: the backslash operator

polyfit is a convenience wrapper. Behind it sits a linear system. For a line y = a*x + b, every data point gives one equation, so with 11 points you have 11 equations and only 2 unknowns. Such an overdetermined system has no exact solution, but the backslash operator solves it in the least-squares sense:

MATLAB
A = [x(:), ones(numel(x), 1)];    % columns: x and the constant 1
coef = A \ y(:)                   % same slope and intercept as polyfit

When A is not square, A \ b returns the vector that minimizes norm(A*c - b). This is more general than polyfit: put any functions you like in the columns, such as sin(x), exp(-x) or x.^2, and you fit a model that is linear in its coefficients. For a model like y = c1 + c2*x + c3*x.^2, use A = [ones(size(x(:))), x(:), x(:).^2].

Python Backslash and polyfit agree

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Residuals: how good is the fit?

A residual is the difference between a data point and the fit: r = y - yfit. Look at them in two ways.

  • Size: sqrt(mean(r.^2)) is the root-mean-square error, in the units of y. The R-squared value, 1 - sum(r.^2) / sum((y - mean(y)).^2), is the fraction of the variation explained by the model; 1 is a perfect fit.
  • Pattern: plot r against x. Random scatter around zero means the model is adequate. A curve or a trend means the model is missing something.

A higher degree always lowers the error on the data you have, because it has more freedom, but it can wiggle wildly between points (overfitting) and fail on new data. Compare degrees with data you did not use for the fit, and prefer the simplest model that leaves patternless residuals.

Python Residuals tell you when a line is not enough

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The straight line leaves a clear U-shaped pattern in the residuals, a signal that the curve bends; the parabola leaves only scatter.

Interpolation with interp1

Fitting smooths data. Interpolation instead passes through every point and estimates values between them. interp1(x, y, xi, method) evaluates at the new points xi.

MATLAB
x = [0 1 2 3 4];
y = [0 1 4 9 16];                 % squares
interp1(x, y, 2.5)                % 'linear' (default): 6.5
interp1(x, y, 2.5, 'spline')      % 6.25, exactly 2.5^2
interp1(x, y, 2.5, 'pchip')       % shape-preserving cubic
interp1(x, y, 5)                  % NaN: outside the data range
interp1(x, y, 5, 'linear', 'extrap')   % 25 by extending the last segment

x must be strictly increasing (or at least have no repeats). Linear is predictable; spline is smooth but can overshoot; pchip never overshoots the data. Interpolate to fill gaps and to resample, but fit when the data is noisy, because interpolation reproduces the noise faithfully.

Python Compare interpolation methods

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

NumPy's np.interp clamps outside the range instead of returning NaN as interp1 does, so check the edges when you translate code.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which vector represents x^3 - 1?

    Coefficients run from the highest power down and missing powers need explicit zeros: 1x^3 + 0x^2 + 0*x - 1.
  2. 2. What does conv([1 1], [1 -1]) return?

    (x + 1)(x - 1) = x^2 - 1, which is [1 0 -1]. Convolving coefficient vectors multiplies the polynomials.
  3. 3. What does A \ b compute when A has more rows than columns?

    For an overdetermined system backslash returns the least-squares solution, which is how polyfit works internally.
  4. 4. A plot of residuals against x shows a clear U shape. What does that suggest?

    Residuals of an adequate model look like random scatter. A systematic pattern means the model form is wrong.
  5. 5. When should you prefer polyfit over interp1?

    Interpolation reproduces the noise because it passes through every point; fitting smooths it.

Summary

  • A polynomial is a coefficient vector, highest power first, with zeros for missing powers.
  • polyval, conv, polyder, polyint, roots and poly evaluate, multiply, differentiate, integrate and convert polynomials.
  • polyfit(x, y, n) gives the least-squares polynomial; A \ y solves the same problem for any model that is linear in its coefficients.
  • Judge a fit by RMSE, R-squared and above all the pattern of the residuals; beware of overfitting.
  • interp1 passes through the data ('linear', 'spline', 'pchip') and returns NaN outside the range unless you ask for 'extrap'.

Next, solve nonlinear equations and optimize in Solving Equations and Optimization.