polyfitΒΆ

polyfit finds the coefficients of a polynomial of degree N using the least squares method.

p = polyfit(x, y, N)

Return Value

p is a vector with N + 1 polynomial coefficients in descending powers:

P(x) = p_1x^n + p_2x^{n-1}+ \dotsb + p_{n}x + p_{n+1}

Parameters

x

x is a vector with x coordinates.

y

y is a vector with y coordinates. Same number of elements as x.

N

N is the degree of the polynomial. N >= 0 and N <= len(x)-1.

Example

def test_polyfit()
{
    x = 0:0.1:3;
    y = erf(x);
    print p = polyfit(x, y, 3);
    h1 = plot(x, y)
    y2 = polyval(p, x);
    h2 = plot(x, y2, h1[2])
    XYSetProps(h2[3], "curve-color='red'");
}
test_polyfit()

History

Version Description
R2013.2 Documented.

id-204970