[TOC]

polyfit

Basic Idea

In the following discussion, means either the real line or the complex plane . Given data points for . The polynomial fitting problem is to find an -th order polynomial

to minimise the sum of squared errors (SSE)

where is the Vandermonde matrix

and the vectors

To fit an -th order polynomial to data points, we solve the so-called least-square problem:

In this app, the least-square problem is solved by QR decomposition, as we will show in the following.

Solving the Least-square Problem

For convenience, let be the length of . Using QR decomposition with column pivoting, we get

where , is the permutation matrix, and . Let . Then,

where is upper triangular and . For any given , we define

where and . As a result,

We divide into two vectors,

where and . Then,

This can be minimized by selecting and properly. Then, is a solution to the least-square problem. The selection of and depends on the size and rank of , which will be discussed below.

Case 1: Full Rank Overdetermined system

In this case, . Therefore, . Hence, and are empty and . Then, is minimized if is the solution of the upper triangular system . In the special case where , the vector is also empty and hence . That means, the polynomial will pass through of the data points.

Case 2: Rank Deficient Overdetermined system

For convenience, let . In this case, . Therefore, . Hence, , and are all non-empty. Then, is minimized when and is the solution of the upper triangular system . There can be other choices of and that minimise . Hence,

is one of the many solutions to the least-square problem.

Case 3: Full Rank Underdetermined system

In this case, . Therefore, . Hence, and are non-empty but is empty. Then, if and is the solution of the upper triangular system . Hence, the polynomial will pass through all of the data points. There can be other choices of and that minimise . Hence,

is one of the many solutions to the least-square problem.

Case 4: Rank Deficient Underdetermined system

For convenience, let . In this case, . Therefore, . Hence, , and are all non-empty. Then, is minimized if and is the solution of the triangular system . There can be other choices of and that minimize . Hence,

is one of the many solutions to the least-square problem.

Summary

 Full rankRank deficient
Overdetermined system, i.e., is the unique polynomial that minimises the mean-square error. In the special case where , the polynomial given by passes through all of the data points.The polynomial fitting problem has many solutions, where is among one of them and is called a basic solution.
Underdetermined system, i.e., The polynomial fitting problem has many solutions, where is among one of them and is called a basic solution. The polynomial given by p passes through all of the data points.The polynomial fitting problem has many solutions, where is among one of them and is called a basic solution.

Usages

p = polyfit(x,y,n)

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

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

Examples

Input
clear
% Data points.
x=linspace(0,2);
y=x.^2+normrnd(0,0.2,[1 length(x)]);
% Get polynomial of degree 2 that fits the data.
p=polyfit(x,y,2)
scatter(x,y)
hold('on')

% Plot the polynomial
xx=0:0.01:2;
yy=polyval(p,xx);
plot(xx,yy)
hold('off')
Output
All variables cleared.

p = 1e-1 × 
 9.9945  -0.0455   0.1359
Input
clear
% Data points.
% y is constant.
x=1:5;
y=rand*ones(1,length(x));
% Get polynomial of degree 3 that fits the data.
% It returns a zero-th degree polynomial because the y-data are constant.
[p,S]=polyfit(x,y,3)
scatter(x,y)
hold('on')

% Plot the polynomial
xx=0:0.01:5;
yy=polyval(p,xx);
plot(xx,yy)
ylim([0 1])
ylabel('y')
xlabel('x')
hold('off')
Output
All variables cleared.

p = 1e-1 × 
-0.0000   0.0000  -0.0000   3.9839

S =
 struct with fields:
    df: [1.0000]
     R: Double [4 x 4]
 normr: [3.3766] × 1e-16