Scilab Website | Contribute with GitLab | Mailing list archives | ATOMS toolboxes
Scilab Online Help
2025.0.0 - English


polyfit

Polynomial curve fitting

Syntax

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

Arguments

x

real or complex vector/matrix

y

real or complex vector/matrix. y must have the same size as x

n

an integer, n>=0. It is a degree of the fitting polynomial. Or a polynom. In the case, polyfit extracts the degree of polynom and returns a polynom containing the coefficients.

p

a 1 x n+1 real or complex vector or polynom, the polynomial coefficients

S

a structure containing the following fields:

R

a matrix of doubles, the triangular factor R form the qr decomposition

df

a real, the degrees of freedom

normr

a real, the norm of the residuals

mu

a 1 x 2 vector. mu(1) is mean(x) and mu(2) is stdev(x)

Description

p = polyfit(x, y, n) returns a vector of coefficients of a polynomial p(x) of degree n:

p(x) = p_1 x^n + p_2 x^{n-1} + ... + p_n x + p_{n+1}.

Depending on the type of n, p will be a real or complex vector or a polynom.

p can be used with polyval function to evaluate the polynomial at the data points.

[p, S] = polyfit(x, y, n) returns a vector of coefficients of a polynomial and a structure S that can be used with polyval to compute the estimated error of the predicted values.

[p, S, mu] = polyfit(x, y, n) returns a third output argument mu containing [mean(x), stdev(x)]. x is centered at zero and scaled to have unit standard deviation.

Examples

example 1 - p = polyfit(x, y, n)

x = 1:5;
y = #(x) -> (-2*x.^4 + x.^3 - 5 * x.^2 + 6 *x -2);
p = polyfit(x, y(x), 3)

xx = linspace(1, 5, 100);
yy = polyval(p, xx);

plot(x, y(x), "b.", "thickness", 2);
plot(xx, yy, "r");

example 2 - p = polyfit(x, y, n) with n a polynom

x = 1:5;
y = #(x) -> (-2*x.^4 + x.^3 - 5 * x.^2 + 6 *x -2);
p = polyfit(x, y(x), %s^3)

xx = linspace(1, 5, 100);
yy = polyval(p, xx);

plot(x, y(x), "b.", "thickness", 2);
plot(xx, yy, "r");

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

x = 0:10;
y = #(x) -> (x.^3 - 3*x + 2);

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

xx = linspace(0, 10, 100);
yy = polyval(p, xx, S, mu);

plot(x, y(x), "b.", "thickness", 2);
plot(xx, y(xx), "g");
plot(xx, yy, "r");

See also

  • vander — Vandermonde matrix
  • polyval — evaluates the polynomial for given values

History

VersionDescription
2025.0.0 Introduction in Scilab.
Report an issue
<< kmeans Statistics polyval >>

Copyright (c) 2022-2024 (Dassault Systèmes)
Copyright (c) 2017-2022 (ESI Group)
Copyright (c) 2011-2017 (Scilab Enterprises)
Copyright (c) 1989-2012 (INRIA)
Copyright (c) 1989-2007 (ENPC)
with contributors
Last updated:
Thu Oct 24 11:13:09 CEST 2024