Skip to content

polyfit

Polynomial curve fit by least squares.

p = polyfit(x, y, n)
[p, S, mu] = polyfit(x, y, n)

Fits a polynomial of degree n to the data (x, y) in a least-squares sense. Returns p, a row vector of coefficients in descending order of power. Optional S is a structure usable by polyval for confidence intervals. Optional mu = [mean(x), std(x)] returns the centering / scaling parameters used internally for numerical stability with high-degree fits.

x = 1:10; y = 2*x + 3 + randn(1,10)*0.1;
p = polyfit(x, y, 1) % approximately [2 3]
  • polyval — Evaluate a polynomial.
  • linsolve — Solve a linear system with optional structure hints.