Skip to content

Polynomials

Alvaro edited this page Aug 17, 2016 · 5 revisions

Poly1d

Fitting

polyfit(x, y, deg) Least squares polynomial fit.

numpy.polyfit

Least squares polynomial fit.

Parameters:
x : array_like, x-coordinates of the M sample points (x[i], y[i]).
y : array_like, y-coordinates of the sample points. Several data sets of sample points sharing the same x-coordinates can be fitted at once by passing in a 2D-array that contains one dataset per column.
deg : int. Degree of the fitting polynomial.

Returns:
p : ndarray. Polynomial coefficients, highest power first.

Example:

Python

>>> import numpy
>>> x = numpy.array([0.0, 1.0, 2.0, 3.0,  4.0,  5.0])
>>> y = numpy.array([0.0, 0.8, 0.9, 0.1, -0.8, -1.0])
>>> numpy.polyfit(x, y, 3)
    array([ 0.08703704, -0.81349206,  1.69312169, -0.03968254])

Javascript with PyExtJS
> x = [0.0, 1.0, 2.0, 3.0,  4.0,  5.0];
  [0.0, 1.0, 2.0, 3.0,  4.0,  5.0]
> y = [0.0, 0.8, 0.9, 0.1, -0.8, -1.0];
  [0.0, 0.8, 0.9, 0.1, -0.8, -1.0]
> numpy.polyfit(x, y, 3);
  [0.0870370370370341, -0.8134920634920405, 1.6931216931216477, -0.039682539682528106]